文章目录
ActiveMQ-安装和基本使用
1.Windows下安装ActiveMQ
1.1.官网下载ActiveMQ
1.2.解压修改配置文件
apache-activemq-5.15.3\conf\activemq.xml,将0.0.0.0修改为localhost
![]()
ps:若出现以下错误闪退无法启动,则因MQ版本和JRE版本不匹配问题导致,更换JRE或者MQ版本即可。这里我降低了MQ的版本解决问题。
ps:如果更换版本依然出现闪退,则可以通过编辑activemq.bat,在末端加上pause,重新启动则可以暂停闪退找出问题
ps:windows系统直接运行activemq.bat闪退,则可尝试使用cms命令行 运行activemq-admin.bat start可以解决问题
启动成功后,访问http://localhost:8161/admin,即可登录ActiveMQ管理界面。默认账号admin 密码admin
2.Linux下安装ActiveMQ
2.1 创建目录
cd /usr/local
mkdir activemq
2.2 下载并解压activemq
tar -zxvf apache-activemq-5.13.5-bin.tar.gz
2.3 启动activemq
activemq启动分linux-x86-32和linux-x86-64
cd linux-x86-64
./activemq start
2.4 访问
3.ActiveMQ 在Java Api的基本使用
以下是一个使用参考:
3.1 导包
<!-- activemq 相关maven依赖 -->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.7.0</version>
</dependency>
<!-- 日志相关依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
3.2 API使用
ProducePool
package com.ithzk.activemq;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/** * @author hzk * @date 2018/3/31 */
public class ProducePool {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "mytopic";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageProducer messageProducer = null;
MessageProducer producer = null;
/** * 初始化 * @throws JMSException */
public void initialize() throws JMSException {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = activeMQConnectionFactory.createConnection();
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
/** * 发送消息 * @param message * @throws JMSException */
public void produceMessage(String message) throws JMSException {
initialize();
TextMessage textMessage = session.createTextMessage(message);
connection.start();
System.out.println("Producer -> Send Message:"+message);
producer.send(textMessage);
System.out.println("Producer -> Send Message complete!");
}
public void close() throws JMSException {
System.out.println("Producer -> close connection!");
if(producer != null){
producer.close();
}
if(session != null){
session.close();
}
if(connection != null){
connection.close();
}
}
}
ProduceTest
package com.ithzk.activemq;
import javax.jms.JMSException;
import java.util.Random;
/** * @author hzk * @date 2018/3/31 */
public class ProduceTest {
public static void main(String[] args) throws JMSException, InterruptedException {
ProducePool producePool = new ProducePool();
Random random = new Random();
for (int i =0;i < 20;i++){
Thread.sleep(random.nextInt(3)*1000);
producePool.produceMessage("Hello,ActiveMQ! Number:"+i);
producePool.close();
}
producePool.initialize();
}
}
这样就可以往队列中发送信息了