ActiveMQ1--安装、配置

1.下载安装 ActiveMQ

ActiveMQ 官网下载地址:http://activemq.apache.org/download.html
可根据操作系统不同,下载不同版本,此处我下载的是apache-activemq-5.15.2-bin.tar.gz linux 版本。
需要注意下依赖的jdk版本

2.启动 ActiveMQ

2.1 下载后需要先解压

tar -xvf apache-activemq-5.15.2-bin.tar.gz  

2.2 目录结构

bin 存放的是脚本文件
conf 存放的是基本配置文件
data 存放的是日志文件
docs 存放的是说明文档
examples 存放的是简单的实例
lib 存放的是activemq所需jar包
webapps 用于存放项目的目录

2.3 启动

cd cd /home/sherlocky/apache-activemq-5.15.2/bin  
./activemq start

输入命令之后,会提示创建了一个进程号,这就说明服务已经成功启动了。
如果是windows系统,可以通过以下命令启动

apache-activemq-5.15.2\bin\win64\activemq.bat  

也可以注册服务

apache-activemq-5.15.2\bin\win64\InstallService.bat  

2.3 退出

cd cd /home/sherlocky/apache-activemq-5.15.2/bin  
./activemq stop

可以将以上命令写成 sh 方便执行。

3.配置

ActiveMQ 默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQadmin应用。

http://127.0.0.1:8161/admin/  

默认账号:admin/admin

3.1 authenticate

ActiveMQ 使用的是jetty服务器, 打开{ACTIVEMQ_HOME}/conf/jetty.xml文件,找到

<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">  
    <property name="name" value="BASIC" />
    <property name="roles" value="user,admin" />
    <!-- set authenticate=false to disable login -->
    <property name="authenticate" value="true" />
</bean>  
<bean id="adminSecurityConstraint" class="org.eclipse.jetty.util.security.Constraint">  
    <property name="name" value="BASIC" />
    <property name="roles" value="admin" />
     <!-- set authenticate=false to disable login -->
    <property name="authenticate" value="true" />
</bean>  

低版本的authenticate的属性默认为false 需要改为true,高版本的已经默认为true,忽略即可。

3.2 修改控制台密码

控制台的登录用户名密码配置在 {ACTIVEMQ_HOME}/conf/jetty-realm.properties文件中。

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
## 
## http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin  
user: user, user  

需要注意的是:用户名和密码的格式是:username: password [,rolename ...] 即 用户名: 密码, 角色

3.3 消息消费者密码

(1) 修改{ACTIVEMQ_HOME}/conf/activemq.xml配置,需要新增一个插件,在<broker>节点里面<systemUsage>节点前面添加:

<plugins>  
    <simpleAuthenticationPlugin>
        <users>
            <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
        </users>
    </simpleAuthenticationPlugin>
</plugins>  

(2) 消费者密码配置在 {ACTIVEMQ_HOME}/conf/credentials.properties文件中

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
## 
## http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

# Defines credentials that will be used by components (like web console) to access the broker

activemq.username=system  
activemq.password=manager  
guest.password=password  

这样配置后,在客户端调用处就需要设置密码才能连接,以 java 为例:

// 建立ConnectionFactory工厂对象,需要填入用户名,密码,以及连接的地址  
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
        "system",// ActiveMQConnectionFactory.DEFAULT_USER,  
        "manager",// ActiveMQConnectionFactory.DEFAULT_PASSWORD,  
        "tcp://localhost:61616");