user320478
user320478

Reputation: 155

Camel keep sending messages to queue via JMS after 1 minute

I am currently learning Camel and i am not sure if we can send messages to a activemq queue/topic from camel at fixed interval.

Currently i have created code in Scala which looks up the database and create a message and sends it to queue after every minute can we do this in camel.

We have a timer component in camel but it does not produce the message. I was thinking something like this.

from("timer://foo?fixedRate=true&period=60000")
.to("customLogic")
.to("jms:myqueue")
  1. Timer will kick after a minute.
  2. Custom logic will do database look up and create a message
  3. Finally send to jms queue

I am very new to Camel so some code will be really helpful thanks

Can you please point me to how can i create this customeLogic method that can create a message and pass it to next ".to("jms:myqueue")". Is there some class that in need to inherit/implement which will pass the the message etc.

Upvotes: 1

Views: 1568

Answers (1)

SebastianBrandt
SebastianBrandt

Reputation: 459

I guess your question is about how to hook custom java logic into your camel route to prepare the JMS message payload.

The JMS component will use the exchange body as the JMS message payload, so you need to set the body in your custom logic. There are several ways to do this.

You can create a custom processor by implementing the org.apache.camel.Processor interface and explicitly setting the new body on the exchange:

Processor customLogicProcessor = new Processor() {
    @Override
    public void process(Exchange exchange) {
        // do your db lookup, etc.
        String myMessage = ...
        exchange.getIn().setBody(myMessage);
    }
};

from("timer://foo?fixedRate=true&period=60000")
    .process(customLogicProcessor)
    .to("jms:myqueue");

A more elegant option is to make use of Camel's bean binding:

public class CustomLogic {
    @Handler
    public String doStuff() {
        // do your db lookup, etc.
        String myMessage = ...
        return myMessage;
    }
}

[...]

CustomLogic customLogicBean = new CustomLogic();
from("timer://foo?fixedRate=true&period=60000")
        .bean(customLogicBean)
        .to("jms:myqueue");

The @Handler annotation tells Camel which method it should call. If there's only one qualifying method you don't need that annotation. Camel makes the result of the method call the new body on the exchange that will be passed to the JMS component.

Upvotes: 1

Related Questions