Java Deferred/Promise 库JDeferred 实例

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 holder = new ValueHolder();
		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  Callable successCallable(final R result,
			final int waitMs) {
		return new Callable() {

			@Override
			public R call() throws Exception {
				if (waitMs > 0) {
					Thread.sleep(waitMs);
				}
				System.out.println("successCallable");

				return result;
			}
		};
	}
}


	4.0.0

	com.jiepu
	testjdeferred
	0.0.1-SNAPSHOT
	jar

	testjdeferred
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.10
			test
		
		
			org.jdeferred
			jdeferred-core
			1.2.4
		

	
	
	
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.7
					1.7
				
			

		
	
	



你可能感兴趣的:(java)