DJ180
DJ180

Reputation: 19854

Apache Camel: Producer template does not add to SEDA endpoint

I am not sure what the problem is with my small application, if it resides on the RouteBuilder or within the ProducerTemplate

Either way, my "Test message" is not logged when running this application.

What may be going wrong here?

public static void main(String[] args) {

        Main main = new Main();
        main.addRouteBuilder(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("seda:myqueue").log(LoggingLevel.INFO, "${in.body").end();
            }
        });



        ProducerTemplate producerTemplate = main.getOrCreateCamelContext().createProducerTemplate();
        producerTemplate.setDefaultEndpointUri("seda:myqueue");
        producerTemplate.sendBody("Test message");
    }

Upvotes: 1

Views: 1273

Answers (1)

Souciance Eqdam Rashti
Souciance Eqdam Rashti

Reputation: 3191

It doesn't look like you are creating and starting the context which is probably why the message never reaches your route. Here is an example to get you started:

https://examples.javacodegeeks.com/enterprise-java/apache-camel/apache-camel-hello-world-example/

import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelHelloWorldExample {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        try {
            context.addComponent("activemq", ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"));
            context.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("activemq:queue:test.queue")
                    .to("stream:out");
                }
            });
            ProducerTemplate template = context.createProducerTemplate();
            context.start();
            template.sendBody("activemq:test.queue", "Hello World");
            Thread.sleep(2000);
        } finally {
            context.stop();
        }
    }
}

Notice context.start() and context.stop();

Upvotes: 1

Related Questions