本文章是在网易云课堂的课程学习中编写,部分图片从网易云课堂ppt引用
一、ActiveMQ入门
1、ActiveMQ是什么
ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线。 ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
2、JMS规范
Java消息服务((Java Message Service,即JMS)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的APl,是客户端API。
JMS对象模型:
JMS消息模型:
JMS消息结构:包含消息头、消息属性、消息体
消息头:关于消息的描述信息
消息属性:消息的附加消息头,属性名可以自定义
消息体:传输的数据
3、ActiveMQ的特性
遵循JMS规范,只要熟悉JMS规范,就能操作ActiveMQ
支持多种编程语言:java、C、php等
支持多种传输协议:http协议、UDP、MTP、AMQP等
有多种持久化方式:文件、数据等存储方式
4、ActiveMQ如何安装及使用
准备CentOS7和JDK1.8环境
下载地址: http://activemq.apache.org/activemq-5158-release.html
解压: tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var
修改目录名称 mv /var/apache-activemq-5.15.8/ /var/activemq/
启动: ./bin/activemq start
停止:./bin/activemq stop
操作练习
1、创建一个systemd服务文件:vi /usr/lib/systemd/system/activemq.service
2、 放入内容
[Unit]
Description=ActiveMQ service
After=network.target
[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq
[Install]
WantedBy=multi-user.target
3、 找到java命令所在的目录 whereis java
4、设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
5、 通过systemctl管理activemq启停
- 启动activemq服务:
systemctl start activemq
- 查看服务状态:
systemctl status activemq
- 创建软件链接:
ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service
- 开机自启:
systemctl enable activemq
- 检测是否开启成功(enable):
systemctl list-unit-files |grep activemq
6、 防火墙配置,Web管理端口默认为8161(admin/admin),通讯端口默认为61616
- 添加并重启防火墙
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
- 或者直接关闭防火墙:
systemctl stop firewalld.service
7、 修改web管理系统的部分配置,配置文件在/var/activemq/conf
ActiveMQ的Web管理平台是基于jetty运行,因此在/var/activemq/conf目录可以看到jetty的配置文件
- 端口修改
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<!--此处即为管理平台的端口-->
<property name="port" value="8161"/>
</bean>
- 关闭登录
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="user,admin" />
<!-- 改为false即可关闭登陆 -->
<property name="authenticate" value="true" />
</bean>
- 其他配置:
/var/activemq/conf/jetty-realm.properties
## ---------------------------------------------------------------------------
# 在此即可维护账号密码,格式:
# 用户名:密码,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
8、 JAVA客户端的使用
- 标准客户端使用
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.8</version>
</dependency>
- Spring中使用:
http://spring.io/guides/gs/messaging-jms/
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.15.8</version>
<exclusions>
<exclusion>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
</exclusions>
</dependency>