ActiveMQ消息队列学习
来源:互联网 发布:淘宝商品优化 编辑:程序博客网 时间:2024/06/05 04:22
ActiveMQ消息队列是apache下面的开源的消息中间件,使用方便扩展性好。下面我就我学习mq跟大家分享一下:
- ActiveMQ环境的配置
下载ActiveMQ:http://activemq.apache.org/
解压缩apache-activemq-5.9.1-bin.zip,然后双击apache-activemq-5.9.1\bin\win64\activemq.bat运行ActiveMQ程序。 - 启动ActiveMQ以后,登陆:http://localhost:8161/admin/ 用户名和密码都是admin
打开eclipse创建一个Java项目
1. 创建一个消息生产者
package com.java.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class MessageProvider { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; private static final String BROKERURL=ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { ConnectionFactory connectionFactory;//创建连接工厂 Connection connection = null;//创建连接 Session session;//创建session 消息的线程 Destination destination;//创建消息发送目的地 MessageProducer messageProducer;//消息生产者 try { connectionFactory = new ActiveMQConnectionFactory(MessageProvider.USERNAME, MessageProvider.PASSWORD, MessageProvider.BROKERURL); connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue1"); messageProducer = session.createProducer(destination); sendMessage(session, messageProducer); session.commit(); } catch (JMSException e) { e.printStackTrace(); }finally{ try { if(connection != null){ connection.close(); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void sendMessage(Session session , MessageProducer messageProducer)throws JMSException{ for(int i = 0 ;i<10;i++){ TextMessage message = session.createTextMessage("activeMQ发送的消息:"+i); messageProducer.send(message); System.out.println("发送消息:activeMQ发送的消息:"+i); } }}运行的效果如下:
log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
发送消息:activeMQ发送的消息:0
发送消息:activeMQ发送的消息:1
发送消息:activeMQ发送的消息:2
发送消息:activeMQ发送的消息:3
发送消息:activeMQ发送的消息:4
发送消息:activeMQ发送的消息:5
发送消息:activeMQ发送的消息:6
发送消息:activeMQ发送的消息:7
发送消息:activeMQ发送的消息:8
发送消息:activeMQ发送的消息:9
2. 创建一个消息消费者
package com.java.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class JMSConsumer { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; private static final String BROKERURL=ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { ConnectionFactory connectionFactory;//创建连接工厂 Connection connection = null;//创建连接 Session session;//创建session 消息的线程 Destination destination;//创建消息发送目的地 MessageConsumer messageConsumer;//消息生产者 try { connectionFactory = new ActiveMQConnectionFactory( JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKERURL); connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue1"); messageConsumer = session.createConsumer(destination); while(true){ TextMessage textMessage = (TextMessage)messageConsumer.receive(100000); if(textMessage != null){ System.out.println("接收到的消息为:"+textMessage.getText()); }else{ break; } } } catch (JMSException e) { e.printStackTrace(); }finally{ try { if(connection != null){ connection.close(); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
执行的效果是:
log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.接收到的消息为:activeMQ发送的消息:0接收到的消息为:activeMQ发送的消息:1接收到的消息为:activeMQ发送的消息:2接收到的消息为:activeMQ发送的消息:3接收到的消息为:activeMQ发送的消息:4接收到的消息为:activeMQ发送的消息:5接收到的消息为:activeMQ发送的消息:6接收到的消息为:activeMQ发送的消息:7接收到的消息为:activeMQ发送的消息:8接收到的消息为:activeMQ发送的消息:9
0 0
- ActiveMQ消息队列学习
- Java activemq消息队列入门学习
- ActiveMQ消息队列的使用学习笔记
- 消息队列-ActiveMQ学习笔记(二)-点对点消息实现
- 消息队列-ActiveMQ学习笔记(二)-点对点消息实现
- ActiveMQ实现消息队列
- ActiveMQ 消息队列
- 队列消息ActiveMq
- 深入浅出 消息队列 ActiveMQ
- 消息队列中间件ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ .
- 消息队列ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- redis集群搭建及分析
- TCP和UDP协议的对比
- 寻找Coder——去哪儿2015研发工程师笔试题
- 《GeekBand》c++面向对象高级编程04
- JavaSE复习总结(四)
- ActiveMQ消息队列学习
- js框架 - 占一个位置
- Ubuntu 16.04添加32位库
- 微信相关功能,跳转链接
- QM UML状态机建模实例之Blinky for cortex-m0
- 安装ubuntu后没有win7启动项,直接启动ubuntu
- js框架 - 占一个位置
- 安卓流式布局(可换行的标签)
- ajaxfileupload文件上传带参数,json序列化到后台!