路由完成后,Apache骆驼的onCompletion回调不会触发

时间:2018-12-07 08:10:06

标签: java apache-camel disruptor-pattern

我在骆驼中设置了以下路线。我已经在这里发布了完整的代码。 调用ProcessorTwo后,我希望可以调用ProcessorOnComplete,但根本不会触发它。我在这里想念什么?

公共类CamelRoute扩展了RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("disruptor:routingChannel?concurrentConsumers=10")
            .onCompletion()
            .process(new ProcessorOnComplete())
            .end()
            .to("disruptor:processingOne?concurrentConsumers=10")
            .process(new ProcessorOne())
            .to("disruptor:processingTwo?concurrentConsumers=10")
            .process(new ProcessorTwo())
            .stop();
    }
}

public class ProcessorOne implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println("Procesing one");
    }
}

public class ProcessorTwo implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println("Procesing two");
    }
}

public class ProcessorOnComplete implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println("Completion Mayuran");
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new CamelRoute());
        camelContext.start();

        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();

        producerTemplate.sendBody("disruptor:routingChannel", "Message");

        Thread.sleep(1000*1000);
    }
}

1 个答案:

答案 0 :(得分:0)

onCompletion()在测试时会运行,只要我有发送到破坏者队列的消息的使用者。

如果Exchange是InOnly,您将看到描述的行为,并且您没有任何消耗发送到disruptor:processingOnedisruptor:processingTwo的消息的东西

onCompletion正在等待干扰者队列中的消息得到处理。

之所以调用处理器ProcessorOne()ProcessorTwo()是因为骆驼不等待响应,因为交换是InOnly。

如果Exchange为InOut,则路由将等待第一个干扰者的响应,并且可能在30秒后超时。这样您就看不到来自处理器的消息。