ActiveMQ初实践之【一】——入门实例
来源:互联网 发布:java身份证校验 编辑:程序博客网 时间:2024/06/16 21:23
一、准备工作
1. 下载:官方网站下载ActiveMQ
2. 安装:直接解压运行bin目录下面的activemq.bat文件
3. 启动成功后登陆:http://localhost:8161/admin/
4. 创建一个Queue,命名为FirstQueue,到这里准备工作OK
二、开始实例
首先,创建一个普通的java项目,然后导入java包(本人导入的是activemq-all-5.9.0.jar)
其次,创建消息发送者
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 Sender { // 连接工厂 private static ConnectionFactory connectionFactory; static { connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); } public static void send(Session session, MessageProducer producer) throws JMSException { for (int i = 0; i < 10; i++) { // 创建消息 TextMessage msg = session.createTextMessage("消息" + i); // 发送消息 producer.send(msg); System.out.println("发送消息:消息" + i); } } public static void main(String[] args) { // 连接 Connection connection = null; // 会话 Session session = null; // 目的地,消息要发往的地方 Destination destination; // 消息发送者 MessageProducer producer = null; try { // 获取消息连接 connection = connectionFactory.createConnection(); // 开启连接 connection.start(); // 通过连接创建会话 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建消息队列 destination = session.createQueue("FirstQueue"); // 创建发送者 producer = session.createProducer(destination); // 发送消息 send(session, producer); } catch (JMSException e) { e.printStackTrace(); } finally { try { session.commit(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }}
最后,添加消息接受者
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Consumer { private static ConnectionFactory connectionFactory; static { connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); } public static void main(String[] args) { Connection connection = null; Session session = null; Destination destination; MessageConsumer consumer = null; try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); while (true) { TextMessage msg = (TextMessage) consumer.receive(30000); if (msg != null) { System.out.println("接收到的消息:" + msg.getText()); } else { break; } } } catch (JMSException e) { e.printStackTrace(); } finally { try { consumer.close(); session.commit(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }}
运行结果
- sender
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.发送消息:消息0发送消息:消息1发送消息:消息2发送消息:消息3发送消息:消息4发送消息:消息5发送消息:消息6发送消息:消息7发送消息:消息8发送消息:消息9
- Consumer
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.接收到的消息:消息0接收到的消息:消息1接收到的消息:消息2接收到的消息:消息3接收到的消息:消息4接收到的消息:消息5接收到的消息:消息6接收到的消息:消息7接收到的消息:消息8接收到的消息:消息9
注意:接受消息还可以通过MessageListener接收
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.MessageListener;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class ConsumerListener { private static ConnectionFactory connectionFactory; static { connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); } public static void main(String[] args) { Connection connection = null; Session session = null; Destination destination; MessageConsumer consumer = null; try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener() { @Ovoutide public void onMessage(Message message) { TextMessage msg = (TextMessage) message; try { System.out.println(msg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } catch (JMSException e) { e.printStackTrace(); }finally{ try { consumer.close(); session.commit(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }}
实例托管于coding
1 0
- ActiveMQ初实践之【一】——入门实例
- ActiveMQ初实践之【二】——持久化方式
- ActiveMQ 实践之路(一) 启动你的ActiveMQ
- ActiveMQ入门实例 - ActiveMQ教程
- ActiveMQ入门实例 - ActiveMQ教程
- Java中间件JMS(一)之ActiveMQ入门
- Java中间件JMS(一)之ActiveMQ入门
- Webservice(实践)——入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- ActiveMQ入门实例
- 【Java并发编程】7、线程池
- ubuntu vagrant 镜像下载地址
- repo的简单介绍
- map复合key值,如何重载操作符?
- 北大 C++ 4.5 流插入和流输出运算符重载
- ActiveMQ初实践之【一】——入门实例
- dpdk-hugepage源码分析
- PHP防止SQL注入方法
- 深度学习检测方法梳理
- Spring aop事务异常回滚(rollbackFor)机制和源码分析
- HDU3338Kakuro Extension(最大流+边的流量)
- Ubuntu14.04+CUDA8.0+GTX1080+caffe配置教程
- POJ 3096 Surprising Strings 可能会
- LeetCode:9.Palindrome Number[Easy]