|
1 | 1 | # DeferredJava Project |
2 | 2 |
|
3 | | -**`DeferredJava`** is a thread safe non-blocking `deferred/promise` implementation for `Java`. It provides an easy to use fluent `deferred/promise` API that can be used to manage complex flow of asynchronus operations easily. |
| 3 | +**`DeferredJava`** is a non-blocking, thread safe `Deferred/Promise` implementation for `Java`. It provides an easy to use fluent `Deferred/Promise` API that can be used to manage complex flow of asynchronus operations easily. |
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | | -Both `deferred` and `promise` means the same thing a `promise` that represents the result of a `asynchronus operation` that is not completed. The difference is that `deferred` provides a `interface` that can access to the `promise object` to change its state where as `promise` provides an interface that cannot. |
| 7 | +Both `Deferred` and `Promise` means the same thing a `promise` that represents the result of a `asynchronus operation` that is not completed. The difference is that `Deferred` provides a `interface` that can access to the `Promise object` to change its state where as `Promise` provides an interface that cannot. |
8 | 8 |
|
9 | 9 | It is not very easy to controll a complex flow of asynchronus operations specially where there are many asynchronus operations needs to be done and one or more operations depands on another one or some other operations to complete and those other operations may wait for other operations to complete. Promise helps us to establish a callback chain that can be used to maintain any cmplex flow of asynchronus operations easily. |
10 | 10 |
|
@@ -39,9 +39,29 @@ It is not very easy to controll a complex flow of asynchronus operations special |
39 | 39 | * `when(callable1).then(...)` |
40 | 40 | * `when(callable1,callable2,callable3).then(...)` |
41 | 41 | * After finish of a callable the deferred object will be automatically resolved by the return value. However, `RunnableDeferred` class can be used to controll when the deferred is resolved. |
| 42 | + |
| 43 | +## How to Build? |
| 44 | + |
| 45 | +* Need JDK7 or more |
| 46 | +* Need Apache Maven |
| 47 | +* Use `mvn clean package install` to build the project and then install in your local maven repository. |
| 48 | + |
| 49 | +Use below maven dependency in your applications. |
| 50 | + |
| 51 | +```xml |
| 52 | + |
| 53 | + <dependency> |
| 54 | + <groupId>org.djava</groupId> |
| 55 | + <artifactId>deferredjava</artifactId> |
| 56 | + <version>0.1-SNAPSHOT</version> |
| 57 | + </dependency> |
| 58 | + |
| 59 | +``` |
42 | 60 |
|
43 | 61 | ## Quick Examples |
44 | 62 |
|
| 63 | +Below are some quick example snippets. For complete examples please see examples in source code. |
| 64 | + |
45 | 65 | ### 1. Hello World!! |
46 | 66 |
|
47 | 67 | ```java |
@@ -119,10 +139,23 @@ It is not very easy to controll a complex flow of asynchronus operations special |
119 | 139 | //to reject use deferred.reject(new RuntimeException("Hello Hell!!")); |
120 | 140 | ``` |
121 | 141 |
|
122 | | -### 3. When all with Callable |
| 142 | +### 3. When usage examples. |
| 143 | + |
| 144 | +For now we have implemented when.all method. Later we will implement other popular when methods. |
| 145 | + |
| 146 | +You can use Callable objects as arguments of these method. The library will convert the callable objects to a deffered task and will be submitted automatically to its built in asynchronous task execution container. So you souuld run the container before using whens. There are also other types like DeferredTask, RunnnableDeffered and Promise that the when method also supports other than Callable type. |
| 147 | + |
| 148 | +DefferedTask and RunnableDeffered tasks will not be automatically submitted to the container. You can use these classes' submit methods to submit the tasks. |
| 149 | + |
| 150 | +There is a small difference between DeferredTask and RunnableDeffered. For DefferedTask the library will automatically resolve the deferred object as soon as the call returns where as in a RunnableDeferred object you will decide when the deferred obeject will be resolved. |
| 151 | + |
| 152 | +#### 3.1 When example1 |
123 | 153 |
|
124 | 154 | ```java |
125 | 155 |
|
| 156 | + //first, you should create the container |
| 157 | + DeferredContainer.createNewContainer(); |
| 158 | + |
126 | 159 | Deferred.when(new Callable<String>(){ |
127 | 160 | public String call() throws Exception { |
128 | 161 | Thread.sleep(100); |
@@ -162,6 +195,70 @@ It is not very easy to controll a complex flow of asynchronus operations special |
162 | 195 | } |
163 | 196 | }); |
164 | 197 |
|
165 | | - //now wait sometimes to give some time to finish tasks |
| 198 | + //now wait sometimes to give some time to finish the asynchronous tasks |
166 | 199 | Thread.wait(1000); |
| 200 | + |
| 201 | + //finally, you should stop the container |
| 202 | + DeferredContainer.getContainer().stop(); |
| 203 | +``` |
| 204 | + |
| 205 | +#### 3.2 When example2 |
| 206 | + |
| 207 | +```java |
| 208 | + |
| 209 | + //first, you should create the container |
| 210 | + DeferredContainer.createNewContainer(); |
| 211 | + |
| 212 | + DeferredTask<String> dt1 = new DeferredTask<>(new Callable<String>(){ |
| 213 | + public String call() throws Exception { |
| 214 | + Thread.sleep(100); |
| 215 | + return "Hello"; |
| 216 | + } |
| 217 | + }); |
| 218 | + |
| 219 | + DeferredTask<String> dt2 = new DeferredTask<>(new Callable<String>(){ |
| 220 | + public String call() throws Exception { |
| 221 | + Thread.sleep(200); |
| 222 | + if(accept) { |
| 223 | + return " "; |
| 224 | + } |
| 225 | + |
| 226 | + throw new RuntimeException("Second call failed"); |
| 227 | + } |
| 228 | + }); |
| 229 | + |
| 230 | + DeferredTask<String> dt3 = new DeferredTask<>(new Callable<String>(){ |
| 231 | + public String call() throws Exception { |
| 232 | + return "World!!"; |
| 233 | + } |
| 234 | + }); |
| 235 | + |
| 236 | + Deferred.when(dt1, dt2, dt3).then(new SuccessCallBack<Object, List<String>>() { |
| 237 | + @Override |
| 238 | + public Object call(List<String> values) { |
| 239 | + String result = ""; |
| 240 | + for(String value : values) { |
| 241 | + result = result + value; |
| 242 | + } |
| 243 | + System.out.println(result); |
| 244 | + return VoidType.NOTHING; |
| 245 | + } |
| 246 | + }).fail(new Callbacks.FailureCallBack() { |
| 247 | + @Override |
| 248 | + public VoidType call(Exception reason) { |
| 249 | + System.out.println(reason.getMessage()); |
| 250 | + return VoidType.NOTHING; |
| 251 | + } |
| 252 | + }); |
| 253 | + |
| 254 | + dt1.submit(); |
| 255 | + dt2.submit(); |
| 256 | + dt3.submit(); |
| 257 | + |
| 258 | + //now wait sometimes to give some time to finish the asynchronous tasks |
| 259 | + Thread.wait(1000); |
| 260 | + |
| 261 | + //finally, you should stop the container |
| 262 | + DeferredContainer.getContainer().stop(); |
| 263 | + |
167 | 264 | ``` |
0 commit comments