前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊puma的Dispatcher

聊聊puma的Dispatcher

作者头像
code4it
发布2020-06-09 13:09:55
3730
发布2020-06-09 13:09:55
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下puma的Dispatcher

Dispatcher

puma/puma/src/main/java/com/dianping/puma/sender/dispatcher/Dispatcher.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Dispatcher
  • AbstractDispatcher
  • SimpleDispatcherImpl
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档