创建Eclipse项目并运行
创建java project:ActiveMQ-5.14,新建lib文件夹 打开apache-activemq-5.14.0-bin\apache-activemq-5.14.0\lib目录 拷贝如下6个jar包:activemq-broker-5.14.0.jar
activemq-client-5.14.0.jar
activemq-all-5.14.0.jar
geronimo-j2ee-management_1.1_spec-1.0.1.jar
geronimo-jms_1.1_spec-1.1.1.jar
slf4j-api-1.7.13.jar
这6个jar文件到lib文件夹中,并Build Path->Add to Build Path
结构如图:
Sender.java
1 package test; 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 18 ConnectionFactory connectionFactory; // ConnectionFactory--连接工厂,JMS用它创建连接 19 // Provider 的连接 20 Connection connection = null; // Connection :JMS 客户端到JMS 21 Session session; // Session: 一个发送或接收消息的线程 22 Destination destination; // Destination :消息的目的地;消息发送给谁. 23 MessageProducer producer; // MessageProducer:消息发送者 24 // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar 25 connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, 26 ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); 27 try { // 构造从工厂得到连接对象 28 connection = connectionFactory.createConnection(); 29 // 启动 30 connection.start(); 31 // 获取操作连接 32 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 33 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 34 destination = session.createQueue("FirstQueue"); 35 // 得到消息生成者【发送者】 36 producer = session.createProducer(destination); 37 // 设置不持久化,此处学习,实际根据项目决定 38 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 39 // 构造消息,此处写死,项目就是参数,或者方法获取 40 sendMessage(session, producer); 41 session.commit(); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } finally { 45 try { 46 if (null != connection) 47 connection.close(); 48 } catch (Throwable ignore) { 49 } 50 } 51 } 52 53 public static void sendMessage(Session session, MessageProducer producer) throws Exception { 54 for (int i = 1; i <= SEND_NUMBER; i++) { 55 TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i); 56 // 发送消息到目的地方 57 58 System.out.println("发送消息:" + "ActiveMq 发送的消息" + i); 59 producer.send(message); 60 } 61 } 62 }
Receiver.java
package test;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注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); while (true) { // 设置接收者接收消息的时间,为了便于测试,这里谁定为100s TextMessage message = (TextMessage) consumer.receive(500000); 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.测试过程
先运行:Receiver.java 再运行:Sender.java可以看到结果
Sender运行后: 发送消息:ActiveMq 发送的消息1 发送消息:ActiveMq 发送的消息2 发送消息:ActiveMq 发送的消息3 发送消息:ActiveMq 发送的消息4 发送消息:ActiveMq 发送的消息5Receiver运行后: 收到消息ActiveMq 发送的消息1 收到消息ActiveMq 发送的消息2 收到消息ActiveMq 发送的消息3 收到消息ActiveMq 发送的消息4 收到消息ActiveMq 发送的消息5
要想看到不同的输出内容,通过点击如下图的按钮切换console