使用spring + ActiveMQ 总结
使用spring + ActiveMQ 总结
目录[-]
- Spring 整合JMS 基于ActiveMQ 实现消息的发送接收
- 看了网上很多文件,最后总结出了自己需要的。
- 一、下载并安装ActiveMQ
- 首先我们到apache官网上下载activeMQ(http://activemq.apache.org/download.html),进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。
- 二、Spring中加入ActiveMQ的配置
- 首先将相关的jar拷贝到项目的lib文件下
- 配置之前先看一下相关目录以便于理解
- 下面开始配置
- PhoneNoticeInfo
- QueueMessageProducer
- NotifyMessageConverter
- QueueMessageListener
Spring 整合JMS 基于ActiveMQ 实现消息的发送接收
看了网上很多文件,最后总结出了自己需要的。
一、下载并安装ActiveMQ
首先我们到apache官网上下载activeMQ(http://activemq.apache.org/download.html),进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。
二、Spring中加入ActiveMQ的配置
首先将相关的jar拷贝到项目的lib文件下
配置之前先看一下相关目录以便于理解
下面开始配置
<!-- ActiveMQ 连接工厂 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="connectinFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- <property name="brokerURL" value="tcp://192.168.1.79:61616" /> -->
<property name="brokerURL" value="${mqUrl}" />
</bean>
<!-- Spring Caching连接工厂 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="connectinFactory"></property>
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="10"></property>
</bean>
<!-- 配置消息发送目的地方式 -->
<!-- Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 -->
<bean id="notifyQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="q.notify"></constructor-arg>
</bean>
<!-- 目的地:Topic主题 :放入一个消息,所有订阅者都会收到 -->
<!--这个是主题目的地,一对多的-->
<bean id="notifyTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="t.notify"></constructor-arg>
</bean>
<!-- Spring JMS Template 配置JMS模版 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="cachingConnectionFactory" />
</bean>
<!-- 使用Spring JmsTemplate 的消息生产者 -->
<bean id="queueMessageProducer" class="com.common.jms.QueueMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate"></property>
<property name="notifyQueue" ref="notifyQueue"></property>
<property name="messageConverter" ref="messageConverter"></property>
</bean>
<bean id="topicMessageProducer" class="com.common.jms.TopicMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate"></property>
<property name="notifyTopic" ref="notifyTopic"></property>
<property name="messageConverter" ref="messageConverter"></property>
</bean>
<!-- 消息消费者 一般使用spring的MDP异步接收Queue模式 -->
<!-- 消息监听容器 -->
<bean id="queueContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectinFactory"></property>
<property name="destination" ref="notifyQueue"></property>
<property name="messageListener" ref="queueMessageListener"></property>
</bean>
<!-- 消息监听容器 -->
<bean id="topicContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectinFactory"></property>
<property name="destination" ref="notifyTopic"></property>
<property name="messageListener" ref="topicMessageListener"></property>
<!-- 发布订阅模式 -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 异步接收消息处理类 -->
<bean id="queueMessageListener" class="com.common.jms.QueueMessageListener">
<property name="messageConverter" ref="messageConverter"></property>
</bean>
<bean id="topicMessageListener" class="com.common.jms.TopicMessageListener">
<property name="messageConverter" ref="messageConverter"></property>
</bean>
<bean id="messageConverter" class="com.common.jms.NotifyMessageConverter">
</bean>
下面展示一下Sender
public class Sender {
private static ServletContext servletContext;
private static WebApplicationContext ctx;
/**
* 发送点对点信息
* @param noticeInfo
*/
public static void setQueueSender(){
servletContext = ServletActionContext.getServletContext();
ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
QueueMessageProducer notifyMessageProducer = ((QueueMessageProducer) ctx.getBean("queueMessageProducer"));
PhoneNoticeInfo noticeInfo = new PhoneNoticeInfo();
(下面先展示PhoneNoticeInfo 然后是 QueueMessageProducer )
noticeInfo.setNoticeContent("Hello Word");
noticeInfo.setNoticeTitle("hello Word");
noticeInfo.setReceiver("hello");
noticeInfo.setReceiverPhone("1111111");
notifyMessageProducer.sendQueue(noticeInfo);
}
public static ServletContext getServletContext() {
return servletContext;
}
public static void setServletContext(ServletContext servletContext) {
Sender.servletContext = servletContext;
}
public static WebApplicationContext getCtx() {
return ctx;
}
public static void setCtx(WebApplicationContext ctx) {
Sender.ctx = ctx;
}
}
PhoneNoticeInfo
public class PhoneNoticeInfo implements Serializable {
/** 消息标题 */
public String noticeTitle;
/** 消息内容 */
public String noticeContent;
/** 接收者 */
public String receiver;
/** 接收手机号 */
public String receiverPhone;
public String getNoticeTitle() {
return noticeTitle;
}
public void setNoticeTitle(String noticeTitle) {
this.noticeTitle = noticeTitle;
}
public String getNoticeContent() {
return noticeContent;
}
public void setNoticeContent(String noticeContent) {
this.noticeContent = noticeContent;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getReceiverPhone() {
return receiverPhone;
}
public void setReceiverPhone(String receiverPhone) {
this.receiverPhone = receiverPhone;
}
}
QueueMessageProducer
/**
* 消息生产者服务类
*/
public class QueueMessageProducer {
private JmsTemplate jmsTemplate;
private Destination notifyQueue;
private NotifyMessageConverter messageConverter;
public void sendQueue(PhoneNoticeInfo noticeInfo){
sendMessage(noticeInfo);
}
private void sendMessage(PhoneNoticeInfo noticeInfo) {
// TODO Auto-generated method stub
jmsTemplate.setMessageConverter(messageConverter);
jmsTemplate.setPubSubDomain(false);
jmsTemplate.convertAndSend(notifyQueue,noticeInfo);
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Destination getNotifyQueue() {
return notifyQueue;
}
public void setNotifyQueue(Destination notifyQueue) {
this.notifyQueue = notifyQueue;
}
public NotifyMessageConverter getMessageConverter() {
return messageConverter;
}
public void setMessageConverter(NotifyMessageConverter messageConverter) {
this.messageConverter = messageConverter;
}
}
NotifyMessageConverter
/**
* 消息转换
*/
public class NotifyMessageConverter implements MessageConverter {
private static Logger logger = LoggerFactory.getLogger(NotifyMessageConverter.class);
@Override
/**
* 转换接收到的消息为NoticeInfo对象
*/
public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
// TODO Auto-generated method stub
if (logger.isDebugEnabled()) {
logger.debug("Receive JMS message :"+message);
}
if (message instanceof ObjectMessage) {
ObjectMessage oMsg = (ObjectMessage)message;
if (oMsg instanceof ActiveMQObjectMessage) {
ActiveMQObjectMessage aMsg = (ActiveMQObjectMessage)oMsg;
try {
PhoneNoticeInfo noticeInfo = (PhoneNoticeInfo)aMsg.getObject();
return noticeInfo;
} catch (Exception e) {
// TODO: handle exception
logger.error("Message:${} is not a instance of NoticeInfo."+message.toString());
throw new JMSException("Message:"+message.toString()+"is not a instance of NoticeInfo."+message.toString());
}
}else{
logger.error("Message:${} is not a instance of ActiveMQObjectMessage."+message.toString());
throw new JMSException("Message:"+message.toString()+"is not a instance of ActiveMQObjectMessage."+message.toString());
}
}else {
logger.error("Message:${} is not a instance of ObjectMessage."+message.toString());
throw new JMSException("Message:"+message.toString()+"is not a instance of ObjectMessage."+message.toString());
}
}
@Override
/**
* 转换NoticeInfo对象到消息
*/
public Message toMessage(Object obj, Session session) throws JMSException,
MessageConversionException {
// TODO Auto-generated method stub
if (logger.isDebugEnabled()) {
logger.debug("Convert Notify object to JMS message:${}"+obj.toString());
}
if (obj instanceof PhoneNoticeInfo) {
ActiveMQObjectMessage msg = (ActiveMQObjectMessage)session.createObjectMessage();
msg.setObject((PhoneNoticeInfo)obj);
return msg;
}else {
logger.debug("Convert Notify object to JMS message:${}"+obj.toString());
}
return null;
}
}
QueueMessageListener
public class QueueMessageListener implements MessageListener {
private static Logger logger = LoggerFactory.getLogger(QueueMessageListener.class);
private NotifyMessageConverter messageConverter;
/**
* 接收消息
*/
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
ObjectMessage objectMessage = (ObjectMessage)message;
PhoneNoticeInfo noticeInfo = (PhoneNoticeInfo)messageConverter.fromMessage(objectMessage);
System.out.println("queue收到消息"+noticeInfo.getNoticeContent());
System.out.println("model:"+objectMessage.getJMSDeliveryMode());
System.out.println("destination:"+objectMessage.getJMSDestination());
System.out.println("type:"+objectMessage.getJMSType());
System.out.println("messageId:"+objectMessage.getJMSMessageID());
System.out.println("time:"+objectMessage.getJMSTimestamp());
System.out.println("expiredTime:"+objectMessage.getJMSExpiration());
System.out.println("priority:"+objectMessage.getJMSPriority());
} catch (Exception e) {
// TODO: handle exception
logger.error("处理信息时发生异常",e);
}
}
public NotifyMessageConverter getMessageConverter() {
return messageConverter;
}
public void setMessageConverter(NotifyMessageConverter messageConverter) {
this.messageConverter = messageConverter;
}
}
使用spring + ActiveMQ 总结的更多相关文章
- spring +ActiveMQ 实战 topic selecter指定接收
spring +ActiveMQ 实战 topic selecter指定接收 queue:点对点模式,一个消息只能由一个消费者接受 topic:一对多,发布/订阅模式,需要消费者都在线(可能会导致信息 ...
- spring+activemq中多个consumer同时处理消息时遇到的性能问题
最近在做数据对接的工作,用到了activemq,我需要从activemq中接收消息并处理,但是我处理数据的步骤稍微复杂,渐渐的消息队列中堆的数据越来越多,就想到了我这边多开几个线程来处理消息. 可是会 ...
- Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听
对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...
- spring activemq 整合
创建maven项目 项目目录结构为 首先配置相关maven依赖 <!-- 版本管理 --> <properties> <springframework>4.1.8. ...
- spring+activemq配置文件内容及实现原理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 基于Maven,Spring+ActiveMQ实现,贴近实际
本文只实现了Topic,queue改点配置就行了 一.pom依赖 Spring的太长了,具体可以看下面源码里面 <dependency> <groupId>org.apache ...
- spring+activemq实战之配置监听多队列实现不同队列消息消费
摘选:https://my.oschina.net/u/3613230/blog/1457227 摘要: 最近在项目开发中,需要用到activemq,用的时候,发现在同一个项目中point-to-po ...
- Spring ActiveMQ Caused By: javax.jms.IllegalStateException: Connection closed
根据 http://www.cnblogs.com/yshyee/p/7448808.html 进行JMS操作时,发送跟监听放到不同的项目中进行时,出现以下异常信息: org.springframew ...
- Spring ActiveMQ 整合(三): 确认机制ACK(收到消息后,应该有一个回应也就是确认答复)
https://blog.csdn.net/dly1580854879/article/details/68490197
随机推荐
- Android开发牛刀小试之“AA算钱软件”开发(一)
事实上想去做android开发已经有非常长一段时间了,可是因为还在上课,加上老板那边的项目接连不断.也一直都没有机会抽出身来做.可是,楼主当然也不会闲着,首先我了解到android开发须要java学习 ...
- Windows 开发之VC++垃圾清理程序软件
概述 本程序软件的主要实现垃圾文件清理的功能,即对指定的文件格式的临时文件或垃圾文件进行遍历.扫描.显示.删除清理等功能.在程序界面设计方面,对默认对话框重新自定义绘制,主要包括标题栏的重绘.对话框边 ...
- jquery 常用api 小结2
*一)jQuery常用方法API实战 (1)DOM简述与分类 A)DOM是一种标准,它独立于平台,语言,浏览器. B)如果项目中,你完全按照DOM标准写代码,你就能在各大主流的浏览器中操作标准控件. ...
- Hive面试题——累计求和
需求: 有如下访客访问次数统计表 t_access_times 访客 月份 访问次数 A 2015-01 5 A 2015-01 15 B 2015-01 5 A 2015-01 8 B 2015-0 ...
- Redis总结(七)Redis运维常用命令(转载)
redis 服务器端命令 redis 127.0.0.1:6380> time ,显示服务器时间 , 时间戳(秒), 微秒数 1) "1375270361" 2) &quo ...
- spi flash偶尔出现写入错误的情况
spi flash W25Q128会偶尔出现写入错误的情况,会发现读出的值和写入的值不一致,需加入2次读出比较判断. W25QXX_Read(&temp_date_count,0x000000 ...
- bash的输出多行和vim的全部选择
使用cat命令加输出符>来在bash脚本里面输出多行文本是最直观的做法. cat >out.file <<EOF start a line ... ... a line aga ...
- 使用eclipse进行web开发的3个lib文件夹
1.右击project>Build Path>Configure Build Path(一般是在你的项目文件夹中手动创建一个lib文件夹,里面设置若干子文件夹存放不同的jar包,然后通过C ...
- JAVA第一个窗体小程序
import java.awt.*;public class Day1015_Frame{ public static void main(String[] args) { ...
- Makefile 11——支持头文件目录指定
现在,是时候在对应目录放入对应文件了: /× foo.h */ #ifndef __FOO_H #define __FOO_H void foo(void) #endif/*__FOO_H*/ /* ...