0
votes
  • I am using apache camel to receive all messages from the queue. But I need to mention sleep time of thread to run the camel context.
  • Now the problem is it consumes only some messages ie. within the sleep time specified.
  • If I increase the sleep time then it will route all the messages, but it waits until the completion of sleep time to terminate the program.
  • What I need is, the camel should route all the messages in the queue and then it should terminate immediately.

Is there any way to run the camel without specifying thread sleep time using java DSL?

Sample Code :

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class SplitJson {

    public static void main(String[] args) {

        try {
            CamelContext context = new DefaultCamelContext();
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
                    ActiveMQConnection.DEFAULT_BROKER_URL);
            context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("test-jms:queue:testMQDestination")
                    .choice()
                    .when().jsonpath("$.[?(@.Status == 'YetToStart')]")
                    .to("test-jms:queue:YetToStart")
                    .when().jsonpath("$.[?(@.Status == 'Started')]]")
                    .to("test-jms:queue:Started")
                    .when().jsonpath("$.[?(@.Status == 'Completed')]]")
                    .to("test-jms:queue:Completed")
                    .otherwise()
                    .to("test-jms:queue:Others")
                    .end();
                }
            });
            context.start();
            Thread.sleep(10000);
            context.stop();
            System.out.println("Done");
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

}
1
why to not use destinationInterceptors activemq.apache.org/virtual-destinations.html or activemq.apache.org/broker-camel-component.html on the broker side ? you can edit the config when using destinationInterceptors without restarting the broker when using this plugin activemq.apache.org/runtime-configuration.html - Hassen Bennour
You don't need to put sleep. You just need to ensure the CamelContext runs forever. camel.apache.org/… - Souciance Eqdam Rashti
@SoucianceEqdamRashti But the execution need to terminate after routing all the messages in the queue - Karthikeyan KR
Just stop everything from within the route. There is an EIP for that. - Souciance Eqdam Rashti

1 Answers

2
votes

You need to build your own logic to check out when Camel is idle and no more messages is on that queue.

You can use a route policy and then in the onExchangeDone you can reset a clock which is the event when a new message comes in. And then if there has not been new messages after X period that clock hits a timeout or something which you then know to stop the JVM.

This ticket: https://issues.apache.org/jira/browse/CAMEL-10596 is about something similar to be able to auto stop after X seconds, messages or being idle for X time. So it will come out of the box in Camel 2.19 onwards.