消息队列Active mq在Maven项目中的简单使用
来源:互联网 发布:光缆资源软件 编辑:程序博客网 时间:2024/05/22 06:21
1,ActiveMQ安装
下载地址:http://activemq.apache.org/download.html
我下载的是windows版本,5.15.0
下载完成解压缩就可以了,然后双击文件apache-activemq-5.14.3\bin\win64\activemq.bat即可
然后访问地址:http://127.0.0.1:8161/admin/ 用户名/密码:admin/admin
可以看到此时消息队列的内容为空:
2,编写程序,生产者和消费者
Maven项目中引入jar
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.15.0</version></dependency>
编写生产者:这里创建了名为“hello”的消息队列,并向消息队列发送10条消息。)
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by CY on 2017/8/31. */public class JMSProducer { //默认连接用户名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //默认连接密码 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; //默认连接地址 private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { //连接工厂 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL); try { //连接 Connection connection = connectionFactory.createConnection(); //启动连接 connection.start(); //创建session Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //消息目的地 Destination destination = session.createQueue("hello"); //消息生产者 MessageProducer producer = session.createProducer(destination); //设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //发送消息 for (int i = 0; i < 10; i++) { //创建一条文本消息 TextMessage message = session.createTextMessage("ActiveMQ啦啦啦:这是第 " + i + " 条消息"); //生产者发送消息 producer.send(message); } session.commit(); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } }}编写消费者:(这里指向名为“hello”的消息队列,并向消息队列一直取消息出来。)
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * 消息消费者 */public class JMSConsumer { //默认连接用户名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //默认连接密码 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; //默认连接地址 private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { //连接工厂 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL); try { //连接 Connection connection = connectionFactory.createConnection(); //启动连接 connection.start(); //创建session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //消息目的地 Destination destination = session.createQueue("hello"); //消息消费者 MessageConsumer consumer = session.createConsumer(destination); while (true) { TextMessage message = (TextMessage) consumer.receive(); if (message != null) { System.out.println("接收到消息: " + message.getText()); } else { break; } } session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } }}运行代码进行测试:
首先运行生产者:(可以看到有10条消息进入队列)
然后运行消费者:(可以看到10条消息出队列)
现在我们进行第二项测试:
同时启动两个消费者,然后再启动生产者。我们会发现两个消费者都消费了5条信息,这说明一条信息不能被两个消费者同时消费。
现在我们想要两个消费者都能消费10条信息,只需修改一行代码即可:
Destination destination = session.createTopic("hello");效果如下图所示:
参考:http://www.linuxidc.com/Linux/2017-03/142053.htm
阅读全文
0 0
- 消息队列Active mq在Maven项目中的简单使用
- 消息队列 -- active mq 安装测试
- Active MQ(一)--消息队列基本概念介绍
- 消息队列在项目中的使用总结
- 消息队列在项目中的使用总结
- MQ消息队列使用场景
- java 通过jmx获取active mq队列消息
- Active mq使用的简单步骤
- 消息队列在项目中的使用(memcacheq)
- Windows消息队列MQ的使用
- 消息队列MQ的使用场景
- MQ消息队列系列(1)为什么使用MQ
- MQ消息队列系列(2)什么时候使用MQ
- MQ消息队列系列(1)为什么使用MQ
- MQ消息队列系列(2)什么时候使用MQ
- MQ消息队列系列(2)什么时候使用MQ
- MQ消息队列系列(1)为什么使用MQ
- MQ消息队列系列(2)什么时候使用MQ
- Jquery获取一组Radio的选中值
- HTTP长连接和短连接以及推送技术原理
- DrawerLayout的使用 侧滑加载Listview,首页使用Fragment和处理点击触摸冲突、穿透事件和监听菜单滑动状态的改变的详细介绍
- redis、memcache、mongoDB 对比
- jQuery打印插件jqprint
- 消息队列Active mq在Maven项目中的简单使用
- C++ vector 功能简单介绍
- JSON转换工具(Gson)
- iOS UILabel设置行间距和字间距并计算高度
- 在原生android应用中引入React Native
- JVM的内存区域划分以及工作原理
- python基础
- sed 查询文本, 输出带有行号
- spring@Controller注解接受参数的方式