ActiveMq-PTP入门例子
来源:互联网 发布:钛度科技淘宝 编辑:程序博客网 时间:2024/05/11 12:39
1.下载并运行Active Mq
从http://activemq.apache.org/download.html下载自己需要的版本,这里以5.10.0版本为例。
下载解压完成之后,进入bin目录,根据自己的系统选择需要执行的activemq.bat(这里使用的是windows 64位),所以使用win64文件夹下的activemq.bat。
启动之后可以登录http://localhost:8161,如果出现欢迎页则说明开启成功。然后通过默认的用户名admin,密码admin就可以查看ActiveMq中的消息等信息。
2.添加依赖
根据下载时候页面上的说明,添加对应的maven依赖。每个版本都不一样。
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.10.0</version></dependency>
3.发送端
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;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 final int SEND_NUMBER = 5; public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // MessageProducer:消息发送者 MessageProducer producer; // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值FirstQueue是一个服务器的queue,可以在ActiveMq的console配置查看 destination = session.createQueue("FirstQueue"); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // 设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 构造消息,此处写死,项目就是参数,或者方法获取 sendMessage(session, producer); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } } public static void sendMessage(Session session, MessageProducer producer) throws Exception { for (int i = 1; i <= SEND_NUMBER; i++) { TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i); // 发送消息到目的地方 System.out.println("发送消息:" + "ActiveMq 发送的消息" + i); producer.send(message); } }}
4.接收端
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver { public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // 消费者,消息接收者 MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值FirstQueue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); while (true) { //设置接收者接收消息的时间,为了便于测试,这里谁定为100s TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("收到消息" + message.getText()); } else { break; } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }}
5.测试
(1)先启动activemq.bat,根据http://localhost:8161确定开启
(2)启动接收端
(3)启动发送端
结果为:
发送端
发送消息:ActiveMq 发送的消息1发送消息:ActiveMq 发送的消息2发送消息:ActiveMq 发送的消息3发送消息:ActiveMq 发送的消息4发送消息:ActiveMq 发送的消息5接收端:
收到消息ActiveMq 发送的消息1收到消息ActiveMq 发送的消息2收到消息ActiveMq 发送的消息3收到消息ActiveMq 发送的消息4收到消息ActiveMq 发送的消息5
注:可以在eclipse中开启多个控制台,所以在eclipse中测试时完全没有问题的
0 0
- ActiveMq-PTP入门例子
- ActiveMQ demos-PTP domain
- ActiveMQ 入门例子
- ActiveMQ点对点模式(PTP)
- ActiveMq-Publisher/Subscribe入门例子
- ActiveMQ入门学习小例子
- activeMQ之点对点(PTP),监听器模式
- activeMQ之点对点(PTP),主动接受消息
- ActiveMQ两种模式PTP和PUB/SUB
- ActiveMQ例子
- ActiveMQ例子
- ActiveMQ : ActiveMQ入门介绍
- ActiveMQ入门
- ActiveMQ入门
- activemq入门
- ActiveMQ 入门
- activeMQ 入门
- ActiveMQ入门
- FlatSearchGraph API
- GrammarPoint API
- 树莓派GPIO介绍
- GrammarState API
- 二叉树遍历,递归,非递归
- ActiveMq-PTP入门例子
- JAVA 泛型 - Class<T>
- LPC1788--ADC连续中断多通道转换程序学习分析
- 冒泡排序 Java
- GState API
- 日志为什么被低估?
- mongo与Java之Mapreduce
- 浏览器网址请求Tomcat服务器UML图全过程
- LNK4098,默认库冲突的解决