Apache Camel
Goals
Setup ActiveMQ
Create a JMS Message Producer to send messages
Create a Camel Router and Start Camel context to Route Messages
Note: In this example we are not using any spring apis
Setup ActiveMQ Messaging System
1. Download activemq from http://activemq.apache.org/download-archives.html
2. Extract activemq zip file and start ActiveMQ (Click on activemq.bat file)
3. Check activemq console using http://localhost:8161/admin (Use default credentials admin/admin to login)
Create a Message Producer to send Messages
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageProducerExample {
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
ActiveMQConnection.DEFAULT_BROKER_URL);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Destination destination = session.createQueue("testMQ");
// Create a MessageProducer from the Session to the Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage("Helloworld");
//sending message
producer.send(message);
session.close();
connection.close();
System.out.println("Message sent");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Message Producer and check the Queue
Run the above program and see messages are going to the activemq, it provides the console to see the message details
Create a Camel class to route messages
Here we are just consuming the above message and putting in to another queue
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 CamelActiveMQExample {
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:testMQ").log("${body}").to("test-jms:queue:testMQDestination");
}
});
context.start();
Thread.sleep(1000);
context.stop();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Camel route
Now we can check the ActiveMQ console and see new Queue has been created and all the messages are moved from testMQ to testMQDestination queue
Reference
Done!!!
No comments:
Post a Comment