`
a3mao
  • 浏览: 559032 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring+weblogic接收JMS消息

    博客分类:
  • Work
阅读更多
上篇文章学习了在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" class="com.test.jms.ExampleListener" />
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" 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" class="com.test.jms.ExampleSessionAwareMessageListener" />
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" 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"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.test.jms.ExampleMessageListenerAdapter" />
</constructor-arg>
<property name="defaultListenerMethod" value="receive" />
<property name="messageConverter">
<null />
</property>
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" 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" class="org.springframework.transaction.jta.JtaTransactionManager"/>

<bean id="exampleMessageListenerAdapter"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="com.test.jms.ExampleMessageListenerAdapter" />
</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的名称。
1
0
分享到:
评论
1 楼 anqinghaozi 2016-11-02  
请问 你这weblogic jms 如何部署到tomcat上去?我想在页面上点击按钮发送消息。

相关推荐

    weblogic中使用JMS发送和接受消息

    weblogic中使用JMS发送和接受消息(Queue and TOPIC)

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    6.4.2 JMS消息头和消息属性 253 6.4.3 重用消息对象 254 6.4.4 JMS传递方式和有效时间 255 6.4.5 设置消息的优先级 256 6.4.6 消息的确认方式 256 6.4.7 消息选择器 257 6.4.8 消息的临时目的261 6.5 使用队列浏览器...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6 .3. 任务规划 2.6.4. 对Java 5...

    Spring 2.0 开发参考手册

    19.2. 使用Spring JMS 19.2.1. JmsTemplate 19.2.2. 连接工厂 19.2.3. (消息)目的地管理 19.2.4. 消息侦听容器 19.2.5. 事务管理 19.3. 发送一条消息 19.3.1. 使用消息转换器 19.3.2. SessionCallback 和...

    Spring中文帮助文档

    2.4.4. 异步的JMS 2.4.5. JDBC 2.5. Web层 2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持...

    Spring API

    2.4.4. 异步的JMS 2.4.5. JDBC 2.5. Web层 2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...

    springboot参考指南

    接收消息 x. 31. 发送邮件 xi. 32. 使用JTA处理分布式事务 i. 32.1. 使用一个Atomikos事务管理器 ii. 32.2. 使用一个Bitronix事务管理器 iii. 32.3. 使用一个J2EE管理的事务管理器 iv. 32.4. 混合XA和non-XA的JMS...

    JAVA上百实例源码以及开源项目

     在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...

    JAVA上百实例源码以及开源项目源代码

     在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...

Global site tag (gtag.js) - Google Analytics