public interface EventListener { public void handleEvent(Event event); }
public class EventHandler { private HashSet<EventListener> listeners = new HashSet<EventListener>(); public EventHandler() { } //向观察者注册 public void addListener(EventListener listener) { listeners.add(listener); } //事件触发,通知所有注册的处理者 public void fireEventListeners(Event event) { ArrayList<EventListener> listeners = new ArrayList<EventListener>(this.listeners); for (EventListener listener : listeners) { listener.handleEvent(event); } } //取消注册 public void removeListener(EventListener listener) { listeners.remove(listener); } }
public enum Type { FLOW_STARTED, //一个流启动执行 FLOW_FINISHED, //一个流执行完成 JOB_STARTED, //一个任务开始执行 JOB_FINISHED, //一个任务执行完成 JOB_STATUS_CHANGED, //任务状态变化 EXTERNAL_FLOW_UPDATED, //内部流更新 EXTERNAL_JOB_UPDATED //内部任务更新 }
public class LocalFlowWatcherListener implements EventListener { @Override public void handleEvent(Event event) { if (event.getType() == Type.JOB_FINISHED) { if (event.getRunner() instanceof FlowRunner) { // 这里是在job没有执行(被取消或者跳过)的情况下由FlowRunner通知的 Object data = event.getData(); if (data instanceof ExecutableNode) { ExecutableNode node = (ExecutableNode) data; handleJobStatusChange(node.getNestedId(), node.getStatus()); } } else if (event.getRunner() instanceof JobRunner) { // 这里是在一个job执行完成由jobRunner通知flowRunner,在由FlowRunner通知的。 JobRunner runner = (JobRunner) event.getRunner(); ExecutableNode node = runner.getNode(); System.out.println(node + " looks like " + node.getStatus()); handleJobStatusChange(node.getNestedId(), node.getStatus()); } } else if (event.getType() == Type.FLOW_FINISHED) { stopWatcher(); } } }
原文地址:http://blog.csdn.net/yu616568/article/details/43736499