Spring+weblogic接收JMS消息
上篇文章学习了在spring里如何发送JMS消息到weblogic的Queue里。
这篇是学习spring里如何处理消息的。
spring提供了几种不同的api来处理消息:
1. javax.jms.MessageListener
2. org.springframework.jms.listener.SessionAwareMessageListener
3. 任意定义java类
前两种方式都必须要实现他们的接口,而第三种方式则是我们任意定义的java类。
首先第一种:ExampleListener.java
package com.test.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ExampleListener implements MessageListener
{
public void onMessage(Message message)
{
if (message instanceof TextMessage)
{
try
{
System.out.println("ExampleListener receive messages: "+((TextMessage) message).getText());
} catch (JMSException ex)
{
throw new RuntimeException(ex);
}
} else
{
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
}
对应的配置:
<bean id="exampleMessageListener" />
<bean id="jmsContainer"
ref="jmsConnectionFactory" />
<property name="destination" ref="jmsDestination" /> -->
<property name="messageListener" ref="exampleMessageListener" />
</bean>
第二种:ExampleSessionAwareMessageListener.java
package com.test.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.listener.SessionAwareMessageListener;
public class ExampleSessionAwareMessageListener implements SessionAwareMessageListener
{
public void onMessage(Message message, Session session) throws JMSException
{
if (message instanceof TextMessage)
{
try
{
System.out.println("\nsession is : "+session+" ExampleSessionAwareMessageListener receive messages: "+((TextMessage) message).getText());
} catch (JMSException ex)
{
throw new RuntimeException(ex);
}
} else
{
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
}
对应的配置:
<bean id="exampleSessionAwareMessageListener" />
<bean id="jmsContainer"
ref="jmsConnectionFactory" />
<property name="destination" ref="jmsDestination" />
<property name="messageListener" ref="exampleSessionAwareMessageListener" />
</bean>
第三种:ExampleMessageListenerAdapter.java
package com.test.jms;
import javax.jms.JMSException;
import javax.jms.TextMessage;
public class ExampleMessageListenerAdapter
{
public void receive(TextMessage message) throws JMSException
{
String msg = message.getText();
System.out.println("ExampleMessageListenerAdapter receive messages: "+msg);
}
}
对应的配置;
<bean id="exampleMessageListenerAdapter"
/>
</constructor-arg>
<property name="defaultListenerMethod" value="receive" />
<property name="messageConverter">
<null />
</property>
</bean>
<bean id="jmsContainer"
ref="jmsConnectionFactory" />
<property name="destination" ref="jmsDestination" />
<property name="messageListener" ref="exampleMessageListenerAdapter" />
</bean>
这样当spring容器启动完成后便会监听消息Queue,当有消息发送到Queue里,jmsContainer会将消息交给所配置的监听来处理。
需要说明的是,第三种方式只能接收,处理 javax.jms.TextMessage 类型的消息。
spring2.x 里对配置文件做了很大改变,我们可以使用新的配置。
applicationContext-jms.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:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">
<bean id="transactionManager" />
</constructor-arg>
<property name="defaultListenerMethod" value="receive" />
<property name="messageConverter">
<null />
</property>
</bean>
<jms:listener-container connection-factory="jmsConnectionFactory"
destination-type="queue"
concurrency="10"
transaction-manager="transactionManager"
>
<jms:listener destination="JMSServer-0/SystemModule-0!Queue-0" ref="exampleMessageListenerAdapter" />
</jms:listener-container>
</beans>
需要说明的是,listener 属性的destination指的是你所配置的queue的名称。必须加上JMS Server的名称。