消息中间件 ActiveMQ的简单使用
2017-12-16 20:36
597 查看
一、AactiveMQ的下载和安装
1. 下载ActiveMQ
地址:http://activemq.apache.org/activemq-5152-release.html
我这里下载的是window的版本
2. 下载后,解压
里面有win32位和win64两种文件夹,找到你电脑上对应的win版本,我这里用的win64
右击activemq.bat,并且以管理员身份运行
启动成功后,会打印http的地址
打开这个网址http://127.0.0.1:8186
二、代码的使用
1. 创建工程
创建一个Maven工程,
2. 创建生产者
3. 创建消费者
三、主题模式下的消息
1. 消费者
2. 创建生产者
1. 下载ActiveMQ
地址:http://activemq.apache.org/activemq-5152-release.html
我这里下载的是window的版本
2. 下载后,解压
里面有win32位和win64两种文件夹,找到你电脑上对应的win版本,我这里用的win64
右击activemq.bat,并且以管理员身份运行
启动成功后,会打印http的地址
打开这个网址http://127.0.0.1:8186
二、代码的使用
1. 创建工程
创建一个Maven工程,
2. 创建生产者
public class AppProducer { private static final String url = "tcp://192.168.2.121:61616"; private static final String queueName="queue-test"; public static void main(String[] args){ //1. 创建ConnectionFactory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); try { //2. 创建Connection Connection connection = connectionFactory.createConnection(); //3. 启动连接 connection.start(); //4. 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标 Destination destination = session.createQueue(queueName); //6. 创建一个目标 MessageProducer producer = session.createProducer(destination); for(int i=0; i<100; i++){ //7. 创建消息 TextMessage textMessage = session.createTextMessage("test" + i); //8. 发布消息 producer.send(textMessage); System.out.println("发送消息" + textMessage.getText()); } //9.关闭连接 connection.close(); } catch (JMSException e) { e.printStackTrace(); } }
3. 创建消费者
public class AppConsumer { private static final String url = "tcp://192.168.2.121:61616"; private static final String queueName="queue-test"; public static void main(String[] args) throws JMSException{ //1. 创建ConnectionFactory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); //2. 创建Connection Connection connection = connectionFactory.createConnection(); //3. 启动连接 connection.start(); //4. 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标 Destination destination = session.createQueue(queueName); //6. 创建一个消费者 MessageConsumer consumer = session.createConsumer(destination); //7. 创建一个监听器 consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; try { System.out.println("接收消息" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } }
三、主题模式下的消息
1. 消费者
public class AppConsumer { private static final String url = "tcp://192.168.2.121:61616"; private static final String topicName="topic-test"; public static void main(String[] args) throws JMSException{ //1. 创建ConnectionFactory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); //2. 创建Connection Connection connection = connectionFactory.createConnection(); //3. 启动连接 connection.start(); //4. 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标 Destination destination = session.createTopic(topicName); //6. 创建一个消费者 MessageConsumer consumer = session.createConsumer(destination); //7. 创建一个监听器 consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; try { System.out.println("接收消息" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }); } }
2. 创建生产者
public class AppProducer { private static final String url = "tcp://192.168.2.121:61616"; private static final String topicName="topic-test"; public static void main(String[] args){ //1. 创建ConnectionFactory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); try { //2. 创建Connection Connection connection = connectionFactory.createConnection(); //3. 启动连接 connection.start(); //4. 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标 Destination destination = session.createTopic(topicName); //6. 创建一个目标 MessageProducer producer = session.createProducer(destination); for(int i=0; i<100; i++){ //7. 创建消息 TextMessage textMessage = session.createTextMessage("test" + i); //8. 发布消息 producer.send(textMessage); System.out.println("发送消息" + textMessage.getText()); } //9.关闭连接 connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }
相关文章推荐
- spring整合消息中间件ActiveMQ简单使用
- java消息队列ActiveMQ的简单使用
- JMS学习篇《一》ActiveMQ消息中间件的简单介绍与用法-概念篇
- 淘宝消息中间件RocketMQ的安装和简单使用
- 以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ
- Apache ActiveMQ消息中间件的基本使用
- spring-jms(activemq实现)使用queue发送消息简单例子
- [中间件] 消息处理利器 ActiveMQ 的介绍 & Stomp 协议的使用
- 【Java消息中间件】Java消息中间件( 第4章 使用activemq - spring jms演示 )
- [ActiveMQ]消息中间件在Java中的使用
- [置顶] spring boot 使用activeMQ实现消息队列简单应用
- Apache ActiveMQ消息中间件的基本使用
- 消息中间件activemq的使用场景介绍(结合springboot的示例)
- 消息中间件--ActiveMQ的使用与部署
- Apache ActiveMQ消息中间件的基本使用
- Apache ActiveMQ消息中间件的基本使用
- 【Java消息中间件】Java消息中间件( 第4章 使用activemq - 安装activemq )
- JMS Apache ActiveMQ(消息中间件)使用攻略
- Java消息中间件学习笔记四 -- ActiveMQ的使用,【队列模式】
- JMS消息中间件原理及ActiveMQ使用方法