Karthikeyan KR
Karthikeyan KR

Reputation: 1174

Receive messages from activemq using apache camel java DSL

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();
        }

    }

}

Upvotes: 0

Views: 1697

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55540

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.

Upvotes: 2

Related Questions