JMS(java消息服务)整合Spring项目案例
转载自云栖社区
摘要: Sprng-jms消息服务小项目 所需的包: spring的基础包 spring-jms-xx包 spring-message–xx包 commons-collection-xx包 commons-pool2-xx包 aop切面的包: spring-aop,spring-aspect...
Sprng-jms消息服务小项目
所需的包:
spring的基础包
spring-jms-xx包
spring-message–xx包
commons-collection-xx包
commons-pool2-xx包
aop切面的包: spring-aop,spring-aspect,aopalliance,aspectjrt.jar,aspectjweaver.jar
配置:
1.配置ConnectionFactory
2.配置jmsTemplate;
3.配置Destination
4.配置listener
5.配置container
话不多说,直接上代码
前提是你已经开启了activemq服务
看一下项目架构
临听器有两种实现方案: 一种是采用原生的jms的MessageListener
另一种是采用spring的方案:SessionAwareMessageListener
前一种只能消费消息,不能发送回送消息
后一种可以再接到消息后,给生产者回送消息,它自己即是生产者也是消费者
所有所需的连接工、目的地等都是通过Spring注入的,具体看后面的配置文件
发送消息的接口:
package com.tg.service;
public interface PersonService {
public void sendMessage(String message);
}
生产者一:
package com.tg.service; import javax.annotation.Resource;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; import org.springframework.context.annotation.Scope;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service("personServiceImpl")
@Scope("prototype")
public class PersonServiceImpl implements PersonService { private Destination destination;
private JmsTemplate jsmTemplate;
private Destination replyQueueDestination; @Override
public void sendMessage(final String message) {
System.out.println("生产者发送消息"+ message); //回调
jsmTemplate.send(destination, new MessageCreator(){
@Override
public Message createMessage(Session session) throws JMSException {
//创建一个文本消息
Message msg = session.createTextMessage(message);
//指定为非持久化方式
msg.setJMSDeliveryMode( DeliveryMode.NON_PERSISTENT );
return msg;
}
});
} @Resource(name="queueDestination")
public void setDestination(Destination destination) {
this.destination = destination;
} @Resource(name="jmsTemplate")
public void setJsmTemplate(JmsTemplate jsmTemplate) {
this.jsmTemplate = jsmTemplate;
} @Resource(name = "replyQueueDestination")
public void setReplyDestination(Destination replyDestination) {
this.replyQueueDestination = replyDestination;
} }
消费者一:
package com.tg.service; import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; //临听器有两种实现方案: 一种是采用原生的jms的MessageListener
// 另一种是采用spring的方案:SessionAwareMessageListener //注意: 这里的MessageListener接口是 jms的接口
public class ConsumerMessageListener1 implements MessageListener { @Override
public void onMessage(Message message) {
if( message instanceof TextMessage){
TextMessage text=(TextMessage) message;
System.out.println("接收到的消息是一个文本消息:"+ text); //这种方式无法回复 所以采用第二种
}
} }
生产者二:
package com.tg.service; import javax.annotation.Resource;
import javax.jms.DeliveryMode;
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.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service("personServiceImpl2")
public class PersonServiceImpl2 implements PersonService {
private Destination destination; //用于存发送信息的队列
private JmsTemplate jsmTemplate; //jms操作模板
private Destination replyQueueDestination; //用于存回复信息的队列,
@Override
public void sendMessage(final String message) {
System.out.println("生产者2->发送消息" + message);
// 回调
jsmTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
Message msg = session.createTextMessage(message);
// 设置回复的信息的目的地.
msg.setJMSReplyTo(replyQueueDestination);
// 设置发送的信息类型 为非持久化信息
msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); //创建一个消费者,用于接收对方回复的信息 注意,这个消费者临听 replyDestination
MessageConsumer comsumer2 = session.createConsumer(replyQueueDestination);
comsumer2.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
System.out.println("接收到的回复信息:" + ((TextMessage) m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
return msg;
}
});
}
@Resource(name = "replyQueueDestination")
public void setReplyDestination(Destination replyDestination) {
this.replyQueueDestination = replyDestination;
}
@Resource(name = "sendQueueDestination")
public void setDestination(Destination destination) {
this.destination = destination;
} @Resource(name = "jmsTemplate")
public void setJsmTemplate(JmsTemplate jsmTemplate) {
this.jsmTemplate = jsmTemplate;
} }
消费者二:
package com.tg.service; import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component; /**
* SessionAwareMessageListener:是由spring提供,它可以在回调方法中传入session,以此回送信息到生产者
* @author Administrator
*
*/
@Component("consumerMessageListener2")
public class ConsumerMessageListener2 implements SessionAwareMessageListener<TextMessage> { private Destination destination; @Override
public void onMessage(TextMessage message, Session session) throws JMSException {
System.out.println("接收到的消息是一个文本消息:"+ message.getText());
//通过session 创建 producer对象,再回送信息
//从message中取出信息回送的目的地,以便创建生产者.
MessageProducer producer=session.createProducer( message.getJMSReplyTo() );
//创建一条消息
Message textMessage=session.createTextMessage( "生产者发过来的信息已经处理完毕,game over..." );
//调用发送
producer.send(textMessage);
} @Resource(name="sendQueueDestination")
public void setDestination(Destination destination) {
this.destination = destination;
}
}
Spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 启用注解解析器 -->
<context:annotation-config />
<!-- 因为采用了混合解析方式( 有一部分配置在xml中,有一部分在java类中,所以要让spring的注解解析器去扫描包 -->
<context:component-scan base-package="com.*" />
<!-- 启用aspectj的注解方式的代理 -->
<aop:aspectj-autoproxy /> <!-- 创建一个真正的基于 jsm提供者的联接工厂 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://169.254.173.100:61616" />
</bean> <!-- ActiveMQ联接池的方案 -->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory" />
<property name="maxConnections" value="100" />
</bean> <!-- 创建spring联接工厂 -->
<bean id="singleConnectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactory" />
</bean> <!-- <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
<property name="targetConnectionFactory" ref="pooledConnectionFactory" />
</bean> --> <!-- 配置jmsTemplate -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="singleConnectionFactory" />
</bean> <!-- 配置目的地: 这有两种:一种是 Queue对应是 P2P模式,另一种是 Topic 对应的是 发布/订阅模式, -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>myqueue</value>
</constructor-arg>
</bean> <!-- <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg> <value>mytopic</value> </constructor-arg> </bean> --> <!-- 配置临听器 -->
<bean id="consumerMessageListener1" class="com.tg.service.ConsumerMessageListener1">
</bean> <!-- 配置临听器运行时所在的容器 让Listener运行在这个容器,这样
只要有消息过来,就会回调
-->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="singleConnectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="consumerMessageListener1" />
</bean> <!-- 以下是第二种方案 --> <!-- 以下用于存放生产者发送的信息 -->
<bean id="sendQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>sendQueue1</value>
</constructor-arg>
</bean>
<!-- 以下用于存放消费者回复的信息 -->
<bean id="replyQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>replyQueue1</value>
</constructor-arg>
</bean>
<!-- 配置 sessionAware的临听器
<bean id="consumerMessageListener2" class="com.yc.jms5.ConsumerMessageListener2" >
<property name="destination" ref="sendQueueDestination" />
<property name="replyDestination" ref="replyQueueDestination" />
</bean>
-->
<!-- 配置 consumerMessageListener2的容器 -->
<bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" >
<property name="connectionFactory" ref="singleConnectionFactory" />
<property name="destination" ref="sendQueueDestination" />
<property name="messageListener" ref="consumerMessageListener2" />
</bean>
</beans>
第一种的测试类:
package com.tg.test; import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext; import com.tg.service.PersonService; //测试生产者发送了一个消息,消费者接收 //整合spring http://haohaoxuexi.iteye.com/blog/1893038
public class Test1 { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService ps=(PersonService) ac.getBean("personServiceImpl");
for( int i=0;i<10;i++){
Thread t=new Thread(new User(ps,"汤"+i));
t.start();
}
}
} class User implements Runnable{
private PersonService ps;
private String name;
public User(PersonService ps, String name) {
super();
this.ps = ps;
this.name = name;
} @Override
public void run() {
for( int i=0;i<10;i++){
ps.sendMessage(name+"向你问好");
} } }
运行结果 :
第二种方案测试类:
package com.tg.test; import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext; import com.tg.service.PersonService; //测试生产者发送了一个消息,消费者接收后,再回复一个信息到生产者,生产者接收到后,显示这个回复的信息
public class Test2 { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService ps=(PersonService) ac.getBean("personServiceImpl2");
for( int i=0;i<10;i++){
Thread t=new Thread(new User(ps,": 汤1"+i));
t.start();
}
} }
运行结果 :
JMS(java消息服务)整合Spring项目案例的更多相关文章
- JMS(Java消息服务)入门教程
什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...
- JMS(Java消息服务)入门教程(一)
什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...
- JMS Java消息服务(Java Message Service)
JMS 在一些场景下RPC的同步方式可能不太适合业务逻辑的处理,并且这种方式在某些场景下会导致业务的紧耦合. 基于异步交互模型的JMS解决了RPC产生的紧耦合问题,它提供了一个可以通过网络访问的抽象消 ...
- Java消息服务初步学习(基于Spring In Action的整理)
几个名词 Java消息服务(Java Message Service)是一个Java标准,定义了使用消息代理的通用API. 消息代理(message broker):类似于邮局的作用,确保消息被投递到 ...
- JMS(Java消息服务)与消息队列ActiveMQ基本使用(一)
最近的项目中用到了mq,之前自己一直在码农一样的照葫芦画瓢.最近几天研究了下,把自己所有看下来的文档和了解总结一下. 一. 认识JMS 1.概述 对于JMS,百度百科,是这样介绍的:JMS即Java消 ...
- ActiveMQ学习总结(5)——Java消息服务JMS详解
JMS: Java消息服务(Java Message Service) JMS是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互. JMS的编程过程很简单,概 ...
- java消息服务学习之JMS概念
JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...
- 【转载】JAVA消息服务JMS规范及原理详解
转载:https://www.cnblogs.com/molao-doing/articles/6557305.html 作者: moyun- 一.简介 JMS即Java消息服务(Java Messa ...
- JAVA消息服务JMS规范及原理详解
JAVA消息服务JMS规范及原理详解 一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应 ...
随机推荐
- placeholder 属性的支持
placeholder 是HTML5的. 在IE6-8中不能使用.可以使用 JQ 来设置. 给input一个value, JQ判断得到焦点时 value="": 移开焦点时,若 ...
- oledb快速导入Excel案例
DataTable dtImportExcel = null; string pathFile = Server.MapPath("~/ErrorCatory.xlsx"); // ...
- win8安装tfs2010提示未启用iis6.0未启用兼容模式需要静态内容组件
笔者的电脑由于安装TFS2010就提示这个错误,当时网上也没有很好地办法,重装iis组件也不行.如果你同样没有找到更好的办法,建议安装tfs2012,但在vs2010使用tfs2012是无法创建团队项 ...
- C# dll 事件执行 js 回调函数
C# dll 事件执行 js 回调函数 前言: 由于js 远程请求 XMLHttpRequest() 不支持多线程,所以用C# 写了个dll 多线程远程抓住供js调用. 最初代码为: C#代 ...
- 自定义HttpFilter模块完善
自定义HttpFilter模块完善 背景 在12月由于要针对项目做用户操作日志,但不想在每个方法里去增加代码,写入用户日志.因为这样具体的方法违背职责单一的原则,若后期日志内容格式发生变更,或其他 ...
- Django数据库迁移
如果你用过Django的数据库就会发现一个比较令人纠结的地方:数据库更改. 我们知道添加或者删除一个models.Model 需要在数据库里相应的操作,这需要我们进入数据库命令行手动添加或删除,因为s ...
- [转]loadView的用法,loadView创建基本界面,DidLoad读入数据
loadview: // 有没有nib 只要是复写了loadview loadview都会被执行 有nib文件的话加载的是nib文件的view 没有的话会按照loadview里的代码加载 ...
- Moq让单元测试变得更简单
[ASP.Net MVC3 ]使用Moq让单元测试变得更简单 前几天调查完了unity.现在给我的任务是让我调查Moq. 以下是自己找了资料,总结并实践的内容.如果有表述和理解错误的地方.恳请指正. ...
- Bootstrap3.0(进度条、媒体对象、列表组、面板)
Bootstrap3.0学习第十六轮(进度条.媒体对象.列表组.面板) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehy ...
- 企业架构研究总结(39)——TOGAF架构能力框架之架构委员会和架构合规性
3. 架构委员会 正如前面所说,一个用来对架构治理策略的实现进行监督的跨组织的架构委员会是架构治理策略成功的主要要素之一.架构委员会应该能够代表所有主要干系人的需求,并且通常还需要对整个架构的审查及维 ...