Skip to content

Commit dd9cb7b

Browse files
committed
some more quick example snippets added
1 parent 9c24160 commit dd9cb7b

4 files changed

Lines changed: 179 additions & 6 deletions

File tree

README.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# DeferredJava Project
22

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.
44

55
## Usage
66

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.
88

99
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.
1010

@@ -39,9 +39,29 @@ It is not very easy to controll a complex flow of asynchronus operations special
3939
* `when(callable1).then(...)`
4040
* `when(callable1,callable2,callable3).then(...)`
4141
* 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+
```
4260

4361
## Quick Examples
4462

63+
Below are some quick example snippets. For complete examples please see examples in source code.
64+
4565
### 1. Hello World!!
4666

4767
```java
@@ -119,10 +139,23 @@ It is not very easy to controll a complex flow of asynchronus operations special
119139
//to reject use deferred.reject(new RuntimeException("Hello Hell!!"));
120140
```
121141

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
123153

124154
```java
125155

156+
//first, you should create the container
157+
DeferredContainer.createNewContainer();
158+
126159
Deferred.when(new Callable<String>(){
127160
public String call() throws Exception {
128161
Thread.sleep(100);
@@ -162,6 +195,70 @@ It is not very easy to controll a complex flow of asynchronus operations special
162195
}
163196
});
164197

165-
//now wait sometimes to give some time to finish tasks
198+
//now wait sometimes to give some time to finish the asynchronous tasks
166199
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+
167264
```

examples/src/main/java/org/djava/examples/WhenExample.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.djava.async.Callbacks.SuccessCallBack;
2323
import org.djava.async.Deferred;
2424
import org.djava.async.util.DeferredContainer;
25+
import org.djava.async.util.DeferredContainer.DeferredTask;
2526
import org.djava.async.util.VoidType;
2627

2728
public class WhenExample {
@@ -67,6 +68,57 @@ public VoidType call(Exception reason) {
6768

6869
}
6970

71+
@SuppressWarnings("unchecked")
72+
public void whenAllExampleWithDeferredTask(final boolean accept) throws Exception {
73+
74+
DeferredTask<String> dt1 = new DeferredTask<>(new Callable<String>(){
75+
public String call() throws Exception {
76+
Thread.sleep(100);
77+
return "Hello";
78+
}
79+
});
80+
81+
DeferredTask<String> dt2 = new DeferredTask<>(new Callable<String>(){
82+
public String call() throws Exception {
83+
Thread.sleep(200);
84+
if(accept) {
85+
return " ";
86+
}
87+
88+
throw new RuntimeException("Second call failed");
89+
}
90+
});
91+
92+
DeferredTask<String> dt3 = new DeferredTask<>(new Callable<String>(){
93+
public String call() throws Exception {
94+
return "World!!";
95+
}
96+
});
97+
98+
Deferred.when(dt1, dt2, dt3).then(new SuccessCallBack<Object, List<String>>() {
99+
@Override
100+
public Object call(List<String> values) {
101+
String result = "";
102+
for(String value : values) {
103+
result = result + value;
104+
}
105+
System.out.println(result);
106+
return VoidType.NOTHING;
107+
}
108+
}).fail(new Callbacks.FailureCallBack() {
109+
@Override
110+
public VoidType call(Exception reason) {
111+
System.out.println(reason.getMessage());
112+
return VoidType.NOTHING;
113+
}
114+
});
115+
116+
dt1.submit();
117+
dt2.submit();
118+
dt3.submit();
119+
120+
}
121+
70122
public static void main(String[] args) throws Exception {
71123
//first, we should create the container
72124
DeferredContainer.createNewContainer();
@@ -79,6 +131,15 @@ public static void main(String[] args) throws Exception {
79131

80132
example.whenAllExample(false);
81133

134+
///////////////////////////////////////////
135+
136+
example.whenAllExampleWithDeferredTask(true);
137+
138+
//give some time to finish the asynchronous tasks
139+
Thread.sleep(1000);
140+
141+
example.whenAllExampleWithDeferredTask(false);
142+
82143
//give some time to finish the asynchronous tasks
83144
Thread.sleep(1000);
84145

src/main/java/org/djava/async/Deferred.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,19 @@ public static <T> Promise<List<T>> when(Callable<T>... callables) {
186186
return when(promises);
187187
}
188188

189+
/**
190+
* The when method for multiple {@link DeferredTask} objects
191+
*
192+
* @param deferredTasks the deferred tasks
193+
*
194+
* @return a new promise
195+
*/
196+
public static <T> Promise<List<T>> when(DeferredTask<T>... deferredTasks) {
197+
Promise<T>[] promises = (Promise<T>[]) new Promise<?>[deferredTasks.length];
198+
for(int i = 0; i < deferredTasks.length; i++) {
199+
promises[i] = deferredTasks[i].promise();
200+
}
201+
return when(promises);
202+
}
203+
189204
}

src/main/java/org/djava/async/util/DeferredContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ private DeferredContainer(ThreadPoolExecutor executor) {
6060
* Creates a container with the default setup.
6161
*
6262
*/
63-
public synchronized static void createNewContainer() {
63+
public synchronized static DeferredContainer createNewContainer() {
6464
if(container == null) {
6565
container = new DeferredContainer();
6666
}
6767

68-
return;
68+
return container;
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)