什么是ActiveMQ?
ActiveMQ是一种开源的基于JMS(Java Message Servie)规范的一种消息中间件的实现,ActiveMQ的设计目标是提供标准的,面向消息的,能够跨越多语言和多系统的应用集成消息通信中间件。
ActiveMQ 有两种模式,分别是队列模式和点对点模式。
队列模式=== 比如生产方发了 10条消息到 activeMQ 服务器, 而此时有多个消费方,那么这些消费方就会瓜分这些10条消息,一条消息只会被一个消费方得到。
订阅模式===就是发布/订阅模式。 比如生产方发了10条消息,而此时有多个消费方,那么多个消费方都能得到这 10条消息,就如同订阅公众号那样。
1.下面以队列模式为例:
首先去下载ActiveMQ安装包,解压
在bin目录下面win64或者win32运行
然后访问地址 http://127.0.0.1:8161/ 就可以看到如图所示的界面。
新建Maven项目activemq
pom.xml导入jar包
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
</dependencies>
新建两个类
消费者TestConsumer 和 生产者TestProducer
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import java.util.UUID;
/**
* 消费者
* @author root
*
*/
public class TestConsumer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次消费的消息名称
private static final String topicName="queue_style";
//消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
private static final String consumerName="consumer-" + UUID.randomUUID();
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
System.out.printf("%s 消费者启动了。 %n", consumerName);
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建一个目标 (主题类型)
Destination destination=session.createQueue(topicName);
//6.创建一个消费者
MessageConsumer consumer=session.createConsumer(destination);
//7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message arg0) {
// TODO Auto-generated method stub
TextMessage textMessage=(TextMessage)arg0;
try {
System.out.println(consumerName +" 接收消息:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
//connection.close();
}
}
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;
public class TestProducer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次发送的消息名称
private static final String topicName="queue_style";
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建一个目标 (队列类型)
Destination destination=session.createQueue(topicName);
//6.创建一个生产者
MessageProducer producer=session.createProducer(destination);
for (int i = 0; i < 100; i++) {
//7.创建消息
TextMessage textMessage=session.createTextMessage("队列消息-"+i);
//8.发送消息
producer.send(textMessage);
System.out.println("发送:"+textMessage.getText());
}
//7. 关闭连接
connection.close();
}
}
测试办法:
1. 首先运行两次 TestConsumer 类,以启动两个不同的消费者
2. 运行一次 TestProducer, 以启动 生产者。
这个生产者会生产100条消息。
这两个消费者会分食这100条消息,如图所示这样的效果,两个消费者分别瓜分不同的消息。
访问地址: http://127.0.0.1:8161/admin/queues.jsp 就可以看到 刚才的消息处理情况。
queue_style 是在代码中定义的消息名称。
number Of Consumers 表示有2个消费者。
Messages Enqueued:表示收到了 100 个消息。
Messages Dequeued:表示消费了 100 个消息。
2.下面以订阅模式为例:
修改TestProducer 类 ,TesConsumer类
Destination destination=session.createQueue(topicName);
修改为
Destination destination=session.createTopic(topicName);
同理测试:
1. 首先运行两次 TestConsumer 类,以启动两个不同的消费者
2. 运行一次 TestProducer, 以启动 生产者。
这个生产者会生产100条消息。
这两个消费者都会收到这100条消息,如图所示这样的效果,两个消费者都能获取全部的信息。
访问地址: http://127.0.0.1:8161/admin/topics.jsp 就可以看到 刚才的消息处理情况。
看最下面那行:
queue_style是在代码中定义的消息名称。(这里名字应该改成topic_style)
number Of Consumers 表示有2个消费者。
Messages Enqueued:表示收到了 100 个消息。
Messages Dequeued:表示消费了 200 个消息。