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;
}