Apache camel Apache Camel-在route builder上记录的Netty异常

Apache camel Apache Camel-在route builder上记录的Netty异常,apache-camel,netty,Apache Camel,Netty,我不熟悉阿帕奇骆驼和内蒂。我有一个要求,如果连接到netty服务器的套接字断开连接,我需要记录该情况。在ApacheCammelRouteBuilder中,我没有找到任何方法来恢复这些异常,比如IOException、连接重置。我尝试了这些链接的建议 但没能完成。是否有某种方法可以在camel route builder级别获取netty服务器异常?请建议 谢谢默认情况下,驼峰错误处理程序的作用域在exchange的边界内,即从使用者创建exchange到销毁 为了扩展错误处理程序的范围,您

我不熟悉阿帕奇骆驼和内蒂。我有一个要求,如果连接到netty服务器的套接字断开连接,我需要记录该情况。在ApacheCammelRouteBuilder中,我没有找到任何方法来恢复这些异常,比如IOException、连接重置。我尝试了这些链接的建议

但没能完成。是否有某种方法可以在camel route builder级别获取netty服务器异常?请建议


谢谢

默认情况下,驼峰错误处理程序的作用域在exchange的边界内,即从使用者创建exchange到销毁

为了扩展错误处理程序的范围,您需要连接使用者和错误处理程序

您需要创建端点并提供属性键“bridgeErrorHandler”

进一步

from("ref:mynetty).

默认情况下,驼峰错误处理程序的作用域在exchange的边界内,即从使用者创建exchange到销毁

为了扩展错误处理程序的范围,您需要连接使用者和错误处理程序

您需要创建端点并提供属性键“bridgeErrorHandler”

进一步

from("ref:mynetty).

您是否尝试定义clientInitializerFactory? 如果使用clientInitializerFactory,则需要手动添加句柄,例如编码器/解码器等

如果只是关于日志记录,您只需要在连接断开时记录(channelInactive)


您是否尝试定义clientInitializerFactory? 如果使用clientInitializerFactory,则需要手动添加句柄,例如编码器/解码器等

如果只是关于日志记录,您只需要在连接断开时记录(channelInactive)


谢谢,它帮助我在route builder级别获得了异常。谢谢,它帮助我在route builder级别获得了异常。谢谢@chiochuan,我尝试过使用它,但添加它之后,消息似乎没有传播到camel处理器。然后在同一条线上,我尝试了ServerChannelHandler,它似乎正在工作。使用ServerChannelHandler正确吗?你有任何链接,我可以找到更多关于这个的细节。因为我不太了解所有这些处理程序。你是作为制作人还是消费者来做camel netty?以上代码假设是生产者(客户机)。如果使用netty作为使用者(服务器),则可以更改extends ServerInitializerFactory和ServerChannelHandler。您可以参考netty文档了解更多详细信息,谢谢@Chiochuan。我正在使用netty作为消费者(服务器)。谢谢@chiochuan,我尝试过使用它,似乎在添加这个之后消息并没有传播到camel处理器。然后在同一条线上,我尝试了ServerChannelHandler,它似乎正在工作。使用ServerChannelHandler正确吗?你有任何链接,我可以找到更多关于这个的细节。因为我不太了解所有这些处理程序。你是作为制作人还是消费者来做camel netty?以上代码假设是生产者(客户机)。如果使用netty作为使用者(服务器),则可以更改extends ServerInitializerFactory和ServerChannelHandler。您可以参考netty文档了解更多详细信息,谢谢@Chiochuan。我使用netty作为消费者(服务器)。

public class NettyProducerTest extends RouteBuilder {

    public CamelContext CamelContext;

    public NettyProducerTest(CamelContext context) {
        this.CamelContext = context;
    }

    public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext();
        NettyProducerTest test = new NettyProducerTest(context);
        context.addRoutes(test);

        context.start();
        TimeUnit.MINUTES.sleep(5);
        context.stop();
    }

    @Override
    public void configure() throws Exception {

        final org.apache.camel.impl.SimpleRegistry registry = new org.apache.camel.impl.SimpleRegistry();
        final org.apache.camel.impl.CompositeRegistry compositeRegistry = new org.apache.camel.impl.CompositeRegistry();

        compositeRegistry.addRegistry(this.CamelContext.getRegistry());
        compositeRegistry.addRegistry(registry);
        ((org.apache.camel.impl.DefaultCamelContext) this.CamelContext).setRegistry(compositeRegistry);
        registry.put("cipf", new NettyProducerTest.CustomChannelPipelineFactory(null));

        from("stream:in?promptMessage=Enter Msg: ").log("message: ${body}")
                .to("netty4:tcp://0.0.0.0:9999?textline=true&sync=true&clientInitializerFactory=#cipf")
                .log("response: ${body}");

    }

    public class CustomChannelPipelineFactory extends ClientInitializerFactory {

        private NettyProducer producer;

        public CustomChannelPipelineFactory(NettyProducer producer) {
            this.producer = producer;
        }

        @Override
        public ClientInitializerFactory createPipelineFactory(NettyProducer producer) {
            return new CustomChannelPipelineFactory(producer);
        }

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline channelPipeline = ch.pipeline();
            channelPipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("delimeter", new LineBasedFrameDecoder(1000));
            channelPipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("handler", new CustomChannelHandler(producer));
        }
    }

    public class CustomChannelHandler extends ClientChannelHandler {

        private Logger LOG = LoggerFactory.getLogger(CustomChannelHandler.class);

        public CustomChannelHandler(NettyProducer producer) {
            super(producer);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            super.channelInactive(ctx);
            LOG.info("disconnected");
        }
    }

}