**
ActiveMQ的发布和订阅
**
一、创建订阅者
订阅者1:
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Consumer1 {
private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址
public static void main(String[] args) {
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接
Session session = null;//会话
Destination destination;//消息目的地 就是一个消息队列
MessageConsumer messageConsumer = null;//消息消费者
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
try {
//1、连接MQ
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start(); //启动连接
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createTopic("短信发送T"); //这里的队列的名字必须与生产者的队列名字一样
messageConsumer = session.createConsumer(destination);//创建消息消费者
//2、消息监听器
messageConsumer.setMessageListener(new MyMessageListener1());
}catch (Exception e){
e.printStackTrace();
}
}
}
class MyMessageListener1 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage)message;
System.out.println("Consumer1从MQ获取的消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
订阅者2:
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Consumer2 {
private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址
public static void main(String[] args) {
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接
Session session = null;//会话
Destination destination;//消息目的地 就是一个消息队列
MessageConsumer messageConsumer = null;//消息消费者
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
try {
//1、连接MQ
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start(); //启动连接
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createTopic("短信发送T"); //这里的队列的名字必须与生产者的队列名字一样
messageConsumer = session.createConsumer(destination);//创建消息消费者
//2、消息监听器
messageConsumer.setMessageListener(new MyMessageListener2());
}catch (Exception e){
e.printStackTrace();
}
}
}
class MyMessageListener2 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage)message;
System.out.println("Consumer2从MQ获取的消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
**
二、发布者
**
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Producer {
private static final String USERNAME= ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址
public static void main(String[] args) {
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接
Session session = null;//会话
Destination destination;//消息目的地 就是一个消息队列
MessageProducer messageProducer = null;//消息生产者
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
try {
//1、连接MQ
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createTopic("短信发送T");
messageProducer = session.createProducer(destination);//创建消息生产者
//2、发送消息
for(int i=0;i<10;i++){
String text = "123456:13800001111"+i;
TextMessage msg = session.createTextMessage(text);
messageProducer.send(destination,msg);
System.out.println("发送到MQ:"+i);
}
session.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("关闭");
//3.断开
try{
messageProducer.close();
session.close();
connection.close();
}catch (Exception e){
}
}
}
}
**
三、测试
**
测试的时候先把订阅者启动,进行监听,然后启动发布者,发布者一旦发布新的信息,订阅者就会收到消息。