ActiveMQ下载与安装使用

ActiveMQ下载与安装
下载
官方网站 http://activemq.apache.org/
在这里插入图片描述
在这里插入图片描述
解压修改配置文件
apache-activemq-5.15.9\conf\activemq.xml,节点下将0.0.0.0修改为localhost
在这里插入图片描述
不要使用bin下的activemq.bat启动,使用bin下wiun64或32(根据你系统位数选择,这里我使用的是64位)的activemq.bat启动
在这里插入图片描述

若出现以下错误闪退无法启动,则因MQ版本和JRE版本不匹配问题导致,更换JRE或者MQ版本即可。这里我使用了1.8的jdk。
在这里插入图片描述
如果更换版本依然出现闪退,则可以通过编辑activemq.bat,在末端加上pause,重新启动则可以暂停闪退找出问题
在这里插入图片描述
windows系统直接运行activemq.bat闪退,则可尝试使用cms命令行 运行activemq-admin.bat start可以解决问题
在这里插入图片描述
Linux下安装ActiveMQ
创建目录

cd /usr/local
mkdir activemq

下载并解压activemq

tar -zxvf apache-activemq-5.15.9-bin.tar.gz

在这里插入图片描述
启动activemq

activemq启动分linux-x86-32和linux-x86-64
cd linux-x86-64
./activemq start

在这里插入图片描述
访问 http://localhost:8161
在这里插入图片描述
账号:admin 密码:admin
点对点消息列表
在这里插入图片描述
在这里插入图片描述
列表各列信息含义如下

Number Of Pending Messages :等待消费的消息 这个是当前未出队列的数量。

Number Of Consumers :消费者 这个是消费者端的消费者数量

Messages Enqueued :进入队列的消息 进入队列的总数量,包括出队列的。

Messages Dequeued :出了队列的消息 可以理解为是消费这消费掉的数量。

重启
./activemq restart
关闭
./activemq stop

ActiveMQ 在Java Api的基本使用(仅供参考)
导包

  <!-- 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>

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();

    }
}


这样就可以往队列中发送信息了
在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值