Spring+JMS+ActiveMQ基础案例

本文介绍了Spring 4.3.16.RELEASE与ActiveMQ 5.15.3结合使用的基础案例,包括activemq.properties配置、spring-activemq.xml文件设置。详细讲述了点对点模式(queue)和发布/订阅模式(topic)的应用。在点对点模式中,生产者QueueProducer使用JmsTemplate发送消息,消费者QueueConsumer1和QueueConsumer2实现MessageListener接口接收消息。而在Pub/Sub模式下,TopicProducer使用jmsTopicTemplate发布消息,多个TopicConsumer如TopicConsumer1和TopicConsumer2能接收到同一消息。
摘要由CSDN通过智能技术生成

版本说明

Spring 4.3.16.RELEASE
activemq-all 5.15.3

activemq.properties 文件

activemq.properties 文件中配置ActiveMQ服务器的url, userName, password等. 还可以指定queue和topic的队列名.

# ActiveMQ Config
com.tao.activemq.brokerURL = tcp\://127.0.0.1\:61616
com.tao.activemq.userName = admin
com.tao.activemq.password = admin
com.tao.activemq.pool.maxConnection = 10

# queueName
com.tao.activemq.queueName = activemq_queue_01
#topicName
com.tao.activemq.topicName = activemq_topic_01

spring-activemq.xml 配置文件说明

spring-activemq.xml 配置文件中, 我们需要配置 ConnectionFactory, JmsTemplate, Destination, Consumer的监听器.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jms
    http://www.springframework.org/schema/jms/spring-jms.xsd">

    <!-- 1: 读入配置文件 -->
    <context:property-placeholder location="classpath*:properties/activemq.properties" ignore-unresolvable="true"/>


    <!-- 2: 扫描service包下的所有使用注解的类型 -->
    <context:component-scan base-package="com.tao.smp.mq"/>


    <!-- 3: 配置ConnectionFactory -->
    <!-- 真正的由JMS服务器厂商提供的ConnectionFactory -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <!-- ActiveMQ Config -->
        <property name="brokerURL" value="${com.tao.activemq.brokerURL}"/>
        <property name="userName" value="${com.tao.activemq.userName}"/>
        <property name="password" value="${com.tao.activemq.password}"/>
    </bean>

    <!--
         ActiveMQ为我们提供了一个PooledConnectionFactory, 通过往里面注入一个ActiveMQConnectionFactory
         可以用来将Connection、Session和MessageProducer池化, 这样可以大大减少资源的消耗.
     -->
    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="connectionFactory" ref="targetConnectionFactory"/>
        <property name="maxConnections" value="${com.tao.activemq.pool.maxConnection}"/>
    </bean>

    <!-- Spring用于管理connectionFactory的ConnectionFactory -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 注入真实的可以产生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="pooledConnectionFactory"/>
    </bean>


    <!-- 4: Spring JmsTemplate 的消息生产者Producer -->
    <!-- 生产者bean -->
    <bean id="queueProducer" class="com.tao.smp.mq.producer.queue.QueueProducer"</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值