Netty ChannelFutureListener对operationComplete执行数据库更新

时间:2019-06-14 11:25:06

标签: java apache-camel netty

我想在ChannelFutureListener返回成功时更新数据库中记录的状态,并且我想在EventExectuor线程而不是在IO线程上执行此更新。 有人可以验证以下方法是否正确吗?

我正在使用Apache Camel中的Netty4组件,并且管道中的最后一个处理程序将执行业务逻辑。

 ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new ....Decoder()));
    ch.pipeline().addLast("idlehandler", new IdleStateHandler(..));
    ch.pipeline().addLast(new DefaultEventExecutorGroup(100),"handler", new ServerChannelHandler());

private class ServerResponseListener implements ChannelFutureListener {
        private String connectionId;

        public LineHandlerServerResponseListener(String connectionId) {
            this.connectionId = connectionId;
        }

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                LOGGER.warn(
                        "Cannot write response " + response + " to " + future.channel().remoteAddress() + " due to ",
                        future.cause());
            } else {
                LOGGER.info("Successfully sent response {} to the remote host {}", response,
                        future.channel().remoteAddress());
                future.channel().pipeline().lastContext().executor().execute(new Runnable() {

                    @Override
                    public void run() {
                        updateMessageStatus(connectionId);

                    }
                });
            }
        }

    }

我正确提交给eventexecutor吗?

0 个答案:

没有答案