ActiveMQ配置文档
本文介绍一对一、一对多、持久化、非持久化消息配置方式
一、创建项目
导入jar


二、创建MQ.xml
<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="failover:(tcp://192.168.1.168:61616)" />
</bean>
集群MQ时value="failover:(tcp://192.168.1.87:61616, tcp://192.168.1.87:61616,tcp://192.168.1.87:61616)
三、队列queue模式(一对一模式)
此模式是一对一的,每条消息只能被一个人使用,类似QQ私聊,其他人看不到消息
1.监听模式
当有消息发出时,会自动接收
①在上面创建的MQ.xml配置文件中添加
<!-- 定义消息队列(Queue),监听一个新的队列,queue2 -->
<bean id="queueDestination2" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>queue2</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="queueDestination2" />
<property name="receiveTimeout" value="10000" />
</bean>
<!--queue消息生产者 -->
<bean id="producerService" class="com.sh.test.Jms_send">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean> <!-- 配置消息队列监听者(Queue),代码下面给出,只有一个onMessage方法 -->
<bean id="queueMessageListener" class="com.sh.test.Jms_jie_auto" /> <!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination2" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
“queueMessageListener”这个class需在项目中写,实例下面有
②创建一个类Jms_jie_auto.java,添加接收消息代码
package com.sh.test; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; public class Jms_jie_auto implements MessageListener {
//当收到消息时,自动调用该方法。
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println("ConsumerMessageListener收到了文本消息:\t"+ tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
} }
③创建一个类Jms_send.java添加发送消息代码
package com.sh.test; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; public class Jms_send implements ProducerService{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
/**
* 向指定队列发送消息
*/
public void sendMessage(Destination destination, final String msg) {
System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
} /**
* 向默认队列发送消息
*/
public void sendMessage(final String msg) {
String destination = jmsTemplate.getDefaultDestination().toString();
System.out.println("向队列" +destination+ "发送了消息------------" + msg);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
}); } public void sendMessage(Destination destination, final String msg, final Destination response) {
System.out.println("ProducerService向队列" + destination + "发送了消息:\t" + msg);
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(msg);
textMessage.setJMSReplyTo(response);
return textMessage;
}
});
} }
④创建ProducerService.java,发送消息实体类
package com.sh.test;
import javax.jms.Destination;
public interface ProducerService {
/**
* 发消息,向默认的 destination
*
* @param msg String 消息内容
*/
public void sendMessage(String msg);
/**
* 发消息,向指定的 destination
*
* @param destination 目的地
* @param msg String 消息内容
*/
public void sendMessage(Destination destination, String msg);
/**
* 发消息,向指定的 destination
*
* @param destination 目的地
* @param msg String 消息内容
*/
/**
* 向指定的destination发送消息,消费者接受消息后,把回复的消息写到response队列
*
* @param destination 目的地
* @param msg String 消息内容
* @param response 回复消息的队列
*/
public void sendMessage(Destination destination, String msg, Destination response);
}
⑤创建Jms_test.java,发送消息测试方法
package com.sh.test; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
public class Jms_test {
/**
* 队列名queue2-监听模式队列
*/
@Autowired
private Destination queueDestination2;
/**
* 队列消息生产者
*/
@Autowired
@Qualifier("producerService")
private ProducerService producer;
/**
* 测试生产者向queue1发送消息
*/
@RequestMapping(value="/shengchanzhe",method=RequestMethod.GET)
public ModelAndView testProduce(HttpServletRequest request, HttpServletResponse response) {
String msg = "Hello world!";
producer.sendMessage(queueDestination2, msg+":auto");//监听模式队列,发送消息后在jms_jie_auto中自动出发事件
return null;
} }
执行结果

