Spring In Action 使用Spring发送和接收JMS消息

本文介绍了如何使用SpringJMS简化JavaEE平台中JMS消息的开发流程,包括添加依赖、配置文件设置、实现消息转换类及发送接收消息的方法。重点展示了如何解决在配置默认消息目的地时的重复代码问题,并通过JmsGatewaySupport类进一步简化代码。同时,介绍了如何使用MailMessageConverter实现消息的转换,使得发送和接收消息变得简洁高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   在Java EE 平台中,应用往往需要使用JMS进行通信。为了发送和接收JMS消息,必须执行如下任务:

     1-在一个消息代理上创建一个JMS连接工厂。

     2-创建一个JMS消息的目的地,可以是一个消息或者一个主题

     3-从连接工厂打开一个JMS连接

  4-从JMS连接中获取一个JMS会话

     5-使用消息生产这或者消息消费者发送或者接受一个JMS消息

     6-关闭JMS会话和连接

正如上面所列的那样,发送或者接受一个简单的jms消息需要许多编码。事实上这些步骤大多数都是样板式的。

Spring JMS 可以帮助我们简化JMS的开发。

注意: 首先需要下载ActiveMq ,在 bin文件夹下有activemq.bat .运行activemq

   首先在maven的POM文件中添加如下依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>spring</groupId>
<artifactId>JMS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JMS</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.4.RELEASE</spring.version>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>
</project>

 

 分别有两个测试方法:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class BackOfficeTest {
@Autowired
private BackOffice backOffice;

@Test
public void testReceiveMail() {
System.out.printf("Receive:%s%n",backOffice.receiveMail());
}

}

 

 

package spring.JMS.post;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class FrontDeskTest {
@Autowired
private FrontDesk frontDesk;

@Test
public void testSendMail() {
Mail m = new Mail();
m.setCountry("CHN");
m.setMailId("12132");
m.setWeight(0.09);
System.out.printf("Start send the mail:%s%n",m);
frontDesk.sendMail(m);
}

}

 

 以及一个测试集合

package spring.JMS.post;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses(value = { FrontDeskTest.class, BackOfficeTest.class })
public class SendAndReceiveTest {

}

 运行附件中的测试集合可以看到如下信息:
写道
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Start send the mail:Mail [mailId=12132, country=CHN, weight=0.09]
Receive:Mail [mailId=12132, country=CHN, weight=0.09]

 -----------------------------------------------------------------------------------------------------------------------------------------------

这样可以实现了一个简单的JMS消息的发送和接收,但是你在代码中会发现有这样一个问题:
写道
jmsTemplate.send(destination, new MessageCreator() {

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;

 

 发送的时候需要指定目的地,一般我们都默认是想一个目的地发送的,这样每次都写很麻烦。这个问题应该如何解决呢?

可以在配置文件中陪一个默认地址

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

 

 这样就可以向默认目的地发送消息了。

在Spring中还提供了这样一个类 JmsGatewaySupport 类,通过扩展这个类可以是我们的代码更加简单明了

 

转换消息也是一件很麻烦的事情,此时你可以实现MessageConverter接口

package spring.JMS.post;

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

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

public class MailMessageConverter implements MessageConverter {

public Object fromMessage(Message arg) throws JMSException,
MessageConversionException {
MapMessage mapMessage = (MapMessage) arg;
Mail mail = new Mail();
mail.setMailId(mapMessage.getString("mailId"));
mail.setCountry(mapMessage.getString("country"));
mail.setWeight(mapMessage.getDouble("weight"));
return mail;
}

public Message toMessage(Object object, Session session)
throws JMSException, MessageConversionException {
Mail mail = (Mail) object;
MapMessage message = session.createMapMessage();
message.setString("mailId", mail.getMailId());
message.setString("country", mail.getCountry());
message.setDouble("weight", mail.getWeight());
return message;
}

}

 

 这样发送和接收的代码就很简单了

public void sendMail(final Mail mail) {
getJmsTemplate().convertAndSend(mail);
}


public Mail receiveMail() {
return (Mail) getJmsTemplate().receiveAndConvert();

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈脩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值