java_user
java_user

Reputation: 939

Apache Camel: Why I can't send bean text to jms

I have very simple pojo class:

public class MessageBean {

    String text;

    public String getMessage()
    {
        return text;
    }

}

And camel route:

public static void main(String[] args) {

        final MessageBean bean = new MessageBean();
        bean.text = "This is the text";

        CamelContext context = new DefaultCamelContext();
        ConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

        context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(conFactory));

        try {
            context.addRoutes(new RouteBuilder() {

                @Override
                public void configure() throws Exception {

                    from("direct:hello").process(new Processor() {

                        public void process(Exchange exchange) throws Exception {

                            exchange.getIn().setBody(bean);
                        }
                    }).setBody(simple("${body.message}")).to("jms:queue:Test.Queue");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            context.start();
            Thread.sleep(5000);
            context.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

I don't understand why I can not send text from bean variable text to activemq queue??

When I try to send from folder the txt file it sends correctly to the queue in jms.

Upvotes: 0

Views: 820

Answers (1)

mdnghtblue
mdnghtblue

Reputation: 1117

To insert a message into a camel route, you need to send it to an endpoint that is a consumer in your route, in this case, direct:start. The easiest way to do this here is to use ProducerTemplate. After your context has been started:

 ProducerTemplate template = context.createProducerTemplate();
 template.sendBody("direct:start", bean);

Although if you ultimately just want to send the contents of bean.getMessage() to your JMS queue (that's what it looks like you're trying to do here), you could just do this instead and remove the setBody() call from your route:

 template.sendBody("direct:start", bean.getMessage());

More information on ProducerTemplate

Upvotes: 1

Related Questions