0
votes

Have Apache Camel simple message route from folder to ActiveMQ topic:

//Create context to create endpoint, routes, processor within context scope
        CamelContext context = new DefaultCamelContext();


        //Create endpoint route
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception 
            {
                from("file:data/outbox").to("activemq:topic:Vadim_Topic");
                //from("activemq:topic:TEST").to.to("file:data/outbox");
            }

        });

        context.start();
            Thread.sleep(5000);
        context.stop();
    }

And JMS implementation if Topic Consumer:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        try {

            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            //connection.setClientID("12345");

            connection.start();
            Topic topic = session.createTopic("Vadim_Topic");
            MessageConsumer messageConsumer = session.createConsumer(topic);

            MessageListener messageListener = new MessageListener() {

                public void onMessage(Message message) {

                    TextMessage textMessage = (TextMessage) message;
                    try {
                        System.out.println("Received message: " + textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            };

            messageConsumer.setMessageListener(messageListener);

        } catch (Exception e) {
            e.printStackTrace();
        }

Can't understand why is my consumer can't recieve messages sent by Camel route?? I guess thet problem is then I need to subscribe my JMS Consumer on messages sent by Camel? How can I do this if this is the case?

1
Are you sure that the files are successfully read and send to the topic?Geert Schuring

1 Answers

0
votes

Camel not only allows you to send messages to a topic, it can also very easily read messages from a topic and send it to one of your POJOs.

A route that reads from your topic and sends the messages to a POJO would look like this:

from("activemq:topic:Vadim_Topic").bean(ExampleBean.class);

Camel will figure out which method to call on the POJO depending on the type of message it received, and the available method signatures. See this page for details on using POJO's in camel routes: https://camel.apache.org/bean.html