ActiveMQ消息队列
来源:互联网 发布:linux的保存命令 编辑:程序博客网 时间:2024/06/11 05:16
1、 官网:http://activemq.apache.org/
2、 安装成功访问地址:http://localhost:8161用户名和密码都是admin
3、 ActiveMQ使用的是标准生产者和消费者模型,有两种数据结构Queue、Topic
1)Queue队列,生产者生产了一个消息,只能由一个消费者进行消费
2)Topic话题,生产者生产了一个消息,可以由多个消费者进行消费
4、 使用 Java 程序操作 ActiveMQ
结合spring完成ActiveMQ编程
导包:
spring-context.jar(4.1.7)
spring-text.jar(4.1.7)
activemq-all.jar(5.14.0)
spring-jms.jar(4.1.7)
配置文件生产者applicationContext.xml
<!-- 扫描包 --> <context:component-scan base-package="XXXXX" /> <!-- ActiveMQ 连接工厂 --> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码--> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- 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--></beans>
配置消费者监听器applicationContext.xml
<!-- 扫描包 --> <context:component-scan base-package="XXXXX" /> <!-- ActiveMQ 连接工厂 --> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码--> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- 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> <!-- 消息消费者 start--> <!-- 定义Queue监听器 --> <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <!-- 默认注册bean名称,应该是类名首字母小写 --> <jms:listener destination="spring_queue" ref="queueConsumer1"/> <jms:listener destination="spring_queue" ref="queueConsumer2"/> </jms:listener-container> <!-- 定义Topic监听器 --> <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="spring_topic" ref="topicConsumer1"/> <jms:listener destination="spring_topic" ref="topicConsumer2"/> </jms:listener-container>
代码实现(queue生产者)
package cn.itcast.activemq.producer.queue;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;@Servicepublic class QueueSender { // 注入jmsTemplate @Autowired @Qualifier("jmsQueueTemplate") private JmsTemplate jmsTemplate; public void send(String queueName, final String message) { jmsTemplate.send(queueName, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); }}
代码实现(topic生产者)
package cn.itcast.activemq.producer.topic;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;@Servicepublic class TopicSender { // 注入jmsTemplate @Autowired @Qualifier("jmsTopicTemplate") private JmsTemplate jmsTemplate; public void send(String topicName, final String message) { jmsTemplate.send(topicName, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); }}
代码实现(消费者)
package cn.itcast.activemq.consumer.queue;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;import org.springframework.stereotype.Service;@Servicepublic class QueueConsumer1 implements MessageListener { public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out .println("消费者QueueConsumer1获取信息:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } }}
阅读全文
0 0
- ActiveMQ实现消息队列
- ActiveMQ 消息队列
- 队列消息ActiveMq
- 深入浅出 消息队列 ActiveMQ
- 消息队列中间件ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ .
- 消息队列ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 消息队列------ActiveMQ
- 消息队列 ActiveMQ
- 消息队列 ActiveMQ
- 【DP】BAJ-Bytecomputer
- 张亦鸣: eBPF 简史
- 低照度图像修复方法总结
- 深入JVM内核—原理与优化之gc参数学习
- 汉诺塔1
- ActiveMQ消息队列
- java基础第四讲JavaScript详解
- 汉诺塔2
- vue-router传参数(get和post方式)
- SQLite jdbc 单例
- IDEA错误:Cannot start compilation: the output path is not specified for module "Test". (转)
- 汉诺塔3
- Python学习之Numpy
- 深入JVM内核—原理与优化之六类加载器