http://www.oschina.net/p/jdeferred
package com.jiepu.testjdeferred;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.jdeferred.AlwaysCallback;
import org.jdeferred.Deferred;
import org.jdeferred.DoneCallback;
import org.jdeferred.FailCallback;
import org.jdeferred.ProgressCallback;
import org.jdeferred.Promise;
import org.jdeferred.Promise.State;
import org.jdeferred.impl.DefaultDeferredManager;
import org.jdeferred.impl.DeferredObject;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
//test001();
test002();
}
private static void test002() {
Deferred deferred = new DeferredObject();
Promise promise=deferred.promise();
promise.done(new DoneCallback() {
@Override
public void onDone(Object result) {
System.out.println("onDone");
}
}).fail(new FailCallback() {
@Override
public void onFail(Object rejection) {
System.out.println("onFail");
}
}).progress(new ProgressCallback() {
@Override
public void onProgress(Object progress) {
System.out.println("onProgress");
}
}).always(new AlwaysCallback() {
@Override
public void onAlways(State state, Object result, Object rejection){
System.out.println("onAlways");
}
});
promise.then(new DoneCallback() {
@Override
public void onDone(Object arg0) {
System.out.println("onDone");
}
});
}
private static void test001() {
final ValueHolder<Integer> holder = new ValueHolder<Integer>();
final AtomicInteger failCount = new AtomicInteger();
DefaultDeferredManager deferredManager = new DefaultDeferredManager();
deferredManager.when(successCallable(100, 10000))
.done(new DoneCallback() {
public void onDone(Object result) {
holder.set((Integer) result);
System.out.println("onDone");
}
}).fail(new FailCallback() {
public void onFail(Object result) {
failCount.incrementAndGet();
System.out.println("onFail");
}
});
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
deferredManager.shutdown();
while (!deferredManager.isTerminated()) {
try {
deferredManager.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
}
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(holder.get());
System.out.println(failCount.get());
}
protected static <R> Callable<R> successCallable(final R result,
final int waitMs) {
return new Callable<R>() {
@Override
public R call() throws Exception {
if (waitMs > 0) {
Thread.sleep(waitMs);
}
System.out.println("successCallable");
return result;
}
};
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiepu</groupId>
<artifactId>testjdeferred</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testjdeferred</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jdeferred</groupId>
<artifactId>jdeferred-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>