异步调用命令

public interface IThreadPoolExcutor {
	
	void execute(Runnable task);

	Future<?> submit(Runnable task);

	<T> Future<T> submit(Callable<T> task);
}


public class ThreadPoolExcetor extends Service implements IThreadPoolExcutor{

	private ThreadPoolTaskExecutor taskExecutor;
	
	@Override
	protected void initService() throws ServiceException {

	}

	@Override
	protected void startService() throws ServiceException {

	}

	@Override
	protected void stopService() throws ServiceException {

	}

	public ThreadPoolTaskExecutor getTaskExecutor() {
		return taskExecutor;
	}

	public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	@Override
	public void execute(Runnable task) {
		taskExecutor.execute(task);
		
	}

	@Override
	public Future<?> submit(Runnable task) {
		return taskExecutor.submit(task);
	}

	@Override
	public <T> Future<T> submit(Callable<T> task) {
		return taskExecutor.submit(task);
	}

	
}


public class CommandExecutor {
	//全局只有一个 CommandContext,里面变量注意会导致内存溢出和线程安全问题
	private static CommandContext  executor  = new CommandContextImpl();
	
	public static void execute(Command cmd) throws Exception {
		execute(cmd, executor);
	}
	
	public static void execute(Command cmd, CommandContext context) throws Exception {
		context.execute(cmd);
	}
}

public class CommandContextImpl implements CommandContext {

	private IThreadPoolExcutor exec; //异步调度执行器
	//private List<Command> history = new LinkedList<Command>(); //命令堆栈

    public CommandContextImpl() {
    	exec =(IThreadPoolExcutor)ServiceFactory.getService("threadPoolService");
    }

	public void execute(final Command cmd) throws Exception {
	    if(cmd==null){
	        return;
	    }
		cmd.setContext(this);
		if(cmd instanceof IAsynchronousCommand) {
			ASDataCenterPlugin.getDefault().debug(cmd.getClass() + " 执行多线程异步操作");
			exec.execute((new Runnable() {
				public void run() {
					try {
						cmd.execute();
					} catch (Exception e) {
						cmd.rollback(); //异步命令吃掉异常
					} finally {
						cmd.finallyExecutor();
					}
				}
			}));
		} else {
			try {
				cmd.execute();
			} catch (Exception e) {
				cmd.rollback();
				throw new RuntimeException(e); //抛出异常
			} finally {
				cmd.finallyExecutor();
			}
		}
	}

	public List<Command> getHistory() {
		//return Collections.unmodifiableList(history);
		throw new UnsupportedOperationException();
	}

	public void rollback() {
//		for (int i = history.size() - 1; i >= 0; i--) {
//			Command cmd = history.get(i);
//			try {
//				System.out.println(cmd.getClass() + " rolllback!!!!!");
//				cmd.rollback();
//			} catch (Exception e) {
//				logger.log(Level.SEVERE, "", e);
//			}
//		}
		throw new UnsupportedOperationException();
	}
}

public abstract class Command implements IAsynchronousCommand {
	protected CommandContext context;

	public Command() {

	}

	public Command(CommandContext context) {
		this.context = context;
	}

	public abstract void execute();

	public void rollback() {
		// do nothing
	}

	public void finallyExecutor(){
		// do nothing
	}

	public <T> T getResult() {
		throw new UnsupportedOperationException();
	}

	public  String getName() {
		return null;
	}

	public CommandContext getContext() {
		return context;
	}

	public void setContext(CommandContext context) {
		this.context = context;
	}
}

private int perform(Activity activity) {
		int successfulCount = 0;
		for (String handlerName : activity.getHandlers()) {
			IActivityHandler handler = this.handlers.get(handlerName);
			if (handler == null) {
				
				continue;
			}
			try {
				Command command = handler.processData(activity.clone()); // process动作是单线程
				CommandExecutor.execute(command); // 这里是多线程异步操作
				successfulCount++;
			} catch (Exception e) {
				
			}
		}
		return successfulCount;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值