如果出现错误,Apache Camel将从路由中断开连接并继续路由

时间:2016-12-16 16:12:54

标签: java apache-camel

我有一个在运行时管理的RouteBuilder。我想在没有向客户端发送响应的情况下抛出错误时将客户端从路由中断开。我还需要在整个过程中保持路由。如何在不停止路线的情况下断开客户端连接?任何帮助将不胜感激。

@Override
public void configure() throws Exception {

    onException(Exception.class)
    .handled(true)
    .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Log.error(String.format(Constants.ERROR_ROUTING_TO, endPointFrom.toUrl(), endPointTo.toUrl(), 
                exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class)));
            //TODO I WANT TO DISCONNECT FROM ROUTE HERE
        }

    });

    from(endPointFrom.toUrl())
    .routeId(endPointFrom.getId())
    .description(endPointFrom.toString())
    .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {


        }
    })
    .to(endPointTo.toUrl());

}

1 个答案:

答案 0 :(得分:0)

我通过在交换变量的响应主体上设置null来解决我的问题,因此Apache Camel断开客户端而不发送响应消息。

@Override
public void configure() throws Exception {

    onException(Exception.class)
    .handled(true)
    .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Log.error(String.format(Constants.ERROR_ROUTING_TO, endPointFrom.toUrl(), endPointTo.toUrl(), 
                exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class)));
                exchange.getOut().setBody(null);
        }

    });

    from(endPointFrom.toUrl())
    .routeId(endPointFrom.getId())
    .description(endPointFrom.toString())
    .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {

        }

    })
    .to(endPointTo.toUrl());

}