分享

SPRING与JMS结合的实例

 dashan_tfsp 2014-05-10
package jms.activemq.myexample.spring;
 
import java.util.Date;
 
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
 
import org.springframework.jms.core.MessageCreator;
 
public class MyMessageCreator implements MessageCreator {
 
    /**
     * 消息序号
     */
    private int msgNo;
 
    public MyMessageCreator(int no) {
        this.msgNo = no;
    }
 
    @Override
    public Message createMessage(Session session) throws JMSException {
        TextMessage textMsg = session.createTextMessage();
        textMsg.setText(new Date() + "第" + this.msgNo + "条消息发出");
 
        return textMsg;
    }
 
}
package jms.activemq.myexample.spring;
 
import javax.jms.*;
 
/**
 * Text消息监听
 *
 * @author xiaochuanyu
 *
 */
public class TextListener implements MessageListener {
 
    /**
     * Casts the message to a TextMessage and displays its text.
     *
     * @param message
     *            the incoming message
     */
    public void onMessage(Message message) {
        TextMessage msg = null;
 
        try {
            if (message instanceof TextMessage) {
                msg = (TextMessage) message;
                System.out.println("Reading message: " + msg.getText());
            } else {
                System.out.println("Message of wrong type: "
                        + message.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():" + t.getMessage());
        }
    }
}
package jms.activemq.myexample.spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringJmsTestMain {
 
    public static void main(String[] args) throws InterruptedException {
 
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "SpringJms/SpringJms.xml" });
 
        SpringPublisher publisher = (SpringPublisher) context
                .getBean("springPublisher");
        publisher.start();
    }
}
package jms.activemq.myexample.spring;
 
import javax.jms.Destination;
 
import org.springframework.jms.core.JmsTemplate;
 
public class SpringPublisher {
    /**
     * Jms模板
     */
    private JmsTemplate template;
 
    /**
     * Topic
     */
    private Destination topic;
 
    public JmsTemplate getTemplate() {
        return template;
    }
 
    public void setTemplate(JmsTemplate template) {
        this.template = template;
    }
 
    public Destination getTopic() {
        return topic;
    }
 
    public void setTopic(Destination topic) {
        this.topic = topic;
    }
 
    /**
     * Start
     *
     * @throws InterruptedException
     */
    public void start() throws InterruptedException {
 
        int messageCount = 10;
 
        while ((--messageCount) > 0) {
            sendMessage(messageCount);
            Thread.sleep(1000);
        }
    }
 
    /**
     * 消息发送
     */
    protected void sendMessage(int msgNO) {
 
        this.template.send(this.topic, new MyMessageCreator(msgNO));
    }
}
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www./schema/beans"
xmlns:xsi
="http://www./2001/XMLSchema-instance"
xsi:schemaLocation
="http://www./schema/beans http://www./schema/beans/spring-beans-2.0.xsd">



<!-- jms 连接工厂 -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>


<!-- jms 连接池 -->

<!--
<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
</bean>
-->

<!-- jms Topic -->
<bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic"
autowire
="constructor">
<constructor-arg value="STOCKS.JAVA" />
</bean>


<!-- 消息监听器 -->
<bean id="myTextListener" class="jms.activemq.myexample.spring.TextListener">
</bean>


<!-- jms Consumer -->
<bean id="javaConsumer"
class
="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="myTopic" />
<property name="messageListener" ref="myTextListener" />
</bean>

<!-- jms 模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
</bean>


<!-- 消息发布器 -->
<bean id="springPublisher" class="jms.activemq.myexample.spring.SpringPublisher">
<property name="template">
<ref local="jmsTemplate" />
</property>
<property name="topic">
<ref local="myTopic" />
</property>
</bean>
</beans>
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多