共计 2706 个字符,预计需要花费 7 分钟才能阅读完成。
序
本文主要研究一下 puma 的 Dispatcher
Dispatcher
puma/puma/src/main/java/com/dianping/puma/sender/dispatcher/Dispatcher.java
public interface Dispatcher extends LifeCycle {String getName();
void dispatch(ChangedEvent event, PumaContext context) throws DispatcherException;
List<Sender> getSenders();}
- Dispatcher 定义了 getName、dispatch、getSenders 方法
AbstractDispatcher
puma/puma/src/main/java/com/dianping/puma/sender/dispatcher/AbstractDispatcher.java
public abstract class AbstractDispatcher implements Dispatcher {
private String name;
/*
* (non-Javadoc)
*
* @see com.dianping.puma.common.LifeCycle#start()
*/
@Override
public void start() {}
/*
* (non-Javadoc)
*
* @see com.dianping.puma.common.LifeCycle#stop()
*/
@Override
public void stop() {}
/*
* (non-Javadoc)
*
* @see com.dianping.puma.sender.dispatcher.Dispatcher#getName()
*/
@Override
public String getName() {return name;}
public void setName(String name) {this.name = name;}
protected void throwExceptionIfNeeded(List<Throwable> exceptionList) throws DispatcherException {if (exceptionList != null && !exceptionList.isEmpty()) {StringWriter buffer = new StringWriter();
PrintWriter out = null;
try {out = new PrintWriter(buffer);
for (Throwable exception : exceptionList) {exception.printStackTrace(out);
}
} finally {if (out != null) {out.close();
}
}
throw new DispatcherException(buffer.toString());
}
}
}
- AbstractDispatcher 定义了 throwExceptionIfNeeded 方法,它将 exceptionList 转换为 DispatcherException
SimpleDispatcherImpl
puma/puma/src/main/java/com/dianping/puma/sender/dispatcher/SimpleDispatcherImpl.java
public class SimpleDispatcherImpl extends AbstractDispatcher {private static final Logger log = Logger.getLogger(SimpleDispatcherImpl.class);
private List<Sender> senders;
/**
* @return the senders
*/
public List<Sender> getSenders() {return senders;}
/**
* @param senders
* the senders to set
*/
public void setSenders(List<Sender> senders) {this.senders = senders;}
@Override
public void start() {for (Sender sender : senders) {sender.start();
}
super.start();}
@Override
public void stop() {for (Sender sender : senders) {sender.stop();
}
super.stop();}
@Override
public void dispatch(ChangedEvent event, PumaContext context) throws DispatcherException {if (senders != null && senders.size() > 0) {List<Throwable> exceptionList = new ArrayList<Throwable>();
for (Sender sender : senders) {
try {sender.send(event, context);
} catch (Exception e) {log.error("Exception occurs in sender" + sender.getName());
exceptionList.add(e);
}
}
throwExceptionIfNeeded(exceptionList);
} else {log.warn("No senders in dispatcher" + getName());
}
}
}
- SimpleDispatcherImpl 继承了 AbstractDispatcher,其 start 方法遍历 senders,挨个执行 sender.start();其 stop 方法遍历 senders,挨个执行 sender.stop();其 dispatch 方法遍历 senders,挨个执行 sender.send(event, context)
小结
Dispatcher 定义了 getName、dispatch、getSenders 方法;AbstractDispatcher 定义了 throwExceptionIfNeeded 方法,它将 exceptionList 转换为 DispatcherException;SimpleDispatcherImpl 继承了 AbstractDispatcher,实现了 start、stop、dispatch 方法
doc
- Dispatcher
正文完