文章目录
- 一、maven方式
- 1. 依赖
- 2. 发送端
- 3. 接收端
- 4. 工具类
- 二、引入jar方式
- 2.1.下载jar
- 2.2. 按需导入
- 三、添加账号密码
一、maven方式
1. 依赖
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>5.5.0</version></dependency>
2. 发送端
简洁版本
package mq;import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws Exception {// 1.创建连接工厂ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.119:61616");// 2.创建连接Connection connection = connectionFactory.createConnection();// 3.启动连接connection.start();// 4.获取session(会话对象) 参数1:是否启动事务 参数2:消息确认方式Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.创建队列对象Queue queue = session.createQueue("test-queue");// 6.创建消息生产者对象MessageProducer producer = session.createProducer(queue);// 7.创建消息对象(文本消息)TextMessage textMessage = session.createTextMessage("发送点点对消息模拟第一轮测试!");// 8.发送消息producer.send(textMessage);// 9.关闭资源producer.close();session.close();connection.close();}
}
测试版本
package com.gblfy.activemq.qq;import javax.jms.*;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static String ADDRESS = "192.168.0.119:61616";public static String QUEUE = "test-queue";public static String SENDMSG = "发送点点对消息模拟第一轮测试!";public static void main(String[] args) throws Exception {//连接工厂ConnectionFactory connectionFactory;//连接Connection connection = null;//会话 接受或者发送消息的线程Session session = null;//消息的目的地Queue queue;//消息生产者MessageProducer messageProducer = null;TextMessage textMessage;try {// 1.创建连接工厂connectionFactory = new ActiveMQConnectionFactory("tcp://" + ADDRESS);// 2.创建连接connection = connectionFactory.createConnection();// 3.启动连接connection.start();// 4.获取session(会话对象) 参数1:是否启动事务 参数2:消息确认方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.创建队列对象queue = session.createQueue(QUEUE);// 6.创建消息生产者对象messageProducer = session.createProducer(queue);// 7.创建消息对象(文本消息)textMessage = session.createTextMessage(SENDMSG);// 8.发送消息messageProducer.send(textMessage);} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.关闭资源messageProducer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}}
}
3. 接收端
简洁版本
package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Consumer {public static void main(String[] args) throws Exception {// 1.创建连接工厂ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.119:61616");// 2.创建连接Connection connection = connectionFactory.createConnection();// 3.启动连接connection.start();// 4.获取session(会话对象) 参数1:是否启动事务 参数2:消息确认方式Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.创建队列对象Queue queue = session.createQueue("test-queue");// 6.创建消息消费者对象MessageConsumer consumer = session.createConsumer(queue);// 7.设置监听consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待键盘输入System.in.read();// 9.关闭资源consumer.close();session.close();connection.close();}
}
测试版本
package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;
import java.io.IOException;public class Consumer {public static String ADDRESS = "192.168.0.119:61616";public static String QUEUE = "test-queue";public static void main(String[] args) throws Exception {//连接工厂ConnectionFactory connectionFactory;//连接Connection connection = null;//会话 接受或者发送消息的线程Session session = null;//消息的目的地Queue queue;//消息生产者MessageConsumer consumer = null;// 1.创建连接工厂try {connectionFactory = new ActiveMQConnectionFactory("tcp://" + ADDRESS);// 2.创建连接connection = connectionFactory.createConnection();// 3.启动连接connection.start();// 4.获取session(会话对象) 参数1:是否启动事务 参数2:消息确认方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.创建队列对象queue = session.createQueue(QUEUE);// 6.创建消息消费者对象consumer = session.createConsumer(queue);// 7.设置监听consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待键盘输入System.in.read();} catch (JMSException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 9.关闭资源consumer.close();session.close();connection.close();}}
}
4. 工具类
package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class MQUtil {//连接工厂ConnectionFactory connectionFactory;//连接Connection connection = null;//会话 接受或者发送消息的线程Session session = null;//消息的目的地Queue queue;public Queue createConnectionFactory(String address, String queueName) {try {// 1.创建连接工厂connectionFactory = new ActiveMQConnectionFactory("tcp://" + address);// 2.创建连接connection = connectionFactory.createConnection();// 3.启动连接connection.start();// 4.获取session(会话对象) 参数1:是否启动事务 参数2:消息确认方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.创建队列对象queue = session.createQueue(queueName);} catch (JMSException e) {e.printStackTrace();}return queue;}public void sendMsg(String address, String queueName, String message) {MessageProducer messageProducer = null;TextMessage textMessage;try {// 6.创建消息生产者对象queue = createConnectionFactory(address, queueName);messageProducer = session.createProducer(queue);// 7.创建消息对象(文本消息)textMessage = session.createTextMessage(message);// 8.发送消息messageProducer.send(textMessage);} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.关闭资源messageProducer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}}public void recMsg(String address, String queueName) {//消息生产者MessageConsumer consumer = null;try {// 6.创建消息消费者对象queue = createConnectionFactory(address, queueName);consumer = session.createConsumer(queue);// 7.设置监听consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待键盘输入System.in.read();} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.关闭资源consumer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}}public static void main(String[] args) {String ADDRESS = "192.168.0.119:61616";String QUEUE = "test-queue";String SENDMSG = "发送点点对消息模拟第一轮测试!";MQUtil mqUtil = new MQUtil();mqUtil.sendMsg(ADDRESS, QUEUE, SENDMSG);mqUtil.recMsg(ADDRESS, QUEUE);}
}
二、引入jar方式
2.1.下载jar
建议用maven坐标先把需要的jar下载到本地仓库,再把本地仓库中的jar复制到工程的lib文件夹下面
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>5.5.0</version></dependency>
如果出现SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder
请跳转https://gblfy.blog.csdn.net/article/details/107018564
2.2. 按需导入
三、添加账号密码
源码分析:
public ActiveMQConnectionFactory(String userName, String password, String brokerURL) {setUserName(userName);setPassword(password);setBrokerURL(brokerURL);}
从源码看出在创建连接工厂的时候,在添加账号面的参数即可,注意参数顺序。
例如:
//连接用户名private static final String USERNAME = "admin";//连接密码private static final String PASSWORD = "admin";//连接地址private static final String BROKEURL = "10.5.6.19:61616";connectionFactory = new ActiveMQConnectionFactory(USERNAME,"tcp://" + BROKEURL,PASSWORD);