{{theTime}}

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(
"tcp://localhost:61616");
connection = connectionFactory.createConnection();
connection.setClientID("Test");
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
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);

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

session.close();
} finally {
if (connection != null) {
connection.close();
}
}
}
}

Note:
- 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:

Generate Models from SQL Server using Entity Framework Core

To generate models from SQL Server database tables using Entity Framework (EF) in .NET, you can follow the Database-First approach with Ent...