一个异步获取EJB 服务实例:
EJBServer 模拟服务端:
package com.concurrent.deep;/** * @ClassName: EJBServer * @Description: 提供EJB服务的对象。 * @author Comsys-Administrator * @date 2012-11-30 下午11:17:36 * */public class EJBServer { /* * 实例花EJB对象。 */ public EJBServer() throws InterruptedException { getConnection(); System.out.println("开始实例化EJBServer对象start................"); try { System.out.println("实例化EJBServer对象doing................"); Thread.sleep(200); } catch (InterruptedException e) { //e.printStackTrace(); System.out.println("实例化出异常。。。。。。"); throw e; } System.out.println("实例化EJBServer对象 end................"); } //建立客户端和服务端的连接 。。。。。。。。。 public static void getConnection() throws InterruptedException{ System.out.println("开始与EJBServer连接start................"); try { System.out.println("EJBServer连接doing................"); Thread.sleep(100); } catch (InterruptedException e) { //e.printStackTrace(); System.out.println("EJBServer连接出异常。。。。。。"); throw e; } System.out.println("成功获取EJBServer连接 end................"); } }同步的方式获取EJB服务的TOOLs
package com.concurrent.deep;/** * @ClassName: EJBTools * @Description: 获取EJB实例的工具类 * @author Comsys-Administrator * @date 2012-11-30 下午11:16:48 * */public class EJBTools { private static EJBServer instance; public static EJBServer getInstance() { if (instance == null) { try { instance = new EJBServer(); } catch (InterruptedException e) { System.out.println("获取EJB连接失败或者是获取EJB实例失败"); } } return instance; } public static void main(String[] args) { EJBServer server= EJBTools.getInstance(); if(server==null){ System.out.println("获取EJB连接失败或者是获取EJB实例失败"); }else{ System.out.println("done!"); } }}
异步获取EJB服务的方法:
package com.concurrent.deep;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * @ClassName: SyncEJBTools * @Description: 异步获取EJB实例的工具类 * @author Comsys-Administrator * @date 2012-11-30 下午11:16:48 * */public class SyncEJBTools { private static EJBServer instance; public static EJBServer getInstance() { int timeout = 2; //秒. ExecutorService executor = Executors.newSingleThreadExecutor(); Futurefuture = executor.submit(new Callable (){ @Override public EJBServer call() throws Exception { return new EJBServer(); } });// 将任务提交到线程池中 try { instance = future.get(timeout*40, TimeUnit.MILLISECONDS);// 设定在200毫秒的时间内完成 System.out.println(instance); } catch (InterruptedException e) { System.out.println("线程中断出错。"); future.cancel(true);// 中断执行此任务的线程 } catch (ExecutionException e) { System.out.println("线程服务出错。"); future.cancel(true);// 中断执行此任务的线程 } catch (TimeoutException e) {// 超时异常 System.out.println("超时。"); future.cancel(true);// 中断执行此任务的线程 }finally{ System.out.println("线程服务关闭。"); executor.shutdown(); } return instance; } public static void main(String[] args) { EJBServer server= SyncEJBTools.getInstance(); if(server==null){ System.out.println("获取EJB连接失败或者是获取EJB实例失败"); }else{ System.out.println("done!"); } }}
一个通用的方式来获取EJBServer的方法。
package com.concurrent.deep;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * @ClassName: SyncEJBTools * @Description: 异步获取EJB实例的工具类 * @author Comsys-Administrator * @date 2012-11-30 下午11:16:48 * */public class SyncGernicEJBTools{ private T instance; public T getInstance(final Class tclass) { int timeout = 2; //秒. ExecutorService executor = Executors.newSingleThreadExecutor(); Future future = executor.submit(new Callable (){ @Override public T call() throws Exception { return tclass.newInstance(); } });// 将任务提交到线程池中 try { instance = future.get(timeout*400, TimeUnit.MILLISECONDS);// 设定在200毫秒的时间内完成 System.out.println(instance); } catch (InterruptedException e) { System.out.println("线程中断出错。"); future.cancel(true);// 中断执行此任务的线程 } catch (ExecutionException e) { System.out.println("线程服务出错。"); future.cancel(true);// 中断执行此任务的线程 } catch (TimeoutException e) {// 超时异常 System.out.println("超时。"); future.cancel(true);// 中断执行此任务的线程 }finally{ System.out.println("线程服务关闭。"); executor.shutdown(); } return instance; } public static void main(String[] args) { SyncGernicEJBTools tools=new SyncGernicEJBTools (); EJBServer server= (EJBServer) tools.getInstance(EJBServer.class); if(server==null){ System.out.println("获取EJB连接失败或者是获取EJB实例失败"); }else{ System.out.println("done!"); } }}