spring jms

1. spring 配置

  

<bean id="jmsConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
          destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="failover:(tcp://localhost:61616)" />
                <property name="useAsyncSend" value="true" />
                <property name="redeliveryPolicy" ref="redeliveryPolicy" />
                <property name="prefetchPolicy" ref="prefetchPolicy" />

            </bean>
        </property>
        <property name="maxConnections" value="100" />
    </bean>

    <!--  Spring JmsTemplate config -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!--  lets wrap in a pool to avoid creating a connection per send -->
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="sessionTransacted" value="true" />
        <property name="defaultDestination" ref="bookGift"/>
        <property name="deliveryMode" value="2"/>
    </bean>

    <bean id="bookGift" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="bookGift" />
    </bean>

    <!-- 配置消息队列监听者(Queue),自己写的实现 onMessage方法 -->
    <bean id="queueMessageListener" class="cn.valenon.test.jms.JmsListener" />

    <!-- 消息监听容器(Queue) -->
    <bean id="jmsContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="destination" ref="bookGift" />
        <property name="messageListener" ref="queueMessageListener" />
    </bean>

2. 发送消息的服务:接口和实现类

 

interface MessageProducerService {

    /**
     * 向指定队列发送消息
     */
    public void sendMessage(Destination destination, final String msg)

/**
 * 向默认队列发送消息
 */
    public void sendMessageOnDefault(final BookMessage msg)

}

class MessageProducerServiceImpl implements MessageProducerService {
    @Autowired
    private JmsTemplate jmsTemplate

    /**
     * 向指定队列发送消息
     */
    @Override
    public void sendMessage(Destination destination, String msg) {
        jmsTemplate.convertAndSend(destination,msg)
    }

/**
 * 向默认队列发送消息
 */
    @Override
    public void sendMessageOnDefault(final BookMessage msg) {
        Destination destination =  jmsTemplate.getDefaultDestination();
        jmsTemplate.convertAndSend(destination,msg)
    }
}

BookMessage是一个自定义的POJO,作为消息体,需要实现Serilizable接口

class BookMessage implements Serializable {

    String name;

    int age;

    public String toString(){
        return this.name + ";" + this.age
    }
}

3. 消息监听类:

  

class JmsListener implements MessageListener {
    @Override
    void onMessage(Message message) {
        try {
            ObjectMessage tm = (ObjectMessage) message;
            BookMessage bm = tm.getObject();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
也可以直接发送String,这时,就需要将Message转换成TextMessage,而不是ObjectMessage。

阅读更多
文章标签: jms
个人分类: web技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

spring jms

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