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

Disruptor之ExceptionHandler

作者头像
克虏伯
发布2020-06-18 10:54:42
1.1K0
发布2020-06-18 10:54:42
举报

Disruptor的版本3.4.2.

List-1

代码语言:javascript
复制
public interface ExceptionHandler<T>
{
    /**
     * <p>Strategy for handling uncaught exceptions when processing an event.</p>
     *
     * <p>If the strategy wishes to terminate further processing by the {@link BatchEventProcessor}
     * then it should throw a {@link RuntimeException}.</p>
     *
     * @param ex       the exception that propagated from the {@link EventHandler}.
     * @param sequence of the event which cause the exception.
     * @param event    being processed when the exception occurred.  This can be null.
     */
    void handleEventException(Throwable ex, long sequence, T event);

    /**
     * Callback to notify of an exception during {@link LifecycleAware#onStart()}
     *
     * @param ex throw during the starting process.
     */
    void handleOnStartException(Throwable ex);

    /**
     * Callback to notify of an exception during {@link LifecycleAware#onShutdown()}
     *
     * @param ex throw during the shutdown process.
     */
    void handleOnShutdownException(Throwable ex);
}

    如List-1所示,它是个接口,定义了异常抛出时回调的方法。

    如下List-2所示,Disruptor中默认使用ExceptionHandler的ExceptionHandlerWrapper实现,如List-3所示,使用了代理,委托内部的delete来处理。

List-2

代码语言:javascript
复制
public class Disruptor<T>
{
    private final RingBuffer<T> ringBuffer;
    private final Executor executor;
    private final ConsumerRepository<T> consumerRepository = new ConsumerRepository<>();
    private final AtomicBoolean started = new AtomicBoolean(false);
    private ExceptionHandler<? super T> exceptionHandler = new ExceptionHandlerWrapper<>();
...

List-3

代码语言:javascript
复制
public class ExceptionHandlerWrapper<T> implements ExceptionHandler<T>
{
    private ExceptionHandler<? super T> delegate = new FatalExceptionHandler();

    public void switchTo(final ExceptionHandler<? super T> exceptionHandler)
    {
        this.delegate = exceptionHandler;
    }
...

    除了ExceptionHandlerWrapper这个实现,还有其它FatalExceptionHandler、IgnoreExceptionHandler,IgnoreExceptionHandler只是将异常打印下,但是FatalExceptionHandler则不同,正如其名字所示,它内部将异常再次封装到RuntimeException再次抛出,如下List-4

List-4

代码语言:javascript
复制
public final class FatalExceptionHandler implements ExceptionHandler<Object>
{
    private static final Logger LOGGER = Logger.getLogger(FatalExceptionHandler.class.getName());
    private final Logger logger;

    public FatalExceptionHandler()
    {
        this.logger = LOGGER;
    }

    public FatalExceptionHandler(final Logger logger)
    {
        this.logger = logger;
    }

    @Override
    public void handleEventException(final Throwable ex, final long sequence, final Object event)
    {
        logger.log(Level.SEVERE, "Exception processing: " + sequence + " " + event, ex);

        throw new RuntimeException(ex);
    }
...

    如果使用了FatalExceptionHandler,运行中抛出异常,那么会时Disruptor线程阻塞的。ExceptionHandlerWrapper和BatchEventProcessor中exceptionHandler都默认使用了FatalExceptionHandler,所以要特别注意。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

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