用Spring发送和接受JMS消息的一个小例子

Spring提供的JmsTemplate对原生的JMS API进行了一层薄薄的封装,使用起来非常的方便。

我使用的JMS消息代理插件是Apache的ActiveMQ,建议安装最新版本,因为我之前安装老版本,各种不兼容,各种bug,最新版的activemq-all-5.9.1.jar包里面已经有了slf4j.impl包,之前就是被这个坑了...把这个jar包加到lib目录下面,就不会有各种ClassNotFound异常和类冲突的bug了。

下载ActiveMQ解压之后运行bin下面的activemq.bat就可以运行了。你可以访问http://127.0.0.1:8161/看看服务是否正常开启

默认账户密码是admin/admin

下面这个例子是一个邮局系统,包括前后台两个子系统,当前台收到一封邮件的时候,它将这封邮件传递给后台,同时发送一个JMS消息给后台子系统,提醒新的邮件。

JMS有两种消息通信模型:

1)、point-to-point

2)、pub/sub

这里我们当然是用的第一种模型.

首先我们建立一个Mail类:

package com.apress.springrecipes.post;

public class Mail {
	private String mailId;
	private String country;
	private double weight;

	public Mail() {
	}

	public Mail(String mailId, String country, double weight) {
		this.mailId = mailId;
		this.country = country;
		this.weight = weight;
	}

	public String getMailId() {
		return mailId;
	}

	public void setMailId(String mailId) {
		this.mailId = mailId;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}
}

一个前台子系统接口:

package com.apress.springrecipes.post;

public interface FrontDesk {
	public void sendMail(Mail mail);
}

和它的实现类:

package com.apress.springrecipes.post;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class FrontDeskImpl2 implements FrontDesk {
	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	private Destination destination;
	@Override
	public void sendMail(final Mail mail) {
		jmsTemplate.send(destination,new MessageCreator() {

			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage message = session.createMapMessage();
				message.setString("mailId", mail.getMailId());
				message.setString("country", mail.getCountry());
				message.setDouble("weight", mail.getWeight());
				return message;
			}
		});
	}
}

一个后台子系统的接口:

package com.apress.springrecipes.post;

public interface BackOffice {
	public Mail receiveMail();
}

和它的实现类:

package com.apress.springrecipes.post;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;

public class BackOfficeImpl2 implements BackOffice {
	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	private Destination destination;

	@Override
	public Mail receiveMail() {
		MapMessage message = (MapMessage) jmsTemplate.receive(destination);
		try {
			if (message == null) {
				return null;
			}
			Mail mail = new Mail();
			mail.setMailId(message.getString("mailId"));
			mail.setCountry(message.getString("country"));
			mail.setWeight(message.getDouble("weight"));
			return mail;
		} catch (JMSException e) {
			throw JmsUtils.convertJmsAccessException(e);
		}
	}
}

前台子系统的xml配置文件bean-front.xml:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config/>	

	<bean id="frontDesk"
		class="com.apress.springrecipes.post.FrontDeskImpl2" />

	<bean id="connectionFactory"
		class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616"/>
	</bean>

	<bean id="destination"
		class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="mail.queue" />
	</bean>

	<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"/>
	</bean>

</beans>

后台子系统的xml配置文件bean-back.xml:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 	<context:annotation-config />	

	<bean id="backOffice"
		class="com.apress.springrecipes.post.BackOfficeImpl2" />

	<bean id="connectionFactory"
		class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>

	<bean id="destination"
		class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="mail.queue" />
	</bean>

	<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="receiveTimeout" value="10000" />
	</bean>

</beans>

两个测试类,一个是发消息,一个是接受消息。

package com.apress.springrecipes.post;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FrontDeskMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans-front.xml");
		FrontDesk frontDesk = (FrontDesk) context.getBean("frontDesk");
		frontDesk.sendMail(new Mail("1234","US",1.5));
	}
}
package com.apress.springrecipes.post;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BackOfficeMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans-back.xml");
		BackOffice backOffice = (BackOffice) context.getBean("backOffice");
		Mail mail = backOffice.receiveMail();
		System.out.println("Mail #" + mail.getMailId() + " received");
	}
}

