转载自云栖社区

摘要: 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项目案例的更多相关文章

  1. JMS(Java消息服务)入门教程

    什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...

  2. JMS(Java消息服务)入门教程(一)

    什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...

  3. JMS Java消息服务(Java Message Service)

    JMS 在一些场景下RPC的同步方式可能不太适合业务逻辑的处理,并且这种方式在某些场景下会导致业务的紧耦合. 基于异步交互模型的JMS解决了RPC产生的紧耦合问题,它提供了一个可以通过网络访问的抽象消 ...

  4. Java消息服务初步学习(基于Spring In Action的整理)

    几个名词 Java消息服务(Java Message Service)是一个Java标准,定义了使用消息代理的通用API. 消息代理(message broker):类似于邮局的作用,确保消息被投递到 ...

  5. JMS(Java消息服务)与消息队列ActiveMQ基本使用(一)

    最近的项目中用到了mq,之前自己一直在码农一样的照葫芦画瓢.最近几天研究了下,把自己所有看下来的文档和了解总结一下. 一. 认识JMS 1.概述 对于JMS,百度百科,是这样介绍的:JMS即Java消 ...

  6. ActiveMQ学习总结(5)——Java消息服务JMS详解

    JMS: Java消息服务(Java Message Service) JMS是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互. JMS的编程过程很简单,概 ...

  7. java消息服务学习之JMS概念

    JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...

  8. 【转载】JAVA消息服务JMS规范及原理详解

    转载:https://www.cnblogs.com/molao-doing/articles/6557305.html 作者: moyun- 一.简介 JMS即Java消息服务(Java Messa ...

  9. JAVA消息服务JMS规范及原理详解

    JAVA消息服务JMS规范及原理详解 一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应 ...

随机推荐

  1. parent和top

    function show(){ //parent是获取本窗口的父窗口   //window.parent.location.href="http://www.baidu.com" ...

  2. Effective C++(15) 在资源管理类中提供对原始资源的访问

      问题聚焦:     资源管理类是为了对抗资源泄露.     如果一些函数需要访问原始资源,资源管理类应该怎么做呢?        关于资源管理的概念总是显得那么的高大上,其实只是抽象一点. 下面用 ...

  3. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定

    使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定   原文地址:http://www.dotnetjalps.com/2013/05/Simple-da ...

  5. Linq无聊练习系列7----Insert,delete,update,attach操作练习

    /*********************Insert,delete,update,attach操作练习**********************************/            ...

  6. 【ios开发】Block编程

    1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...

  7. [转]在 Mac OS X上编译 libimobiledevice 的方法

    link: http://blog.boceto.fr/2012/05/05/libimobiledevice-for-macosx/ The objective of the day: Compil ...

  8. 记关于 Lambda 表达式的基础写法

    namespace MyLambda { public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, ...

  9. Hook SSDT中NtCreateProcessEx

    #ifdef __cplusplus extern "C" { #endif #include <ntddk.h> #ifdef __cplusplus } #endi ...

  10. MyEclipse添加ibatis DTD文件实现xml的自动提示功能

    当我们写ibatis配置文件的时候,希望有xml自动提示功能.这就需要我们添加DTD文件 SqlMapConfig.xml中开头部分有这么一句话 <!DOCTYPE sqlMapConfig P ...