ActiveMQ Starter Example Demo
Turn from https://www.cnblogs.com/parryyang/p/6062160.html
JMS message model
JMS message service application architecture supports two models: point-to-point model and publisher/subscriber model.
(1) Point-to-Point Model (Queue)
A producer publishes a message to a specific queue from which a consumer reads the message in turn.
Model features: Only one consumer receives information.
(2) Publisher/Subscriber Model (Topic)
0 or more subscribers can receive messages on specific topics.
Model features: multiple consumers can get messages.
The biggest difference between Topic and Queue is that Topic notifies all online monitored clients of new messages in the form of broadcasting, while the client without listening will not receive messages; Queue notifies one of several monitored clients in the form of point-to-point.
JMS message format
- MapMessage -- key-value key-value pairs
- TextMessage -- String object
- ObjcetMessage -- A Serialized Java Object
- ByteMessage -- An unexplained byte stream
- StreamMessage -- Data stream of Java raw values
Point-to-Point Model Demo
public class Constants {
public static final String MQ_NAME = "parry";
public static final String MQ_PASSWORD = "parry123";
public static final String MQ_BROKETURL = "tcp://192.168.56.129:61616";
}
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.parry.demo.constant.Constants;
/**
*
* <p>
* MSProduct Point-to-Point Model-Message Producer
* <p>
*/
public class MSProduct {
public static void main(String[] args) {
// Connection factory
ConnectionFactory factory;
// Connection instance
Connection connection = null;
// Thread instances of transceivers and receivers
Session session;
// Message Sending Target Address
Destination destination;
// Message Creator
MessageProducer messageProducer;
try {
factory = new ActiveMQConnectionFactory(Constants.MQ_NAME, Constants.MQ_PASSWORD,
Constants.MQ_BROKETURL);
// Get the connection instance
connection = factory.createConnection();
// Start connection
connection.start();
// Create a thread instance to receive or send (create) session Defines whether transactions should be enabled, and the transaction type is Auto_ACKNOWLEDGE That is, consumers succeed in Listern When a message is returned, the session automatically determines that the user receives the message.
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Create a queue (return to a message destination)
destination = session.createQueue("parryQuene");
// Create a message producer
messageProducer = session.createProducer(destination);
// Establish TextMessage Message entity
TextMessage message = session.createTextMessage("I am parry,This is my first news!");
messageProducer.send(message);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.parry.demo.constant.Constants;
/**
* <p>
* MQConsumer Point-to-point -- message consumers
* <p>
*/
public class MQConsumer {
public static void main(String[] args) {
// Connection factory
ConnectionFactory connectionFactory;
// Connection instance
Connection connection = null;
// Thread instances of transceivers and receivers
Session session;
// Message Sending Target Address
Destination destination;
try {
// Instantiate Connection Factory
connectionFactory = new ActiveMQConnectionFactory(Constants.MQ_NAME, Constants.MQ_PASSWORD, Constants.MQ_BROKETURL);
// Get the connection instance
connection = connectionFactory.createConnection();
// Start connection
connection.start();
// Create a thread instance to receive or send(Consumers don't need to start business anymore.
session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
// Create a queue (return to a message destination)
destination = session.createQueue("parryQuene");
// Creating message consumers
MessageConsumer consumer = session.createConsumer(destination);
//Registered message listener
consumer.setMessageListener(new MQListerner());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* <p>
* MQListerner Producer Monitor
* <p>
*/
public class MQListerner implements MessageListener{
@Override
public void onMessage(Message message) {
try {
System.out.println(((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Publisher/Subscriber Model Demo
(1) Publisher
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.parry.demo.constant.Constants;
/**
* <p>
* MQProducer Sender of subscription message
* <p>
*/
public class MQProducer {
public static void main(String[] args) {
// Connection factory
ConnectionFactory factory;
// Connection instance
Connection connection = null;
// Thread instances of transceivers and receivers
Session session;
// Message Sending Target Address
Destination destination;
try {
// Instantiate Connection Factory
factory = new ActiveMQConnectionFactory(Constants.MQ_NAME, Constants.MQ_PASSWORD, Constants.MQ_BROKETURL);
// Get the connection instance
connection = factory.createConnection();
// Start connection
connection.start();
// Create a thread instance to receive or send(Establish session Defines whether transactions should be enabled, and the transaction type is Auto_ACKNOWLEDGE That is, consumers succeed in Listern When a message is returned, the session automatically determines that the user receives the message.
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Create a queue (return to a message destination)
destination = session.createTopic("parryTopic");
// Create a message publisher
MessageProducer producer = session.createProducer(destination);
// Establish TextMessage news
TextMessage message = session.createTextMessage("Hello, this is my first news!");
// Publish news
producer.send(message);
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
(2) Subscriber 01
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.parry.demo.constant.Constants;
/**
* <p>
* MQCousumer01 Subscription-Publishing Mode Subscriber 01
* <p>
*/
public class MQCousumer01 {
public static void main(String[] args) {
// Connection factory
ConnectionFactory factory;
// Connection instance
Connection connection = null;
// Thread instances of transceivers and receivers
Session session;
// Message Sending Target Address
Destination destination;
try {
// Instantiate Connection Factory
factory = new ActiveMQConnectionFactory(Constants.MQ_NAME, Constants.MQ_PASSWORD, Constants.MQ_BROKETURL);
// Get the connection instance
connection = factory.createConnection();
// Start connection
connection.start();
// Create a thread instance to receive or send
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
// Create a queue (return to a message destination)
destination = session.createTopic("parryTopic");
// Create message subscribers
MessageConsumer consumer = session.createConsumer(destination);
// Message Publisher Add Listener
consumer.setMessageListener(new Listerner01());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* <p>
* Listerner01 Subscriber 01 listener
* <p>
*/
public class Listerner01 implements MessageListener{
@Override
public void onMessage(Message message) {
try {
System.out.println("Subscriber 01 receives the message:" + ((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
(3) Subscriber 02
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.parry.demo.constant.Constants;
/**
* <p>
* MQCousumer02 Subscription-Publishing Mode Subscriber 02
* <p>
*/
public class MQCousumer02 {
public static void main(String[] args) {
// Connection factory
ConnectionFactory factory;
// Connection instance
Connection connection = null;
// Thread instances of transceivers and receivers
Session session;
// Message Sending Target Address
Destination destination;
try {
// Instantiate Connection Factory
factory = new ActiveMQConnectionFactory(Constants.MQ_NAME, Constants.MQ_PASSWORD, Constants.MQ_BROKETURL);
// Get the connection instance
connection = factory.createConnection();
// Start connection
connection.start();
// Create a thread instance to receive or send
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
// Create a queue (return to a message destination)
destination = session.createTopic("parryTopic");
// Create message subscribers
MessageConsumer consumer = session.createConsumer(destination);
// Message Publisher Add Listener
consumer.setMessageListener(new Listerner02());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* <p>
* Listerner02 Subscriber 02 listener
* <p>
*/
public class Listerner02 implements MessageListener{
@Override
public void onMessage(Message message) {
try {
System.out.println("Subscriber 02 receives the message:"+((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}