美文网首页
ActiveMQ 安装及使用过程

ActiveMQ 安装及使用过程

作者: 冬天里的懒喵 | 来源:发表于2021-10-28 14:51 被阅读0次

1.安装jdk

在CentOS操作系统中,可以下载rpm包直接安装。

[root@m161p114 ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.9.2009 (Core)
Release:    7.9.2009
Codename:   Core
[root@m161p114 ~]# 

现在下载jdk的rpm包。
jdk-8u311-linux-x64.rpm

直接安装即可:

[root@m161p114 software]# rpm -ivh jdk-8u311-linux-x64.rpm 
warning: jdk-8u311-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8-2000:1.8.0_311-fcs        ################################# [100%]
Unpacking JAR files...
    tools.jar...
    plugin.jar...
    javaws.jar...
    deploy.jar...
    rt.jar...
    jsse.jar...
    charsets.jar...
    localedata.jar...
[root@m161p114 software]# 

如果需要使用到多个版本的jdk,那么不建议通过rpm的方式来安装,最好安装tar.gz版本,然后在环境变量中配置。

2.active 安装及启动

activeMQ的安装非常简单,在安装好jdk之后,只需要将activeMQ的压缩包上传解压即可。
从apache下载最新的activeMQ。
apache-activemq-5.16.3-bin.tar.gz
activeMQ有两个分支,我们在此选择了传统的分支。

[root@m161p114 opt]# tar -zxvf /opt/software/apache-activemq-5.16.3-bin.tar.gz 
apache-activemq-5.16.3/
apache-activemq-5.16.3/lib/
apache-activemq-5.16.3/docs/
apache-activemq-5.16.3/examples/
apache-activemq-5.16.3/examples/openwire/
apache-activemq-5.16.3/examples/openwire/csharp/
apache-activemq-5.16.3/examples/openwire/csharp/ActiveMQExamples/
apache-activemq-5.16.3/examples/openwire/csharp/ActiveMQExamples/Listener/
apache-activemq-5.16.3/examples/openwire/csharp/ActiveMQExamples/Publisher/
apache-activemq-5.16.3/examples/openwire/advanced-scenarios/

... ...

解压之后就可以通过如下方式启动:

[root@m161p114 apache-activemq-5.16.3]# bin/activemq start
INFO: Loading '/opt/apache-activemq-5.16.3//bin/env'
INFO: Using java '/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/opt/apache-activemq-5.16.3//data/activemq.pid' (pid '14540')
[root@m161p114 apache-activemq-5.16.3]# bin/activemq status
INFO: Loading '/opt/apache-activemq-5.16.3//bin/env'
INFO: Using java '/bin/java'
ActiveMQ is running (pid '14540')
[root@m161p114 apache-activemq-5.16.3]# 

这样activeMQ启动成功。

activeMQ的管理后台在jetty中进行配置,默认端口为8161。不过需要注意的是,activeMQ的管理后台监听为127.0.0.1。需要配置为0.0.0.0.
该配置为于jetty.xml中。这个host需要改为0.0.0.0。


    <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
             <!-- the default port number for the web console -->
        <property name="host" value="0.0.0.0"/>
        <property name="port" value="8161"/>
    </bean>

之后重启activeMQ:

[root@m161p114 apache-activemq-5.16.3]# bin/activemq restart
INFO: Loading '/opt/apache-activemq-5.16.3//bin/env'
INFO: Using java '/bin/java'
INFO: Loading '/opt/apache-activemq-5.16.3//bin/env'
INFO: Using java '/bin/java'
INFO: Waiting at least 30 seconds for regular process termination of pid '14540' : 
Java Runtime: Oracle Corporation 1.8.0_311 /usr/java/jdk1.8.0_311-amd64/jre
  Heap sizes: current=62976k  free=61992k  max=932352k
    JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/opt/apache-activemq-5.16.3//conf/login.config -Dactivemq.classpath=/opt/apache-activemq-5.16.3//conf:/opt/apache-activemq-5.16.3//../lib/: -Dactivemq.home=/opt/apache-activemq-5.16.3/ -Dactivemq.base=/opt/apache-activemq-5.16.3/ -Dactivemq.conf=/opt/apache-activemq-5.16.3//conf -Dactivemq.data=/opt/apache-activemq-5.16.3//data
Extensions classpath:
  [/opt/apache-activemq-5.16.3/lib,/opt/apache-activemq-5.16.3/lib/camel,/opt/apache-activemq-5.16.3/lib/optional,/opt/apache-activemq-5.16.3/lib/web,/opt/apache-activemq-5.16.3/lib/extra]
ACTIVEMQ_HOME: /opt/apache-activemq-5.16.3

通过http://192.168.161.114:8161/ 就能访问activeMQ的管理界面。

管理界面需要用户和密码,位于 jetty-realm.properties文件中:

# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: user, user

可以通过admin和user用户登陆访问。

3.java客户端

现在可以通过java代码进行连接测试。
需要导入的包有:

 <!-- activeMQ -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.16.3</version>
        </dependency>

这样就导入了activeMQ的依赖的包,现在通过如下代码可以对activeMQ进行操作。

package com.dhb.activemq.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
public class ActiveMQTest {

    public static void main(String[] args) {
        Destination destination = new ActiveMQTopic("test.topic");
        testDestination(destination);
    }
    
    public static void testDestination(Destination destination) {
        try {
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.161.114:61616");
            ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection();
            conn.start();
            Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // 创建消费者
            MessageConsumer consumer = session.createConsumer( destination );
            final AtomicInteger count = new AtomicInteger(0);
            MessageListener listener = new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        // 打印所有的消息内容
                        // Thread.sleep();
                        log.info(count.incrementAndGet() + " => receive from " + destination.toString() + ": " + message);
                        // message.acknowledge(); // 前面所有未被确认的消息全部都确认。

                    } catch (Exception e) {
                        e.printStackTrace(); // 不要吞任何这里的异常,
                    }
                }
            };
            // 绑定消息监听器
            consumer.setMessageListener(listener);
            
            // 创建生产者,生产100个消息
            MessageProducer producer = session.createProducer(destination);
            int index = 0;
            while (index++ < 100) {
                TextMessage message = session.createTextMessage(index + " message.");
                producer.send(message);
            }

            Thread.sleep(20000);
            session.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

执行上述代码,可以完成对activeMQ的相关操作。

相关文章

网友评论

      本文标题:ActiveMQ 安装及使用过程

      本文链接:https://www.haomeiwen.com/subject/uhjvaltx.html