对于消息的传递有两种类型:
1.一种是点对点的(Queue),即一个生产者和一个消费者一一对应;
2.另一种是发布/订阅模式(Topic),即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。
一, 点对点式消息队列(Queue ):如果没有人处理,就会持久化储存知道有人来处理
消息生产者
package com.easytop.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* name:zengjun
* Date: 2018/11/5
* Time: 10:40
* Version:1.0
* impact:
*/
public class QueueConsumer {
public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的目的地
Destination destination = null;
//消息消费者
MessageConsumer messageConsumer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个连接QueueTest的消息队列
destination = session.createQueue("QueueTest");
//创建消息消费者
messageConsumer = session.createConsumer(destination);
//为了能够多次发送
while (true) {
TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
if (textMessage != null) {
System.out.println("成功接收消息:" + textMessage.getText());
} else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消息消费者
package com.easytop.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.Scanner;
/**
* name:zengjun
* Date: 2018/11/5
* Time: 9:47
* Version:1.0
* impact:
*/
public class QueueDemo {
public static void main(String[] args) {
//连接信息设置
String username = "system";
String password ="manager";
String borkerURL="failover://tcp://192.168.4.187:61616";
//工厂类
ConnectionFactory connectionFactory= null;
//连接
Connection connection= null;
//会话
Session session= null;
//消息的目的地
Destination destination= null;
//消息生产者
MessageProducer messageProducer=null;
//实例化连接工厂
connectionFactory= new ActiveMQConnectionFactory(username,password,borkerURL);
try {
//创建连接
Connection factoryConnection = connectionFactory.createConnection();
//开始链接
factoryConnection.start();
//创建session
session = factoryConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("QueueTest");
messageProducer = session.createProducer(destination);
TextMessage message = null;
boolean a= true;
System.out.println("请输入你想要发送的消息(按0退出)");
while (a){
Scanner scanner= new Scanner(System.in);
String tmpMessage = scanner.nextLine();
//创建发送的的文本信息
if ("0".equals(tmpMessage)){
a=false;
}else{
message = session.createTextMessage("Queue消息测试:"+tmpMessage);
messageProducer.send(message);
System.out.println("发送成功!"+message.getText());
}
//需要session提交了之后另一端才能接收到消息
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
}finally {
if (null!=connection){
try {
connection.close();
}catch (JMSException e){
e.printStackTrace();
}
}
}
}
}
运行结果为:
二, 主题发布订阅式(Topic): 过时不候
主题发布者
package com.easytop.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.Scanner;
/**
* name:zengjun
* Date: 2018/11/5
* Time: 15:27
* Version:1.0
* impact:
*/
public class TopicDemo {
public static void main(String[] args) {
//连接信息设置
String username = "system";
String password ="manager";
String borkerURL="failover://tcp://localhost:61616";
//工厂类
ConnectionFactory connectionFactory= null;
//连接
Connection connection= null;
//会话
Session session= null;
//消息主题
Topic topic = null;
//消息生产者
MessageProducer messageProducer=null;
//实例化连接工厂
connectionFactory= new ActiveMQConnectionFactory(username,password,borkerURL);
try {
//创建连接
// Connection factoryConnection = connectionFactory.createConnection();
//通过工厂获得连接
connection=connectionFactory.createConnection();
//开始链接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Topic topicTest = session.createTopic("TopicTest");
messageProducer = session.createProducer(topicTest);
//不将数据持久化保存
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = null;
boolean a= true;
System.out.println("请输入你想要发送的消息(按0退出)");
while (a){
Scanner scanner= new Scanner(System.in);
String tmpMessage = scanner.nextLine();
//创建发送的的文本信息
if ("0".equals(tmpMessage)){
a=false;
}else{
message = session.createTextMessage("Topic消息测试:"+tmpMessage);
messageProducer.send(message);
System.out.println("发送成功!"+message.getText());
}
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
}finally {
if (null!=connection){
try {
connection.close();
}catch (JMSException e){
e.printStackTrace();
}
}
}
}
}
消息订阅者
package com.easytop.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* name:zengjun
* Date: 2018/11/5
* Time: 15:28
* Version:1.0
* impact:
*/
public class TopicConsumer {
public static void main(String[] args) {
//连接信息设置
String username = "system";
String password = "manager";
String brokerURL = "failover://tcp://localhost:61616";
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息主题
Topic topic =null;
//消息消费者
MessageConsumer messageConsumer = null;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个连接Topic的消息队列
topic = session.createTopic("TopicTest");
//创建消息消费者
messageConsumer = session.createConsumer(topic);
messageConsumer.setMessageListener(new MyMessageListener());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
class MyMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收订阅主题:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
运行结果: