您的位置:首页 > 其它

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

2017-03-25 01:46 721 查看
本教程旨在帮助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

完整项目源码下载 http://download.csdn.net/detail/qq_20094173/9793159
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: