spring + jms

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();
    }
}
复制代码
  
  
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/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 >
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值