这里还配置了发送的消息的存在时间timeToLive,目标Queue的名字defaultDestinationName,接受消息超时时间receiveTimeout
4。配置发送代码
<bean id="producer"
class="com.liangj.apmgt.jms.DefaultApmgtMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
5。接着配置监听器,这是Spring2.0新增的功能,配置如下:
<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.liangj.apmgt.jms.DefaultApmgtMessageListener" />
</constructor-arg>
<property name="defaultListenerMethod" value="onMessage" />
<property name="messageConverter" ref="messageConverter" />
</bean>
<!-- and this is the attendant message listener container -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="${jms.destinationName.cmpp}" />
<property name="messageSelector" value="${jms.messageSelector}" />
<property name="messageListener" ref="messageListener" />
</bean>
Spring配置监听器有很多种选择,在这里我选择这回种MessageListenerAdapter方法主要是因为这个方法比较灵活。实现他只要一个很普通的java类即可,和JMS以及Spring的耦合度最低。其中方法onMessage可以随便修改方法名,只要在配置文件中对应的修改就好了。
MessageListenerAdapter还有一个功能就是如果处理方法(我这里是onMessage)返回一个非空值,它将自动返回一个响应消息。这个消息会返回给JMS Reply-To属性定义的目的地(如果存在),或者是MessageListenerAdapter设置(如果配置了)的缺省目的地;如果没有定义目的地,那么将产生一个InvalidDestinationException异常(此异常将不会只被捕获而不处理,它将沿着调用堆栈上传)。
这样我们的配置就都完成了。接下来我们来实现对应的Java文件
先是接口文件发送消息接口IApmgtMessageProducer.java
public interface IApmgtMessageProducer {
public abstract void sendMessage(ApmgtMessageData messageData);
}
接受消息接口IApmgtMessageListener.java
public interface IApmgtMessageListener {
public void onMessage(ApmgtMessageData message);
}
发消息的文件DefaultApmgtMessageProducer.java
public class DefaultApmgtMessageProducer implements IApmgtMessageProducer {
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(ApmgtMessageData messageData) {
this.jmsTemplate.convertAndSend(messageData);
}
}
收消息文件DefaultApmgtMessageListener.java
public class DefaultApmgtMessageListener implements IApmgtMessageListener {
public void onMessage(ApmgtMessageData message) {
System.out.println("监听到消息:"+message);
}
}
消息转换类ApmgtMessageConverter.java
public class ApmgtMessageConverter implements MessageConverter {
private Log log = LogFactory.getLog(ApmgtMessageConverter.class);
private SimpleMessageConverter converter;
public void setConverter(SimpleMessageConverter converter) {
this.converter = converter;
}
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
if (message instanceof ObjectMessage) {
ObjectMessage o_message = (ObjectMessage)message;
MessageHeader header = new MessageHeader();
header.setId(message.getLongProperty("id"));
header.setReceiver(message.getIntProperty("receiver"));
header.setSender(message.getIntProperty("sender"));
header.setSendPerson(message.getStringProperty("sendPerson"));
header.setType(message.getIntProperty("type"));
Serializable messageContent = o_message.getObject();
ApmgtMessageData<Serializable> messageData = new ApmgtMessageData<Serializable>();
messageData.setMessageContent(messageContent);
messageData.setMessageHeader(header);
return messageData;
}
return null;
}
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
if (object instanceof ApmgtMessageData) {
ApmgtMessageData data = (ApmgtMessageData) object;
Message message = converter.toMessage(data.getMessageContent(), session);
message.setLongProperty("id", data.getMessageHeader().getId());
message.setIntProperty("receiver", data.getMessageHeader().getReceiver());
message.setIntProperty("sender", data.getMessageHeader().getSender());
message.setIntProperty("type", data.getMessageHeader().getType());
message.setStringProperty("sendPerson", data.getMessageHeader().getSendPerson());
log.info("发送消息[MessageSender]:\n" + message);
return message;
} else {
return null;
}
}
}
消息类文件 消息父类:ApmgtMessageData.java
public class ApmgtMessageData<T extends Serializable>{
protected T messageContent;
protected MessageHeader messageHeader;
public T getMessageContent() {
return this.messageContent;
}
public MessageHeader getMessageHeader() {
return this.messageHeader;
}
public void setMessageContent(T messageContent) {
this.messageContent = messageContent;
}
public void setMessageHeader(MessageHeader messageHeader) {
this.messageHeader = messageHeader;
}
}
消息属性的一个类MessageHeader.java
public class MessageHeader {
/**
* 消息ID
*/
private long id;
/**
* 消息类型
*/
private int type;
/**
* 消息发送方,发送消息的模块
*/
private int sender;
/**
* 消息接收方,接收消息的模块
*/
private int receiver;
/**
* 消息发送者,具体的用户
*/
private String sendPerson;
public MessageHeader(){
this.id = System.currentTimeMillis() ;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSendPerson() {
return sendPerson;
}
public void setSendPerson(String sendPerson) {
this.sendPerson = sendPerson;
}
public int getReceiver() {
return receiver;
}
public void setReceiver(int receiver) {
this.receiver = receiver;
}
public int getSender() {
return sender;
}
public void setSender(int sender) {
this.sender = sender;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
消息体的类ModPasswordRequest.java
public class ModPasswordRequest implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 旧密码
*/
private String oldPassword;
/**
* 新密码
*/
private String newPassword;
public String getNewPassword() {
return newPassword;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
public String getOldPassword() {
return oldPassword;
}
public void setOldPassword(String oldPassword) {
this.oldPassword = oldPassword;
}
}
消息类:ApmgtModPasswordRequest.java
public class ApmgtModPasswordRequest extends ApmgtMessageData<ModPasswordRequest> {
private static final int REQ_MODPASSWORD = 0;
private static final int INTF = 1;
private static final int APMGT = 2;
public void init(){
messageHeader = new MessageHeader();
messageContent = new ModPasswordRequest();
messageHeader.setType(REQ_MODPASSWORD);
messageHeader.setSender(INTF);
messageHeader.setReceiver(APMGT);
messageContent.setNewPassword("123456");
messageContent.setOldPassword("654321");
}
}
最后是测试类Main.java
public class Main {
public static void main(final String[] args) throws Exception {
PropertyConfigurator.configure("log4j.properties");
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "spring-jms.xml" });
// ctx.registerShutdownHook();
IApmgtMessageProducer producer = (IApmgtMessageProducer)ctx.getBean("producer");
ApmgtModPasswordRequest messageData = new ApmgtModPasswordRequest();
messageData.setMessageHeader(new MessageHeader());
messageData.setMessageContent(new ModPasswordRequest());
messageData.init();
producer.sendMessage(messageData);
}
}
还有两个配置文件,第一个spring-jms.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[url]http://www.springframework.org/schema/beans"[/url]
xmlns:xsi="[url]http://www.w3.org/2001/XMLSchema-instance"[/url]
xsi:schemaLocation="[url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans.xsd">[/url]
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>apmgt.properties</value>
</list>
</property>
</bean>
<!-- ####################################### -->
<!-- JMS Spring Beans -->
<!-- ####################################### -->
<!-- Jms ConnectionFactory -->
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${jms.brokerURL}" />
</bean>
<!-- Spring JMS SimpleConverter -->
<bean id="simpleConverter"
class="org.springframework.jms.support.converter.SimpleMessageConverter" />
<!-- JMS Queue Template -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="timeToLive" value="${jms.timeToLive}"/>
<property name="defaultDestinationName" value="${jms.destinationName.cmpp}" />
<property name="messageConverter" ref="messageConverter" />
<property name="receiveTimeout" value="${jms.receiveTimeout}" />
</bean>
<!-- Message Converter -->
<bean id="messageConverter"
class="com.liangj.apmgt.jms.ApmgtMessageConverter">
<property name="converter">
<ref local="simpleConverter" />
</property>
</bean>
<!-- Message porducer -->
<bean id="producer"
class="com.liangj.apmgt.jms.DefaultApmgtMessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.liangj.apmgt.jms.DefaultApmgtMessageListener" />
</constructor-arg>
<property name="defaultListenerMethod" value="onMessage" />
<property name="messageConverter" ref="messageConverter" />
</bean>
<!-- and this is the attendant message listener container -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="${jms.destinationName.cmpp}" />
<property name="messageSelector" value="${jms.messageSelector}" />
<property name="messageListener" ref="messageListener" />
</bean>
</beans>
apmgt.properties
#jms properties
jms.brokerURL=tcp://localhost:61616
jms.receiveTimeout=3000
jms.destinationName.cmpp=cmpp
jms.messageSelector=receiver=2
#one day is 86400000 ms. 0 is means that it lives forever.
jms.timeToLive=86400000
转:http://blog.iecn.net/blog/html/do-showone-tid-1035.html