小记一下activemq的使用过程,很短
主页地址:http://activemq.apache.org
开发包及源码下载地址:http://activemq.apache.org/activemq-5111-release.html
ActiveMQ 服务启动地址:http://127.0.0.1:8161/admin/ 用户名/密码 admin/admin
windows启动activemq
1.点对点的使用(消息生产者讲解)
引入jar包 activemq-all-5.15.3.jar
或导入依赖
<!-- 整合消息队列ActiveMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- 如果配置线程池则加入 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
创建类并且运行测试
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JMSProducer {
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;//会话
Destination destination;//消息目的地
MessageProducer messageProducer;//消息生产者
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
try {
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createQueue("新闻队列");
messageProducer = session.createProducer(destination);//创建消息生产者
//发送消息
for(int i=0;i<10;i++){
TextMessage msg = session.createTextMessage("小鑫工作室客服" + (i + 1) +"号");
messageProducer.send(destination,msg);
}
session.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
消息的消费者讲解
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JMSConsumer {
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;//会话
Destination destination;//消息目的地
MessageConsumer messageCustomer;//消息生产者
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createQueue("新闻队列");
messageCustomer = session.createConsumer(destination);//创建消息消费者
//发送消息
for(int i=0;i<5;i++){
TextMessage Message = (TextMessage)messageCustomer.receive();
System.out.println(Message.getText());
}
session.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
使用循环后,显示取走了5条消息
2.消息的监听器
监听器
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MyMessgeListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage msg=(TextMessage)message;
if (msg!=null){
try {
System.out.println(msg.getText());
}catch (Exception e){
e.printStackTrace();
}
}
}
}
在消费者类中加入监听器,则属于被动接受消息,监听即处理
处理后,剩余5条消息被取出
3.消息的订阅模式
在上述点对点的模式中,如果消息消费者取出,则无法被其他消费者再次使用,所以推出订阅模式,可以一个消息对应对个消费者