activeMQ-简单应用
active 简单介绍:https://baike.baidu.com/item/ActiveMQ#1
点对点的消息模式
点对点的模式主要建立在一个队列上面,当连接一个列队的时候,发送端不需要知道接收端是否正在接收,可以直接向ActiveMQ发送消息,发送的消息,将会先进入队列中,如果有接收端在监听,则会发向接收端,如果没有接收端接收,则会保存在activemq服务器,直到接收端接收消息,点对点的消息模式可以有多个发送端,多个接收端,但是一条消息,只会被一个接收端给接收到,哪个接收端先连上ActiveMQ,则会先接收到,而后来的接收端则接收不到那条消息。
ActiveMQ官网下载地址:http://activemq.apache.org/download.html
楼主这里下载的是windows环境的

解压到自定义的目录下

启动ActiveMQ服务
进入解压文件的apache-activemq-5.15.5\bin目录 执行activemq start命令

运行命令 ,看到如下结果表示启动成功,停止服务命令:activemq stop
在浏览器中输入地址:http://localhost:8161/admin/ 默认账户:admin 密码:admin
看到如下界面,表示环境已经好了

所需要的jar

点对点消息发送端
1 package com.lm.acticemq; 2 3 import javax.jms.Connection; 4 import javax.jms.ConnectionFactory; 5 import javax.jms.DeliveryMode; 6 import javax.jms.Destination; 7 import javax.jms.MapMessage; 8 import javax.jms.MessageProducer; 9 import javax.jms.ObjectMessage; 10 import javax.jms.Session; 11 import javax.jms.TextMessage; 12 import org.apache.activemq.ActiveMQConnection; 13 import org.apache.activemq.ActiveMQConnectionFactory; 14 15 public class Sender { 16 17 private static final int SEND_NUMBER = 5; 18 19 public static void main(String[] args) { 20 // ConnectionFactory :连接工厂,JMS 用它创建连接 21 ConnectionFactory connectionFactory; 22 // Connection :JMS 客户端到JMS Provider 的连接 23 Connection connection = null; 24 // Session: 一个发送或接收消息的线程 25 Session session; 26 // Destination :消息的目的地;消息发送给谁. 27 Destination destination; 28 // MessageProducer:消息发送者 29 MessageProducer producer; 30 // TextMessage message; 31 // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar 32 connectionFactory = new ActiveMQConnectionFactory( 33 ActiveMQConnection.DEFAULT_USER, 34 ActiveMQConnection.DEFAULT_PASSWORD, 35 "tcp://localhost:61616"); 36 try { 37 // 构造从工厂得到连接对象 38 connection = connectionFactory.createConnection(); 39 // 启动 40 connection.start(); 41 // 获取操作连接 42 session = connection.createSession(Boolean.TRUE, 43 Session.AUTO_ACKNOWLEDGE); 44 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 45 destination = session.createQueue("FirstQueue"); 46 // 得到消息生成者【发送者】 47 producer = session.createProducer(destination); 48 // 设置不持久化,此处学习,实际根据项目决定 49 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 50 // 构造消息,此处写死,项目就是参数,或者方法获取 51 sendMessage(session, producer); 52 session.commit(); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } finally { 56 try { 57 if (null != connection) 58 connection.close(); 59 } catch (Throwable ignore) { 60 } 61 } 62 } 63 64 public static void sendMessage(Session session, MessageProducer producer) 65 throws Exception { 66 for (int i = 1; i <= SEND_NUMBER; i++) { 67 User user = new User("006"+i, "622", String.valueOf(i+20)); 68 ObjectMessage message = session.createObjectMessage(); 69 message.setObject(user); 70 /* TextMessage message = session 71 .createTextMessage("ActiveMq 发送的消息" + i);*/ 72 // 发送消息到目的地方 73 System.out.println("发送消息:" + "ActiveMq 发送的消息" + user.toString()); 74 producer.send(message); 75 } 76 } 77 }
运行结果如下

点对消息接收端
接收端即使在消息接收完成后,程序任然在运行。如果再有消息发送端发送消息,还是能够接收到
1 package com.lm.acticemq; 2 3 import javax.jms.Connection; 4 import javax.jms.ConnectionFactory; 5 import javax.jms.Destination; 6 import javax.jms.JMSException; 7 import javax.jms.Message; 8 import javax.jms.MessageConsumer; 9 import javax.jms.MessageListener; 10 import javax.jms.ObjectMessage; 11 import javax.jms.Session; 12 import javax.jms.TextMessage; 13 import org.apache.activemq.ActiveMQConnection; 14 import org.apache.activemq.ActiveMQConnectionFactory; 15 16 public class Receiver { 17 18 public static void main(String[] args) { 19 20 // MessageListener 21 // ConnectionFactory :连接工厂,JMS 用它创建连接 22 ConnectionFactory connectionFactory; 23 // Connection :JMS 客户端到JMS Provider 的连接 24 Connection connection = null; 25 // Session: 一个发送或接收消息的线程 26 Session session; 27 // Destination :消息的目的地;消息发送给谁. 28 Destination destination; 29 // 消费者,消息接收者 30 MessageConsumer consumer; 31 connectionFactory = new ActiveMQConnectionFactory( 32 ActiveMQConnection.DEFAULT_USER, 33 ActiveMQConnection.DEFAULT_PASSWORD, 34 "tcp://localhost:61616"); 35 try { 36 // 构造从工厂得到连接对象 37 connection = connectionFactory.createConnection(); 38 // 启动 39 connection.start(); 40 // 获取操作连接 41 session = connection.createSession(Boolean.FALSE, 42 Session.AUTO_ACKNOWLEDGE); 43 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 44 destination = session.createQueue("FirstQueue"); 45 consumer = session.createConsumer(destination); 46 /*while (true) { 47 //设置接收者接收消息的时间,为了便于测试,这里谁定为100s 48 // TextMessage message = (TextMessage) consumer.receive(100000); 49 // if (null != message) { 50 // System.out.println("收到消息" + message.getText()); 51 // } else { 52 // break; 53 // } 54 ObjectMessage message = (ObjectMessage)consumer.receive(); 55 56 if (message != null) { 57 User user = (User)message.getObject(); 58 System.err.println(user.toString()); 59 } 60 }*/ 61 //使用监听器来监听发送的消息 62 consumer.setMessageListener(new MessageListener() { 63 64 @Override 65 public void onMessage(Message message) { 66 try { 67 Object object = ((ObjectMessage)message).getObject(); 68 if (object != null) { 69 User user = (User)object; 70 System.out.println("ActiveMQ接收到的消息"+user.toString()); 71 } 72 } catch (JMSException e) { 73 e.printStackTrace(); 74 } 75 } 76 }); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 }


浙公网安备 33010602011771号