JMS消息监听和转发实例

来源:互联网 发布:java数据交换 编辑:程序博客网 时间:2024/06/10 14:08

1. 新建web工程,在tomcat下注册queue

iJms.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\workspace\iJms\target\iJms-1.0.0" path="iJms" reloadable="true">
<Resource
        name="CONNECTON_FACTORY"
        auth="Container"
        type="org.apache.activemq.ActiveMQConnectionFactory"
        description="JMS Connection Factory"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        brokerURL="tcp://localhost:61616"
        brokerName="LocalActiveMQBroker"
        useEmbeddedBroker="false"/>

    <Resource name="RECEIVE"
        auth="Container"
        type="org.apache.activemq.command.ActiveMQQueue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        physicalName="RECEIVE"/>

    <Resource name="REPLY"
        auth="Container"
        type="org.apache.activemq.command.ActiveMQQueue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        physicalName="REPLY"/>
</Context>

2.spring配置文件

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

    <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="messageListener" ref="queryListener"/>
        <property name="destination" ref="receiveQueue"/>
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <property name="concurrentConsumers" value="1" />
    </bean>
    
    <bean id="queryListener" class="cn.edu.csu.listener.QueryListener" init-method="init" destroy-method="destroy">
        <property name="replyQueue" ref="replyQueue"/> 
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
    </bean>
    
    <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate"/>
        </property>
        <property name="jndiName">
            <value>java:comp/env/CONNECTON_FACTORY</value>
        </property>
    </bean>
    
    <bean id="receiveQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate"/>
        </property>
        <property name="jndiName">
            <value>java:comp/env/RECEIVE</value>
        </property>
    </bean>
    
    <bean id="replyQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate"/>
        </property>
        <property name="jndiName">
            <value>java:comp/env/REPLY</value>
        </property>
    </bean>
    
    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</prop>
                <prop key="java.naming.provider.url">tcp://localhost:61616</prop>
            </props>
        </property>
    </bean>

</beans>

3.监听类

package cn.edu.csu.listener;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;

public class QueryListener implements MessageListener {

private Destination replyQueue;
private ConnectionFactory connectionFactory;
private Connection conn;
private Session session;
private MessageProducer producer;

public QueryListener(){
}


public void setReplyQueue(Destination replyQueue) {
this.replyQueue = replyQueue;
}


public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}


public void init(){
try {
conn = connectionFactory.createConnection();
session = conn.createSession(false, 1);
producer = session.createProducer(replyQueue);
System.out.println("conn = " + conn + ", session = " + session + ", producer = " + producer);
} catch (JMSException e) {
e.printStackTrace();
}
}

public void destroy(){
try {
producer.close();
session.close();
conn.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void onMessage(Message msg) {
try {
System.out.println("received msg ====== " + msg);
producer.send(msg);
} catch (JMSException e) {
System.out.println("e = " + e);
e.printStackTrace();
}

}

}

4.使用方法

启动activemq,启动该工程,发送消息到RECEIVE,然后监听类会将消息转发到REPLY。

可以在onMessage方法中加入消息转发逻辑,即解析收到的消息,请求其他应用,将获取到的response解析并转发。

原创粉丝点击