jms中间件整合spring

一个JMS点对点的小例子,发送方将消息发送至队列,接收者在队列中取出消息并且返回一个确认

maven导入:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.10.0</version>
        </dependency>


发送方的jms配置:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd">

	 <!-- 配置JMS连接工厂 -->  
    <bean id="connectionFactory"  
        class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://localhost:61616" />  
    </bean>  
    
    <!--配置activemq的联接池  对联接进行池化操作-->
    <bean id="PooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
    	<property name="connectionFactory" ref="connectionFactory"/>
    	<!-- 最大联接数 -->
    	<property name="maxConnections" value="100"/>
    	<!-- 最大激活数 -->
    	<!-- <property name="maximumActive" value="50"/> -->
    </bean>
    
    <!--配置spring的联接工厂,将pooledConnectionFactory,这个联接工厂将缓存session,生产者和消费者  -->
    <bean id="CachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    	<constructor-arg ref="PooledConnectionFactory"></constructor-arg>
    </bean>
    
    <!--配置spring提供的帮助类jsmTemplate  -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    	<constructor-arg ref="CachingConnectionFactory"></constructor-arg>
    </bean>
    
    <!--配置发送消息的队列  -->
    <bean id="sendDestination" class="org.apache.activemq.command.ActiveMQQueue">
    	<constructor-arg value="sendDestinationQueue"></constructor-arg>
    </bean>
    
    <!--配置响应消息的队列  -->
    <bean id="replyDestination" class="org.apache.activemq.command.ActiveMQQueue">
    	<constructor-arg value="replyDestinationQueue"></constructor-arg>
    </bean>
    
    <!--配置监听消费者回送信息的监听器  -->
    <bean id="replayListener" class="com.yc.jms.listener.ReplayListener">
    </bean>
    
    <!--配置这个监听器运行的容器  -->
    <bean id="" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    	<property name="connectionFactory" ref="CachingConnectionFactory" />
    	<property name="destination" ref="replyDestination" />
    	<property name="messageListener" ref="replayListener" />
    </bean>
</beans>  

发送方法的接口:

package com.yc.biz;

import java.util.Map;

public interface SendMessage {
    public void SendMail(Map<String,String> message);
}


实现接口:

package com.yc.biz.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Service;

import com.yc.biz.SendMessage;

@Service
public class SendMessageImpl implements SendMessage {
	private JmsTemplate jmsTemplate;

	private Queue sendDestination; // 发送队列
	private Queue replyDestination; // 接收队列

	@Resource(name = "jmsTemplate")
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	@Resource(name = "sendDestination")
	public void setSendDestination(Queue sendDestination) {
		this.sendDestination = sendDestination;
	}

	@Resource(name = "replyDestination")
	public void setReplyDestination(Queue replyDestination) {
		this.replyDestination = replyDestination;
	}

	public void SendMail(Map<String, String> message) {
		jmsTemplate.convertAndSend(sendDestination, message,
				new MessagePostProcessor() {
					public Message postProcessMessage(Message message)
							throws JMSException {
						return message;
					}
				});

	}

}

监听器,用来监听接收队列中的接收者返回的确认消息

package com.yc.jms.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ReplayListener implements MessageListener {
	//当jms中间件中的队列有消息时,这个消息中间会发送一个消息到本应用,激活本应用中的这个监听器,回调onMessage
	public void onMessage(Message message) {
		TextMessage tm=(TextMessage) message;
		try {
			//TODO:通过这一代码,表示消费成功运行
			//真实项目中,这里考虑修改数据状态(数据有可能存在数据库,或者日志)
			System.out.println(tm.getText().toString());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
测试方法:

public void testApp08() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans_jms.xml");
		SendMessage lub=(SendMessage) ac.getBean("sendMessageImpl");
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "张三");
		lub.SendMail( map );	
	}

这只是发送方 ,接收方可以写在一起,也可以不写在一起。另外使用了中间件的具体实现ActiveMQ,具体怎么使用就不在这一一讲述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值