运行FrontDeskMain之后,发送消息你可以简单的通过访问http://127.0.0.1:8161/admin/queueGraph.jsp和http://127.0.0.1:8161/admin/queues.jsp观察发生的情况。再运行BackOfficeMain观察这两个页面的变化。

时间: 2024-08-25 11:38:32

用Spring发送和接受JMS消息的一个小例子的相关文章

Spring.Net在ASP.NET Mvc里使用的一个小例子

就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里搜索spring.web.mvc,它会自动下载SpringNet的引用包. 安装完成之后你的项目会多三个引用,项目目录../packages文件夹下面也会多出这三个文件夹里面是SpringNet的文件. 2.写代码例子 很简单的例子.定义一个接口,一个对于接口的实现类. namespace MvcA

spring的一个小例子(二)--解析前面的小例子

接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://ww

CMD接受输入参数(定时关机小例子)

为了方便操作,我大程序员经常会将一些重复的工作写成一个批处理. 但是,在批处理中如何接受输入参数呢?且看如下例子: @echo off set /p time=请输入关机时间: shutdown -s -f -t %time% 这是一个定时关机的批处理,其中接受一个时间参数,用户可以输入在多久之后关机. 下面逐行来看: @echo off ,DOS会依次执行文件中的命令,并将执行的命令自动输出在DOS中,如果我们不想将执行的命令显示出来,就可以使用echo off来关闭自动输出. 但是,echo

仿spring的ioc实现之注解注入的小例子

首先,我们先写出一个自定义的注解 <span style="font-size:14px;">package com.test.aonntion; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; // 生

ActiveMQ学习笔记(六)——JMS消息类型

1.前言 ActiveMQ学习笔记(四)--通过ActiveMQ收发消息http://my.oschina.net/xiaoxishan/blog/380446 和ActiveMQ学习笔记(五)--使用Spring JMS收发消息http://my.oschina.net/xiaoxishan/blog/381209   中,发送和接受的消息类型都是TextMessage,即文本消息(如下面的代码所示).显然消息类型只有文本类型是不能满足要求的. //发送文本消息  session.create

JMS消息服务器(一)——基础知识

1.概述 异构集成(heterogeneous integration)是消息传送机制在其中起关键作用的一个领域.无论它的成因是合并.并购.业务需求,或者仅仅是技术方向上的一个变化,越来越多的公司都正面临着在企业内部.跨企业集成异构系统和应用程序的问题. 消息传送机制还具有异步处理请求的能力,它为系统体系机构师和开发者提供的解决方案,能够减轻或消除系统瓶颈,并提高最终用户的生产率和系统的整体可伸缩性.由于消息传送机制能够实现组件之间的高度去耦,因此,使用这种机制的系统还具有高度的体系结构灵活性和

【EJB四】——JMS消息服务之P2P和Pub/Sub

JMS:java消息服务,JMS客户端可以通过JMS服务进行异步消息传输.可以支持两种模型:P2P和Pub/Sub P2P 点对点消息传输模式,这种模式主要包括发送者,消息队列和接收者 特点: 1.每个消息只有一个消费者,一旦被接收(消费),此消息就不存在于消息队列中了. 2.发送者和接收者在时间上没有依赖性(当消息发送后,无论接收者有没有在接收,都不会影响消息进入消息队列) 3.接收者在成功接收消息之后,需要向队列应答成功. Pub/Sub 发布/订阅的模式.主要包括:发布者,主题,订阅者三部

spring AOP + 自定义注解实现权限控制小例子

今天看了一下黑马程序员的视频,上面讲到一个使用spring AOP + 自定义注解的方式来实现权限控制的一个小例子,个人觉得还是可以借鉴,整理出来与大家分享. 需求:service层有一些方法,这些方法需要不同的权限才能访问. 实现方案:自定义一个PrivilegeInfo的注解,使用这个注解为service层中的方法进行权限配置,在aop中根据PrivilegeInfo注解的值,判断用户是否拥有访问目标方法的权限,有则访问目标方法,没有则给出提示. 关键技术:自定义注解及注解解析,spring

Spring 基础,用小例子来理解它

1.什么是Spring? 我们都知道框架,我学的就是struts2,mybatis和spring 三大框架 struts2处于表现层和业务处理 mybatis处理业务处理和数据访问 而Spring是无处不在的. 以上是我对Spring的理解 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了