一、环境准备
1,官网http://activemq.apache.org/下载最新版本的ActiveMQ,并解压
2,打开对应的目录,在Mac环境下,一般可以运行命令:
cd /Users/***/Downloads/apache-activemq-***/bin/macosx
./activemq start
3,启动成功后,登录http://localhose:8161/admin/,登陆账号和密码都为admin,创建一个queue名为jyQueue。
二、创建Eclipse项目
1,新建项目,并导入对应的jar文件如下图所示
2,Sender.java和Receiver.java
1 package com.simple.activemq; 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.MessageProducer; 8 import javax.jms.Session; 9 import javax.jms.TextMessage; 10 import org.apache.activemq.ActiveMQConnection; 11 import org.apache.activemq.ActiveMQConnectionFactory; 12 13 public class Sender { 14 private static final int SEND_NUMBER = 5; 15 16 public static void main(String[] args) { 17 // ConnectionFactory :连接工厂,JMS 用它创建连接 18 ConnectionFactory connectionFactory; 19 // Connection :JMS 客户端到JMS Provider 的连接 20 Connection connection = null; 21 // Session: 一个发送或接收消息的线程 22 Session session; 23 // Destination :消息的目的地;消息发送给谁. 24 Destination destination; 25 // MessageProducer:消息发送者 26 MessageProducer producer; 27 // TextMessage message; 28 // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar 29 connectionFactory = new ActiveMQConnectionFactory( 30 ActiveMQConnection.DEFAULT_USER, 31 ActiveMQConnection.DEFAULT_PASSWORD, 32 "tcp://localhost:61616"); 33 try { 34 // 构造从工厂得到连接对象 35 connection = connectionFactory.createConnection(); 36 // 启动 37 connection.start(); 38 // 获取操作连接 39 session = connection.createSession(Boolean.TRUE, 40 Session.AUTO_ACKNOWLEDGE); 41 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 42 destination = session.createQueue("jyQueue"); 43 // 得到消息生成者【发送者】 44 producer = session.createProducer(destination); 45 // 设置不持久化,此处学习,实际根据项目决定 46 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 47 // 构造消息,此处写死,项目就是参数,或者方法获取 48 sendMessage(session, producer); 49 session.commit(); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } finally { 53 try { 54 if (null != connection) 55 connection.close(); 56 } catch (Throwable ignore) { 57 } 58 } 59 } 60 61 public static void sendMessage(Session session, MessageProducer producer) 62 throws Exception { 63 for (int i = 1; i <= SEND_NUMBER; i++) { 64 TextMessage message = session 65 .createTextMessage("ActiveMq 发送的消息" + i); 66 // 发送消息到目的地方 67 System.out.println("发送消息:" + "ActiveMq 发送的消息" + i); 68 producer.send(message); 69 } 70 } 71 }
1 package com.simple.activemq; 2 3 import javax.jms.Connection; 4 import javax.jms.ConnectionFactory; 5 import javax.jms.Destination; 6 import javax.jms.MessageConsumer; 7 import javax.jms.Session; 8 import javax.jms.TextMessage; 9 import org.apache.activemq.ActiveMQConnection; 10 import org.apache.activemq.ActiveMQConnectionFactory; 11 12 public class Receiver { 13 public static void main(String[] args) { 14 // ConnectionFactory :连接工厂,JMS 用它创建连接 15 ConnectionFactory connectionFactory; 16 // Connection :JMS 客户端到JMS Provider 的连接 17 Connection connection = null; 18 // Session: 一个发送或接收消息的线程 19 Session session; 20 // Destination :消息的目的地;消息发送给谁. 21 Destination destination; 22 // 消费者,消息接收者 23 MessageConsumer consumer; 24 connectionFactory = new ActiveMQConnectionFactory( 25 ActiveMQConnection.DEFAULT_USER, 26 ActiveMQConnection.DEFAULT_PASSWORD, 27 "tcp://localhost:61616"); 28 try { 29 // 构造从工厂得到连接对象 30 connection = connectionFactory.createConnection(); 31 // 启动 32 connection.start(); 33 // 获取操作连接 34 session = connection.createSession(Boolean.FALSE, 35 Session.AUTO_ACKNOWLEDGE); 36 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 37 destination = session.createQueue("jyQueue"); 38 consumer = session.createConsumer(destination); 39 while (true) { 40 //设置接收者接收消息的时间,为了便于测试,这里谁定为100s 41 TextMessage message = (TextMessage) consumer.receive(100000); 42 if (null != message) { 43 System.out.println("收到消息" + message.getText()); 44 } else { 45 break; 46 } 47 } 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } finally { 51 try { 52 if (null != connection) 53 connection.close(); 54 } catch (Throwable ignore) { 55 } 56 } 57 } 58 }
3,先后运行Sender.java和Receiver.java