1.下载ActiveMQ,安装并启动activeMQ,
2.登录控制台, default: admin/admin
新建queue名为:FirstQueue
依赖包:
org.apache.activemq activemq-core 5.7.0
package org.jun.util;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 ActiveMQSenderUtil { public static void main(String arg[]) { Sender("FirstQueue", "通知:everybody,高性能activeMQ!"); } /** * 发送消息 * * @param content 发送内容 * @param queueName 队列名 */ public static void Sender(String queueName, String content) { // ConnectionFactory :连接工厂,JMS 用它创建连接工厂实例,此处采用ActiveMq的实现jar ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); //tcp地址 // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // MessageProducer:消息发送者 MessageProducer producer; try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue(queueName); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // 设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 构造消息,此处写死,项目就是参数,或者方法获取 TextMessage message = session.createTextMessage(content); System.out.println("ActiveMq 发送的消息:" + content); producer.send(message);// 发送消息到目的地方 session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) { connection.close(); } } catch (Throwable ignore) { } } }}
package org.jun.util;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 ActiveMQReceiverUtil { public static void main(String args[]) { queueReceiver("FirstQueue"); } /** * 接收消息 * * @param quequName 队列名 * @return */ public static String queueReceiver(String quequName){ // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");//tcp地址 // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // 消费者,消息接收者 MessageConsumer consumer; String receiveMsg = ""; try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue(quequName); consumer = session.createConsumer(destination); while (true) { //设置接收者接收消息的时间,为了便于测试,这里谁定为100s TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { receiveMsg = message.getText(); System.out.println("收到消息:" + message.getText()); } else { break; } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) { connection.close(); } } catch (Throwable ignore) { } } return receiveMsg; }}
运行结果: