Search This Blog

Total Pageviews

JMS Publish Subscribe Example using Apache Active MQ.

import java.net.URISyntaxException;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JmsPublishSubscriberClientExample {
public static void main(String[] args) throws URISyntaxException, Exception {
Connection connection = null;
try {
// Producer
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Topic topic = session.createTopic("customerTopic");

// Publish
String payload = "PublishNewsTask";
TextMessage msg = session.createTextMessage(payload);
MessageProducer publisher = session.createProducer(topic);
System.out.println("Sending text '" + payload + "'");
publisher.send(msg, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);

// Subscriber1 subscribes to customerTopic
MessageConsumer consumer1 = session.createDurableSubscriber(topic, "consumer1", "", false);

// Subscriber2 subscribes to customerTopic
MessageConsumer consumer2 = session.createDurableSubscriber(topic, "consumer2", "", false);

msg = (TextMessage) consumer1.receive();
System.out.println("Subscriber1" + msg.getText());
msg = (TextMessage) consumer2.receive();
System.out.println("Subscriber2 receives " + msg.getText());

} finally {
if (connection != null) {

- Install Apache Active MQ and start the MQ before executing the program.
- Download  org.apache.activemq jar
- Configure build path to refer  org.apache.activemq.*

No comments:

Cucumber Eclipse sample program

Cucumber Eclipse SetupCreate Eclipse Maven Project Open pom.xml Goto Dependencies Add GroupId: info.cukes, Artifact Id: cucumber-java, Versi...