activeMQ配置,mq使用方法,activeMQ示例,activeMQDemo

本教程旨在帮助activeMQ初学者入门,通过本示例,能完全理解activeMQ的基本概念,为分布式应用打下基础。

本示例中,使用maven管理,完美解决各种依赖问题,不需要自行配置,导入项目等待eclipse自行下载jar包后即可;

完整项目源码下载 http://download.csdn.net/detail/qq_20094173/9793159



1.首页我们需要创建一个文件spring-ActiveMQ.xml,用于加载进spring的容器当中;




内容如下:


	<!-- 引入配置 -->
 	<context:property-placeholder location="classpath:spring/ActiveMQ.properties" ignore-unresolvable="true"/>

    <!-- ActiveMQ 连接工厂 -->
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="${amq.brokerURL}" userName="${amq.userName}" password="${amq.password}"/>

    <!-- Spring Caching连接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
        <!-- 同上,同理 -->
        <!-- <constructor-arg ref="amqConnectionFactory" /> -->
        <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>

    <!-- Spring JmsTemplate 的消息生产者 start-->

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!--Spring JmsTemplate 的消息生产者 end-->


    <!-- 消息消费者 start-->

    <!-- 定义Queue监听器 -->
	<import resource="classpath:spring/ActiveMQ-queue-receiver.xml" />


    <!-- 定义Topic监听器 -->
	<import resource="classpath:spring/ActiveMQ-topic-receiver.xml" />

    <!-- 消息消费者 end -->


2.编辑QueueReceiver类 (这是服务消费者)

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.stereotype.Component;

@Component
public class QueueReceiver implements MessageListener {

	@Override
	public void onMessage(Message message) {
		try {
			System.out.println("queue: "+((TextMessage)(message)).getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

3.编写TopicReceiver类,考虑篇幅的原因,这里不再重复贴上代码了,详细操作请看源码;

4.编写QueueProducer类,(这是服务提供者)

@Component
public class QueueProducer {

	@Autowired
	@Qualifier(value="jmsQueueTemplate")
	private JmsTemplate jmsTemplate;
	
	/**
	 * 
	 * @param queueName 主题
	 * @param message 信息
	 */
	public void send(final String queueName, final String message){
		jmsTemplate.send(queueName, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});
	}
	
}

5.编写TopicProducer类,考虑篇幅的原因,这里不再重复贴上代码了,详细操作请看源码;


6. 编辑ActiveMQ-queue-receiver.xml文件

关键配置如下

	<jms:listener-container destination-type="queue"
		container-type="default" connection-factory="connectionFactory"
		acknowledge="auto">
		
		<!-- 配置业务中的监听 -->
		<jms:listener destination="test.queue" ref="queueReceiver" />
		<jms:listener destination="versatile-web" ref="userQueueReceiver" />
	</jms:listener-container>


完整项目源码下载 http://download.csdn.net/detail/qq_20094173/9793159

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值