下载并安装ActiveMQ服务器端
1.列表内容从[http://activemq.apache.org/download.html下载最新的ActiveMQ
2.直接解压,然后拷贝到需要要安装的位置就OK
启动ActiveMQ
1.普通启动:进入到安装目录下面的bin目录下 ./activemq start
2.启动时指定日志文件:./activemq start > /temp/activemqlog
检查ActiveMQ服务是否启动
1.使用管理界面
打开管理界面http : //127.0.0.1 : 8161/admin/账号密码默认都是admin
2.查看控制台输出或者日志文件。
3.查看监听窗口 运行 netstat -nl | grep 61616 命令。ActiveMQ默认监听的是61616端口。
停止ActiveMQ
- 切换到 ActiveMQ/bin 运行 ./activemq stop
- ps -ef | grep activemq kill -9 进程号直接杀死。
简单用例
在使用ActiveMQ之前最好还是先了解JMS的基本原理和架构。http://blog.csdn.net/pengdandezhi/article/details/72779184
配置Maven依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
生产者发送消息
public class JmsSend{
public static void main(String[] args)throws Exception {
ConnectionFactory connectionFactory = new ActiveMqConnectionFactory("tcp://192.168.1.129:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageProduce producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("message");
produecer.send(message);
session.commit();
session.close();
connection.close();
}
}
消费者接收消息
public class JmsSend{
public static void main(String[] args)throws Exception {
ConnectionFactory connectionFactory = new ActiveMqConnectionFactory("tcp://192.168.1.129:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageConsumer consumer = session.createConsumer(destination);
TextMessage message = consumer.receive();
System.out.println("消费者消费信息:" + messsage);
session.commit();
session.close();
connection.close();
}
}