一 ActiveMQ 下载 安装
ActiveMQ 官网https://activemq.apache.org/
目前打开官网有两个版本选择:ActiveMQ "Classic" 和 ActiveMQ Artemis ,Classic是一直存在的,Artemis是新的高性能,由于是jdk1.8的项目,所以选用之前的Classic,最新的版本使用jdk11+,由于我当前环境是使用jdk8,因此找支持jdk1.8 例如 ActiveMQ 5.16.7,
点这里直接下载linux版(202311 支持jdk1.8最新的安全版5.16.7):https://archive.apache.org/dist/activemq/5.16.7/apache-activemq-5.16.7-bin.tar.gz
ActiveMQ 下载
wget https://archive.apache.org/dist/activemq/5.16.7/apache-activemq-5.16.7-bin.tar.gz
ActiveMQ 安装
tar -xvzf apache-activemq-5.16.7-bin.tar.gz
把下载的ActiveMQ 压缩包解压就算是安装完成。
二 ActiveMQ 配置
1 配置web页面管理界面
打开配置文件路径:apache-activemq-5.16.7/conf/jetty.xml
说明:一般只需改端口,上面是8611,所以这里web管理地址是:http://xx.xx.xx.xx:8611/
配置web管理账号密码
打开配置文件路径:/apache-activemq-5.16.7/conf/jetty-realm.properties
admin: pass, admin
user: pass, user
说明:第1列是用户名,第二列是密码,第三列是角色(一般不用改)
2 配置java客户端 生产与消费者密码
打开配置文件 /apache-activemq-5.16.7/conf/activemq.xml
在位置在broker标签内增加:
<!-- add plugins -->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
配置文件 -> \conf\credentials.properties(设置用户名密码)
activemq.username=admin
activemq.password=xxxxxx
三 ActiveMQ 启动停止管理
方式一:命令行操作
启动运行
bin/activemq start
停止运行
bin/activemq stop
方式二:配置服务
配置成服务并设置开机启动,防止服务器重启忘记启动。
ln -snf /usr/local/apache-activemq-5.16.7/bin/activemq /etc/init.d/activemq
chkconfig --add activemq
chkconfig activemq on
常用的服务指令:
service activemq start
service activemq stop
service activemq status
service activemq restart
四 安装验证
1 web端管理界面验证
web端管理界面,默认端口使是8161
2 检查服务使用端口
检查消费者与生产者默认使用的端口61616,如下:
netstat -an|grep 61616
端口连接正常。
五 在Java应用使用ActiveMQ
1 引用ActiveMQ
pom配置文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
2 spring框架的ActiveMQ配置
spring框架配置文件的ActiveMQ配置 如下:
spring:
activemq:
broker-url: tcp://xx.xx.xx.xx:61616
user: admin
password: xxxxxx
in-memory: true
pool:
enabled: false
3 在JAVA代码例用ActiveMQ:
生产端发送队列信息java代码:
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
/**
* @author hua
*/
@Service
public class MsgSendServiceImpl {
@Autowired
private JmsMessagingTemplate jmsTemplate;
/**
* 发送消息 采用指定队列类型
*
* @param queueName 队列名称
* @param message 队列消息
*/
public void sendMessageByQueue(String queueName, final String message) {
Destination destination = new ActiveMQQueue(queueName);
jmsTemplate.convertAndSend(destination, message);
}
}
客户端接收消息队列代码如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
/**
* @author hua
*/
@Service
public class MsgReceiveServiceImpl {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 根据配置文件只能接收一种类型
@JmsListener(destination = "top_name")
public void receiveQueue(String text) {
System.out.println("收到信息:"+text);
}
}