大家好,我是小利。今天我想分享一个经常被使用的中间件,ActiveMQ。正如大家所知,ActiveMQ 是一个消息队列。接下来,我将详细介绍 ActiveMQ 的安装步骤以及 Java 连接实例。在开始之前,请确保已经安装了 JDK 11 或更高版本,并正确配置了环境变量。下面让我们开始吧。
一、找到activemq的官网,https://activemq.apache.org/,然后
点击如下图所示的下载。
我这边下载的是5.18.4的版本
二、下载完传到服务器上,然后解压
tar -zxvf apache-activemq-5.18.4-bin.tar.gz
进入安装目录下的conf,改jetty.xml的地址为0.0.0.0,如果是本地的话不用改,服务器的话改下,具体看下边的图
cd apache-activemq-5.18.4/conf/
三、在启动之前服务器上要把8161端口和61616打开。然后进入bin目录,运行启动命令./activemq start
四、http://47.108.178.107:8161/admin/,进入这个地址(自己的服务器地址),会出现下面这个画面,说明安装成功。
五、接下来进行代码连接,作为生产者和消费者进行发送和接收消息,首先配置maven依赖,看下面的示例
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version> <!-- 根据你需要的版本修改 -->
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version> <!-- 根据你需要的版本修改 -->
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.18.3</version> <!-- 这里使用的版本号可能会有所不同 -->
</dependency>
</dependencies>
生产者代码
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQSender {
public static void main(String[] args) {
// ActiveMQ 的连接信息
String brokerUrl = "tcp://47.108.178.107:61616"; // ActiveMQ 服务器的地址和端口
String queueName = "exampleQueue"; // 队列的名称
try {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
// 创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建队列目标
Destination destination = session.createQueue(queueName);
// 创建消息生产者
MessageProducer producer = session.createProducer(destination);
// 创建消息
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
// 发送消息
producer.send(message);
System.out.println("Sent message: " + message.getText());
// 关闭连接
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Exception caught: " + e);
e.printStackTrace();
}
}
}
运行,结果如下图所示
点击下图quene选项,会看到如下信息。解释下都代表什么意思
Number Of Pending Messages :待消费数量
Number Of Consumers :消费者数量
Messages Enqueued :入队数量
Messages Dequeued :出队数量
这个图表示,待消费数量为1,消费者为0,入队1,出队0
消费者代码
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.TextMessage;
public class ActiveMQConsumer {
public static void main(String[] args) {
// ActiveMQ 的连接信息
String brokerUrl = "tcp://47.108.178.107:61616"; // ActiveMQ 服务器的地址和端口
String queueName = "exampleQueue"; // 队列的名称
try {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
// 创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建队列目标
Destination destination = session.createQueue(queueName);
// 创建消息消费者
MessageConsumer consumer = session.createConsumer(destination);
// 使用死循环持续接收消息
while (true) {
// 接收消息
TextMessage message = (TextMessage) consumer.receive();
if (message != null) {
System.out.println("Received message: " + message.getText());
}
}
// 不关闭连接
// session.close();
// connection.close();
} catch (Exception e) {
System.out.println("Exception caught: " + e);
e.printStackTrace();
}
}
}
点击运行,会发现消费者数量为1,入队1,出队0,说明消息被消费了
以上就是activemq安装的全部内容了,希望能对大家有所帮助