安装[win/linux]
1 win下安装
1.1下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
1.2运行ActiveMQ
解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。
启动ActiveMQ以后,登陆:(http://localhost:8161/admin/),账号密码默认均为admin
、解压运行期bin目录下的可执行文件
2 linux下安装
2.1下载ActiveMQ的tar包
去官方网站下载:http://activemq.apache.org/
2.2解压并启动
#解压
tar -xzvf apache-activemq-5.13.1-bin.tar.gz
mv apache-activemq-5.13.1 /usr/local
#启动
cd /usr/local/apache-activemq-5.13.1/bin
./activemq start
2.3测试
ip:8161 //http://hadoop:8161
2.4其他
三种运行方式:
(1)普通启动 ./activemq start
(2)启动并指定日志文件 ./activemq start >tmp/smlog
(3)后台启动方式nohup ./activemq start >/tmp/smlog
前两种方式下在命令行窗口关闭时或者ctrl+c时导致进程退出,采用后台启动方式则可以避免这种情况
测试端口:
ActiveMQ默认采用61616端口提供JMS服务,使用8161端口提供管理控制台服务,执行以下命令以便检验是否已经成功启动ActiveMQ服务。
打开端口:nc -lp 61616 &
查看哪些端口被打开 netstat -anp
查看61616端口是否打开: netstat -an | grep 61616
检查是否已经启动:
(1).查看控制台输出或者日志文件
(2).直接访问activemq的管理页面:http://localhost:8161/admin/
(1).查看控制台输出或者日志文件
(2).直接访问activemq的管理页面:http://localhost:8161/admin/
关闭:
如果开启方式是使用(1)或(2),则直接ctrl+c或者关闭对应的终端即可
如果开启方式是(3),则稍微麻烦一点:
先查找到activemq对应的进程:
ps -ef | grep activemq
然后把对应的进程杀掉,假设找到的进程编号为 168168
kill -9 168168
设置UI管理权限:
activeMQ是使用jetty容器启动的,所以要修改权限配置应该在jetty相关配置下找,进入$activeMQ_home/conf查询jetty.xml
将property name为authenticate的属性value="true"改为"false",登陆时就不会弹出用户名密码要求输入
当然 为了安全,这里一般是不会设为false的,我们可以添加自己的账户配置,控制台的登录用户名密码保存在conf/jetty-realm.properties文件中,我们可以通过修改此文件区添加自己的账户
添加一个账号为root,密码为123,角色为admin的用户
注意:以上是对管控台做的用户权限设置,而非java接口API调用的设置
我们知道,在利用java接口调用activeMQ时建立链接时是需要账户名和密码已经传输协议的,通常使用默认即可,
账户:ActiveMQConnectionFactory.DEFAULT_USER
密码:ActiveMQConnectionFactory.DEFAULT_PASSWORD
//1:创建链接工厂ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://hadoop:61616"
);
为了安全起见,如果要自己设置账户密码,需要修改$ACTIVEMQ_HOME/conf/activemq.xml,在此配置文件中添加一个插件配置即可
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="root" password="123" groups="users,admins" />
</users>
</simpleAuthenticationPlugin>
</plugins>
3.activeMQ的java操作-helloword
创建project:ActiveMQ-5.5,并导入apache-activemq-5.5.1\lib目录下需要用到的jar文件,项目结构如下图所示:
java代码:
Receiver.java
package MQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
Sender.java
package MQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private static final int SEND_NUMBER = 5;
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session
.createTextMessage("ActiveMq 发送的消息" + i);
// 发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}
发送端:
接收端: