设为首页 加入收藏

TOP

ActiveMQ结合Spring收发消息(二)
2018-10-11 16:12:59 】 浏览:506
Tags:ActiveMQ 结合 Spring 收发 消息
置消息队列监听者(Queue or Topic) --> <bean id="messageListener" class="com.service.TopicMessageListener" /> <!-- 显示注入消息监听容器,配置连接工厂,监听的目标是QueueDestination,监听器是上面定义的监听器 --> <bean id="ListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicDestination" /> <property name="messageListener" ref="messageListener" /> </bean> </beans>
配置 connectionFactory

connectionFactory 是 Spring 用于创建到 JMS 服务器链接的,Spring 提供了多种 connectionFactory。

<!-- ActiveMQ 连接工厂 -->
<amq:connectionFactory id="amqConnectionFactory"
                       brokerURL="tcp://localhost:61616"
                       userName="admin"
                       password="admin" />
<!-- 提高效率,配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="sessionCacheSize" value="100" />
</bean>
配置Queue
<bean id="QueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
       <!-- 设置消息队列的名字 -->
       <constructor-arg value="Queue-zy"/>
</bean>
配置Topic
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="Topic-zy"/>
</bean>
配置JMS消息模板——jmsTemplate
<!-- 配置JMS模板,Spring提供的JMS工具类,利用它发送、接收消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="defaultDestination" ref="QueueDestination" />
    <!--<property name="defaultDestination" ref="topicDestination" />-->
    <property name="receiveTimeout" value="10000" />
    <property name="pubSubDomain" value="false" /><!-- true是topic,false是queue,默认是false -->
</bean>

最后,在 applicationContext.xml 中引入配置好的 ActiveMQ.xml

<import resource="ActiveMQ.xml" />

以上就是配置文件相关的,下面是具体的业务代码。

消息生产者服务

@Service
public class ProducerService {
    @Autowired
    private JmsTemplate jmsTemplate;
    //使用默认目的地
    public void sendMessageDefault(final String msg){
        Destination destination = jmsTemplate.getDefaultDestination();
        System.out.println("向队列: " + destination + " 成功发送一条消息");
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }
    //可指定目的地
    public void sendMessage(Destination destination,final String msg){
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }
}
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java并发之Condition的实现分析 下一篇ERROR 1044 (42000) : Access den..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目