How to set two RabbitMQ connections for the same Camel RouteBuilder configure method

为君一笑 提交于 2019-12-13 03:16:01

问题


I want listen FROM rabbit queue, process, and post message TO another rabbit queue. I'm not working with spring. The messages are duplicated in this configuration.

This is a short code:

context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("rabbitmq://localhost/B?autoDelete=false&queue=worker&threadPoolSize=1&autoAck=false").
                    log(LoggingLevel.INFO, "Message ${id}").
                    to("rabbitmq://localhost/B?autoDelete=false&queue=processed");
        }
    });

I have tried this too:

public static void main(String[] args) throws Exception {
    org.apache.camel.impl.DefaultCamelContext context = new DefaultCamelContext();

    com.rabbitmq.client.ConnectionFactory connectionFactoryWorker = new ConnectionFactory();
    com.rabbitmq.client.ConnectionFactory connectionFactoryProcessed = new ConnectionFactory();
    org.apache.camel.impl.SimpleRegistry registry = new SimpleRegistry();
    String camelBeanNameWorker = "connectionFactoryWorker";
    String camelBeanNameProcessed = "connectionFactoryProcessed";
    registry.put(camelBeanNameWorker, connectionFactoryWorker);
    registry.put(camelBeanNameProcessed, connectionFactoryProcessed);
    context.setRegistry(registry);

    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("rabbitmq://localhost/B?connectionFactory=connectionFactoryWorker&autoDelete=false&queue=worker&threadPoolSize=1&autoAck=false").
                    log(LoggingLevel.INFO, "Message ${id}").
                    to("rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed");
        }
    });

    context.start();
    Thread.sleep(10000l);
    context.stop();
}

Worker queue start in four messages, but Messages are re-queued:

561 Message ID-host-1510600494291-0-2
566 Message ID-host-1510600494291-0-4
566 Message ID-host-1510600494291-0-6
567 Message ID-host-1510600494291-0-8
568 Message ID-host-1510600494291-0-10
571 Message ID-host-1510600494291-0-12
572 Message ID-host-1510600494291-0-14
572 Message ID-host-1510600494291-0-16
573 Message ID-host-1510600494291-0-18
574 Message ID-host-1510600494291-0-20
574 Message ID-host-1510600494291-0-22
575 Message ID-host-1510600494291-0-24
576 Message ID-host-1510600494291-0-26
576 Message ID-host-1510600494291-0-28
577 Message ID-host-1510600494291-0-30
578 Message ID-host-1510600494291-0-32
578 Message ID-host-1510600494291-0-34
...
64k after
...
10561 Message ID-host-1510600494291-0-128690
10561 Message ID-host-1510600494291-0-128692
10561 Message ID-host-1510600494291-0-128694
10561 Message ID-host-1510600494291-0-128696


回答1:


Looking at your code, without knowing the outcome, I noticed that you are missing that you are missing the "#" when you are trying to use a bean from the registry.

What you need to do when using something from registry is

public static void main(String[] args) throws Exception {
DefaultCamelContext context = new DefaultCamelContext();

ConnectionFactory connectionFactory = new ConnectionFactory();
SimpleRegistry registry = new SimpleRegistry();
String beanNameWorker = "connectionFactoryWorker";
String beanNameProcessed = "connectionFactoryProcessed";
registry.put(beanNameWorker, connectionFactory);
registry.put(beanNameProcessed, connectionFactory);
context.setRegistry(registry);

context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() throws Exception {
        from("rabbitmq://localhost/B?connectionFactory=#connectionFactoryWorker&autoDelete=false&queue=worker&threadPoolSize=1&autoAck=false").
                log(LoggingLevel.INFO, "Message ${id}").
                to("rabbitmq://localhost/B?connectionFactory=#connectionFactoryProcessed&autoDelete=false&queue=processed");
    }
});

}

By the way what is the actual outcome? what errors do you have?




回答2:


I believe I may have found your answer in another question

Infinite loop in Camel - Rabbit MQ

Apparently setting BridgeEnpoint=false option stops this. Without specifying this the EXCHANGE_NAME and ROUTING_KEY headers are taken from the received message overriding whatever you might have specified in the "to" configuration. For example... rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed&BridgeEndpoint=false



来源:https://stackoverflow.com/questions/47263407/how-to-set-two-rabbitmq-connections-for-the-same-camel-routebuilder-configure-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!