2.非监听模式
此模式当有消息进入指定队列时,需调用方法接收消息
①在上面创建的MQ.xml配置文件中添加
注意:如果是在上面配置的基础上添加,只需添加下面代码中的queueDestination和consumerService
<!-- 定义消息队列(Queue) -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>queue1</value>
</constructor-arg>
</bean> <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="queueDestination" />
<property name="receiveTimeout" value="10000" />
</bean> <!--queue消息生产者 -->
<bean id="producerService" class="com.sh.test.Jms_send">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean> <!--queue消息消费者 -->
<bean id="consumerService" class="com.sh.test.Jms_jie_notauto">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
②添加Jms_jie_notauto.java,接收消息代码
package com.sh.test;
import javax.jms.Destination; import javax.jms.JMSException;
import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate;
/**
* 接收jms消息,非监听模式
* @author Administrator
*
*/
public class Jms_jie_notauto implements ConsumerService {
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
/**
* 接受消息
*/
public void receive(Destination destination) {
TextMessage tm = (TextMessage) jmsTemplate.receive(destination);
try {
System.out.println("从队列" + destination.toString() + "收到了消息:\t"
+ tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
} }
③添加ConsumerService.java,消费消息的类
package com.sh.test;
import javax.jms.Destination;
public interface ConsumerService {
public void receive(Destination queueDestination);
}
④发送消息测试方法,在上面Jms_test.java 中添加
/**
* 测试生产者向queue1发送消息
*/
@RequestMapping(value="/shengchanzhe",method=RequestMethod.GET)
public ModelAndView testProduce(HttpServletRequest request, HttpServletResponse response) {
String msg = "Hello world!";
producer.sendMessage(queueDestination, msg); //非监听模式队列,发送消息后需调用testConsume()方法接收
return null;
}
⑤接收消息测试方法,在上面Jms_test.java 中添加,分别执行shengchanzhe,fjt_jieshouzhe,即可看到结果
/**
* 队列消息接收者
*/
@Autowired
@Qualifier("consumerService")
private ConsumerService consumer;
/**
* 队列名queue1-非监听模式队列
*/
@Autowired
private Destination queueDestination; /**
* 非监听模式,测试消费者从queue1接受消息
*/
@RequestMapping(value="/fjt_jieshouzhe",method=RequestMethod.GET)
public ModelAndView testConsume(HttpServletRequest request, HttpServletResponse response) {
consumer.receive(queueDestination);
return null;
}
四、订阅topic模式(一对多)
此模式是一对多的,每条消息能被多个人使用,类似QQ群聊
①在上面创建的MQ.xml配置文件中添加
<!-- 定义消息主题(Topic) -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg>
<value>topic_name</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板(Topic),pubSubDomain="true"-->
<bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="topicDestination" />
<property name="pubSubDomain" value="true" /><!-- 此项关了就变成了队列模式 -->
<property name="receiveTimeout" value="10000" />
</bean>
<!--topic消息发布者 -->
<bean id="topicProvider" class="com.sh.test.Jms_topic_send">
<property name="topicJmsTemplate" ref="topicJmsTemplate"></property>
</bean>
<!-- 消息主题监听者 和 主题监听容器 可以配置多个,即多个订阅者 -->
<!-- 消息主题监听者(Topic) -->
<bean id="topicMessageListener" class="com.sh.test.Jms_topic_jie" />
<!-- 主题监听容器 (Topic) -->
<bean id="topicJmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="topicMessageListener" />
</bean>
②添加Jms_topic_jie.java 接收信息代码
package com.sh.test;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; /**
* 订阅者监听端,代码和队列监听一样,需要把这个类配置到xml配置到订阅配置中
* @author Administrator
*
*/
public class Jms_topic_jie implements MessageListener { public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println("TopicMessageListener \t" + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
} }
③添加Jms_topic_send.java,发送代码
package com.sh.test;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; /**
* 发布订阅消息
* @author Administrator
*
*/
public class Jms_topic_send {
private JmsTemplate topicJmsTemplate; /**
* 向指定的topic发布消息
*
* @param topic
* @param msg
*/
public void publish(final Destination topic, final String msg) { topicJmsTemplate.send(topic, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
System.out.println("topic name 是" + topic.toString()
+ ",发布消息内容为:\t" + msg);
return session.createTextMessage(msg);
}
});
} public void setTopicJmsTemplate(JmsTemplate topicJmsTemplate) {
this.topicJmsTemplate = topicJmsTemplate;
} }
④发送消息测试方法,在上面Jms_test.java 中添加
/**
* 订阅队列 topic_name
*/
@Autowired
@Qualifier("topicDestination")
private Destination topic;
/**
* 订阅消息发布者
*/
@Autowired
private Jms_topic_send topicProvider;
/**
* 发布订阅消息,发布后自动在jms_topic_jie中接收
*/
@RequestMapping(value="/sendDy",method=RequestMethod.GET)
public ModelAndView sendDingYue(HttpServletRequest request, HttpServletResponse response){
for(int i=0;i<11;i++){
topicProvider.publish(topic, "订阅发布"+i);
}
return null;
}
以上配置是非持久化订阅,既发送发在接收方服务器关闭情况下发送消息,接收方启动后是无法收到的,下面是持久化订阅
替换上面xml中对应配置即可
<!-- 配置JMS模板(Topic),pubSubDomain="true"-->
<bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="topicDestination" />
<property name="pubSubDomain" value="true" /><!-- 此项关了就变成了队列模式 -->
<property name="receiveTimeout" value="10000" />
<!--设置持久化:1,非持久化;2,持久化-->
<property name="deliveryMode" value="2" />
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false -->
<property name="explicitQosEnabled" value="true" />
</bean>
<!-- 主题监听容器 (Topic) -->
<bean id="topicJmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="topicMessageListener" />
<!-- 持久化订阅 start -->
<property name="subscriptionDurable" value="true" />
<property name="pubSubDomain" value="true" />
<property name="clientId" value="clientId_001" /> <!-- id唯一 -->
<property name="durableSubscriptionName" value="clientId_001" />
<!-- 持久化订阅 end -->
</bean>
有问题(BUG)请反馈,谢谢
ActiveMQ配置文档的更多相关文章
- ActiveMQ+Zookeeper集群配置文档
Zookeeper + ActiveMQ 集群整合配置文档 一:使用ZooKeeper实现的MasterSlave实现方式 是对ActiveMQ进行高可用的一种有效的解决方案, 高可用的原理:使用Zo ...
- MYSQL服务器my.cnf配置文档详解
MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...
- 转!!Java代码规范、格式化和checkstyle检查配置文档
为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...
- Hibernate配置文档详解
Hibernate配置文档有框架总部署文档hibernate.cfg.xml 和映射类的配置文档 ***.hbm.xml hibernate.cfg.xml(文件位置直接放在src源文件夹即可) (在 ...
- Java代码规范、格式化和checkstyle检查配置文档
http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...
- Spring Hibernate4 整合配置文档
1 applicationContext.xml配置文档 <?xml version="1.0" encoding="UTF-8"?><bea ...
- Kerberos主从配置文档
Kerberos主从配置文档 1. Kerberos主从同步机制 在Master上通过以下命令同步数据: kdb5_util dump /var/kerberos/krb5kdc/slave_db ...
- python常用模块-配置文档模块(configparser)
python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...
- azkaban编译安装配置文档
azkaban编译安装配置文档 参考官方文档: http://azkaban.github.io/azkaban/docs/latest/ azkaban的配置文件说明:http://azkaban. ...
随机推荐
- Java程序员常用的Linux命令01——linux命令基础
1.显示日期的命令date 显示日期: [root@localhost ~]# date 显示年月日: [root@localhost ~]# date '+%Y%m%d' 2.显示日历指令cal 显 ...
- php不重新编译,添加模块
本文已安装mysqli模块为例 一:检查 1:首先保证php-fpm能正常启动 2:查看当前已安装的php模块是否有mysqli [root@oldboyedu ~]# /application/ph ...
- 4.pca与梯度上升法
(一)什么是pca pca,也就是主成分分析法(principal component analysis),主要是用来对数据集进行降维处理.举个最简单的例子,我要根据姓名.年龄.头发的长度.身高.体重 ...
- 基于Chromium的浏览器已上线通用“显示密码”按钮
基于Chromium的Edge在日前发布的Canary通道版本中,对用户界面进行了优化调整从而让InPrivate窗口变得更加简洁.在今天获得的版本更新中,微软继续带来了隐私相关的新内容--实现通用的 ...
- primer看完了
放假这将近十天吧,终于把C++ Primer中文版第五版大致看完了,然而第四部分(高级主题)还是没有看,感觉那是需要有足够的项目经验再看. 另外,看一遍肯定不够,忘得也会很快,我十分确认今后还会很多次 ...
- 〇三——css常规使用
我们在前面已经学习了常用的html基础,就可以画出一个最直接的‘裸体’ ,那么这么画出来的比较简陋,那怎么能让他变得更漂亮呢?这里就引出今天要讲的——css 我们先看看怎么把页面加上修饰的效果 < ...
- RDO远程连接时错误提示解决办法
RDO远程连接时提示“远程计算机需要网络级别身份验证,而您的计算机不支持该验证,请联系您的系统管理员或者技术人员来获得帮助” 1.先用个人电脑自带远程工具mstsc连接到服务器Windows Serv ...
- 生产环境跑PHP动态程序
Nginx + PHP5(FastCGI)生产环境跑PHP动态程序可超过“700次请求/秒” 我生产环境下的两台Nginx + PHP5(FastCGI)服务器,跑多个一般复杂的纯PHP动态程序, ...
- 【leetcode】1271. Hexspeak
题目如下: A decimal number can be converted to its Hexspeak representation by first converting it to an ...
- 仅1年GitHub Star数翻倍,Flink 做了什么?
Apache Flink 是公认的新一代开源大数据计算引擎,其流水线运行系统既可以执行批处理程序也可以执行流处理程序.目前,Flink 已成为 Apache 基金会和 GitHub 社区最为活跃的项目 ...