Linux下安装ActiveMQ-5.15.8

简介: Linux下安装ActiveMQ-5.15.8

1.ActiveMQ官网链接:http://activemq.apache.org/
image
①选择Download
image
②选择ActiveMQ 5.15.8 Release
image
需要注意的是:ActiveMQ 5.15.8所需Jdk版本最低为1.8,修改具体请看Change Log
image
③复制下载链接

cd /usr/local
wget http://www.apache.org/dyn/closer.cgi?filename=/activemq/5.15.8/apache-activemq-5.15.8-bin.tar.gz&action=download

image
image
会发现,文件无法下载,我尝试着关闭防火墙,重启network,无效(因为响应是200)
image
因为URL中有一个"&"需要转义"&"

cd /usr/local
wget http://www.apache.org/dyn/closer.cgi?filename=\/activemq\/5.15.8\/apache-activemq-5.15.8-bin.tar.gz\&action=download

image
image
wget的命名规则是取最后一个"/"后面的内容,文件重命名

mv ./closer.cgi\?filename\=%2Factivemq%2F5.15.8%2Fapache-activemq-5.15.8-bin.tar.gz\&action\=download ./apache-activemq-5.15.8-bin.tar.gz

image
2.解压apache-activemq-5.15.8-bin.tar.gz

tar -xzvf ./apache-activemq-5.15.8-bin.tar.gz

image
3.启动ActiveMQ
①ActiveMQ内置了jetty Web容器,jetty的相关配置在jetty.xml中

vim /usr/local/apache-activemq-5.15.8/conf/jetty.xml

image
②ActiveMQ的配置文件

vim /usr/local/apache-activemq-5.15.8/conf/activemq.xml

image
③ActiveMQ管控台的用户名密码配置

vim /usr/local/apache-activemq-5.15.8/conf/jetty-realm.properties

image
④进入bin目录,启动ActiveMQ

/usr/local/apache-activemq-5.15.8/bin/activemq start

image
查看端口61616是否开启

netstat -an|grep 61616
netstat -an|grep 8161

image
image
⑤登录ActiveMQ管控台:http://192.168.0.115:8161/admin/
image
image
4.编写程序
①消息发送方(生产者):

package activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

public class ActiveMQ_Sender {
    public static void main(String[] args) throws JMSException {
        //1.建立ConnectionFactory工厂对象,需要填入用户名、密码以及要连接的地址,均使用默认即可,
        // 默认端口为"tcp://localhost:61616"
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://192.168.0.115:61616"
        );

        //2.通过ConnectionFactory工厂对象我们创建一个Connection连接,并且调用Connection的start方法开启连接,
        // connection默认是关闭的
        Connection connection = connectionFactory.createConnection();
        connection.start();

        //3.通过Connection工厂对象创建Session会话(上下文环境对象),用于接收消息,
        // 参数1为是否启用事务,参数2为签收模式,一般我们设置自动签收
        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

        //4.通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费信息来源的对象,
        // 在P2P模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。
        // 在程序中可以使用多个Queue和Topic
        Destination destination = session.createQueue("Queue_01");

        //5.我们需要通过Session对象创建消息的发送和接收对象(生产者和消费者),MessageProducer/MessageConsumer
        MessageProducer messageProducer = session.createProducer(destination);

        //6.我们可以使用MessageProducer的setDeliveryMode()方法为其设置持久化特性和非持久化特性(DeliveryMode)
        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        //7.使用JMS规范的TextMessage形式创建数据(通过Session对象),并用MessageProducer的send方法发送数据。
        // 同理客户端使用receive方法进行接收数据
        for (int i = 1; i <= 10; i++) {
            TextMessage textMessage = session.createTextMessage();
            textMessage.setText("Sender: HelloWorld! Message_ID = "+i);
            messageProducer.send(textMessage);
        }

        //8.关闭Connection连接
        if (connection != null){
            connection.close();
        }
    }
}

运行成功后,刷新ActiveMQ管控台,会看到刚刚创建的消息数量和状态
image
点击消息名称查看
image
②消息接收方(消费者):

package activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

public class ActiveMQ_Receiver {
    public static void main(String[] args) throws JMSException {
        //1.建立ConnectionFactory工厂对象,需要填入用户名、密码以及要连接的地址,均使用默认即可,
        // 默认端口为"tcp://localhost:61616"
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://192.168.0.115:61616"
        );

        //2.通过ConnectionFactory工厂对象我们创建一个Connection连接,并且调用Connection的start方法开启连接,
        // connection默认是关闭的
        Connection connection = connectionFactory.createConnection();
        connection.start();

        //3.通过Connection工厂对象创建Session会话(上下文环境对象),用于接收消息,
        // 参数1为是否启用事务,参数2为签收模式,一般我们设置自动签收
        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

        //4.通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费信息来源的对象,
        // 在P2P模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。
        // 在程序中可以使用多个Queue和Topic
        Destination destination = session.createQueue("Queue_01");

        //5.我们需要通过Session对象创建消息的发送和接收对象(生产者和消费者),MessageProducer/MessageConsumer
        MessageConsumer messageConsumer = session.createConsumer(destination);

        //6.使用JMS规范的TextMessage形式创建数据(通过Session对象),并用MessageProducer的send方法发送数据。
        // 同理客户端使用receive方法进行接收数据
        while (true){
            TextMessage textMessage = (TextMessage) messageConsumer.receive();
            if (textMessage == null) break;
            System.out.println("Receive_Message: "+textMessage.getText());
        }

        //8.关闭Connection连接
        if (connection != null){
            connection.close();
        }
    }
}

image
image
image
5.ActiveMQ安全机制
只有符合认证的用户才能进行发送和接收消息

<plugins>
    <simpleAuthenticationPlugin>
        <users>
            <authenticationUser username="ysx" password="ysx" groups="users,admins"/>
        </users>
    </simpleAuthenticationPlugin>
</plugins>

在/usr/local/apache-activemq-5.15.8/conf/activemq.xml的大约123行,之前,之后加上上面的插件配置,重启ActiveMQ
按之前的程序发送消息就会报错:
image
需要对程序进行修改(生产者和消费者都要改)
image
image
image
待续。。。

相关文章
|
4天前
|
Linux 开发工具 C语言
Linux 安装 gcc 编译运行 C程序
Linux 安装 gcc 编译运行 C程序
22 0
|
4天前
|
Ubuntu Linux Python
Linux(15)Ubuntu安装ninja构建工具
Linux(15)Ubuntu安装ninja构建工具
15 0
|
7天前
|
NoSQL Linux 测试技术
Redis的安装(Linux版)
Redis的安装(Linux版)
149 1
|
17天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
90 0
|
17天前
|
Linux C语言
linux yum安装ffmpeg 图文详解
linux yum安装ffmpeg 图文详解
38 0
|
17天前
|
NoSQL Linux Redis
linux 下和win下安装redis 并添加开机自启 图文详解
linux 下和win下安装redis 并添加开机自启 图文详解
17 0
|
17天前
|
Linux
linux yum 安装rar和unrar
linux yum 安装rar和unrar
57 0
|
1天前
|
关系型数据库 MySQL Java
Linux 安装 JDK、MySQL、Tomcat(图文并茂)
Linux 安装 JDK、MySQL、Tomcat(图文并茂)
12 2
|
1天前
|
负载均衡 Java 应用服务中间件
nginx安装在linux上
nginx安装在linux上
21 2
|
3天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
41 2