org.apache.activemq
activemq-all
5.11.0
消息生产者:
package com.harry.activitymq.demo;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息生产者
* @author Harry Wan
*
*/
public class JMSProducer {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //apache默认提供的用户名
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//apache默认提供的密码
public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址,消息总线(failover://tcp://localhost:61616)
public static final Integer SEND_COUNT = 10; //发送10条消息
public static void main(String[] args) {
try {
//实例化链接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
//通过链接工厂获取链接
Connection connection = connectionFactory.createConnection();
//启动链接
connection.start();
//创建session消息总线,true=是否启动事物,Session.AUTO_ACKNOWLEDGE = 自动确认客户端受到的消息
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//通过session创建消息队列,指向消息的目的地址
Destination destination = session.createQueue("First");
//创建消息生产者
MessageProducer messageProducer = session.createProducer(destination);
for (int i = 0; i < SEND_COUNT; i++) {
//Session 创建消息实体
Message msg = session.createTextMessage("ActivityMQ Send Msg: " + i);
//生产者发送消息
messageProducer.send(msg);
//添加了事物,最后提交发送
session.commit();
System.out.println("Message has been sent: " + i );
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消息消费者:
package com.harry.activitymq.demo;
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 javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息的消费者(点对点方式)
*
* @author Harry Wan
*
*/
public class JMSConsumer {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER; // apache默认提供的用户名
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// apache默认提供的密码
public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;// 默认的链接地址,消息总线(failover://tcp://localhost:61616)
public static void main(String[] args) {
try {
//实例化链接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
//通过链接工厂获取链接
Connection connection = connectionFactory.createConnection();
//启动链接
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//通过session创建消息队列,指向消息的目的地址
Destination destination = session.createQueue("First");
//创建消息的消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
for (;;) {
TextMessage textMessage = (TextMessage)messageConsumer.receive(1000);// 每隔1000ms接受一次
if(textMessage != null){
System.out.println("收到的消息: " + textMessage.getText());
}else{
break;
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注: 这里消息的消费者,一直处于无限循环的方式每隔1000ms接收一次消息,实际开发中通常是使用注册监听的方式,当有消息产生时,触发监听器从而接受消息。
监听器实现:
package com.harry.activitymq.demo;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Listener implements MessageListener {
/**
* 消息监听
*/
public void onMessage(Message message) {
try {
System.out.println("收到的消息: " + ((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
try {
//实例化链接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
//通过链接工厂获取链接
Connection connection = connectionFactory.createConnection();
//启动链接
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//通过session创建消息队列,指向消息的目的地址
Destination destination = session.createQueue("First");
//创建消息的消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
messageConsumer.setMessageListener(new Listener());//注册消息监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.基于发布订阅的模式
消息发布者:
package com.harry.activitymq.publish.demo;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息发布者
* @author Harry Wan
*
*/
public class JMSPublisher {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //apache默认提供的用户名
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//apache默认提供的密码
public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址,消息总线(failover://tcp://localhost:61616)
public static final Integer SEND_COUNT = 10; //发送10条消息
public static void main(String[] args) {
try {
//实例化链接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
//通过链接工厂获取链接
Connection connection = connectionFactory.createConnection();
//启动链接
connection.start();
//创建session消息总线,true=是否启动事物,Session.AUTO_ACKNOWLEDGE = 自动确认客户端受到的消息
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//通过session创建消息队列,指向消息的目的地址
//Destination destination = session.createQueue("First");
Destination destination = session.createTopic("FirstTopic");
//创建消息生产者
MessageProducer messageProducer = session.createProducer(destination);
for (int i = 0; i < SEND_COUNT; i++) {
//Session 创建消息实体
Message msg = session.createTextMessage("ActivityMQ Send Msg: " + i);
//生产者发送消息
messageProducer.send(msg);
//添加了事物,最后提交发送
session.commit();
System.out.println("Message has been publish: " + i );
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消息的订阅者:
package com.harry.activitymq.publish.demo;
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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息的订阅者(发布订阅方式)
*
* @author Harry Wan
*
*/
public class JMSubscriber {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER; // apache默认提供的用户名
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// apache默认提供的密码
public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;// 默认的链接地址,消息总线(failover://tcp://localhost:61616)
public static void main(String[] args) {
try {
//实例化链接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
//通过链接工厂获取链接
Connection connection = connectionFactory.createConnection();
//启动链接
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//通过session创建消息队列,指向消息的目的地址
//Destination destination = session.createQueue("First");
//创建消息订阅者
Destination destination = session.createTopic("FirstTopic");
//创建消息的消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
messageConsumer.setMessageListener(new Listener());//注册消息监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相比较于点对点的发送,主要是将创建的消息队列改为创建会话,发布会话所有的订阅者都能接收,1对多模式
实际开发中用的比较多的也是发布订阅模式
Final: 消息的状态,以及消息的发布者,消费者,消息队列等均可以在客户端界面进行管理!!