关闭

spring jms

标签: jms
291人阅读 评论(0) 收藏 举报
分类:

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。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12899次
    • 积分:215
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论