Search code examples
spring-bootapache-camelmicroservicesjmsactivemq-artemis

Create thread and route message from Camel to microservice and back


I'm using Camel with JMS and Spring Boot and would like to build a route for next scenario:

User 1 (MQTT client) sends a message (topic) to ActiveMQ Artemis. Camel (by using from) catch that message and print it out with a help of log.

Thing I would like to do is - create a new thread (asynchronous) for caught message. Send that message from Camel to microservice (python program) that should take message and insert some extra strings, and then send changed message back to Camel and ActiveMQ. On the end, from ActiveMQ changed message will be sent to User 2.

Can you give me some directions or route examples of how to do something like that?

So, key points are to create new thread for every message and create route to and back from that microservice.


Solution

  • The route could look like this

    from("jms:queue:inputQueue")
        .log("${body}")
        .to("http://oldhost")
        .to("jms:queue:outputQueue")
    

    Some notes:

    • You can call a downstream HTTP endpoint with .to(). The current message body is used as request body. The response of the service overwrites the message body.
    • I don't know why you want to create a new thread for a synchronous call. You can leverage parallel processing by consuming multiple messages from Artemis in parallel with multiple consumers. Like this, every message is processed in its own thread. If your motivation is resilience, there is also a Circuit Breaker EIP in Camel
    • If you use Camel 2.x, use the HTTP4 Component ("http4://") to use the newer HTTP client lib. In Camel 3.x the old one was dropped and the new one is simply called HTTP component