Home > database >  Spring boot Camel problems when using rest post to send a message to rabbitmq
Spring boot Camel problems when using rest post to send a message to rabbitmq

Time:09-03

I have a spring boot application that publish a rest endpoint to receives xml messages using apache camel, below the relevant part of the code corresponding to the configuration of the route:

        restConfiguration()
            .component("servlet")
            .bindingMode(RestBindingMode.auto);
    
        rest("/api/").description("REST Message Endpoint Service")
            .id("rest-producer-route")
            // Accept post requests at /api/message
            .post("/message")
            // request in xml format             
            .consumes("application/xml")
            .to("direct:remoteService");

        from("direct:remoteService") 
            // RABBIT_URI = "spring-rabbitmq:%s?routingKey=%s&arg.queue.autoDelete=false";
            .to(String.format(CamelConfiguration.RABBIT_URI, "coreEngine", "coreEngine"))
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201));

When trying to send a message in xml format using Postman, after about 30 seconds I get a 500 error, with the following exception:

org.springframework.amqp.core.AmqpReplyTimeoutException: Reply timed out
    at org.springframework.amqp.rabbit.AsyncRabbitTemplate$RabbitFuture$TimeoutTask.run(AsyncRabbitTemplate.java:762) ~[spring-rabbit-2.4.6.jar:2.4.6]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.22.jar:5.3.22]
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

However on the rabbitmq server the message was successfully saved to the coreEngine queue.

I don't need any transformation to be done, the goal of the endpoint is simply to receive messages in xml format and store them in a rabbitmq queue.

From what I could see, an AsyncRabbitTemplate is used, would that be the cause of the problem? What should I do to configure it correctly?

CodePudding user response:

This is because the exchange pattern by default is expecting a reply, hence camel is using the AsyncRabbitTemplate.sendAndReceive. If you just want to fire and forget, set the exchange pattern to ExchangePattern.InOnly for Camel to use RabbitTemplate.send.

You code should be changed to,

from("direct:remoteService") 
            // RABBIT_URI = "spring-rabbitmq:%s?routingKey=%s&arg.queue.autoDelete=false";
            .to(ExchangePattern.InOnly, String.format(CamelConfiguration.RABBIT_URI, "coreEngine", "coreEngine"))
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201));

Refer the Apache Camel SpringRabbitMQProducer source code SpringRabbitMQProducer

  • Related