ActiveMQ消息丟失問題測試以及解決

ActiveMQ消息丟失問題

一、基本環境準備與搭建

首先搭建環境,兩臺ActiveMQ,然後採用的是靜態鏈接的方式。下面是演示過程:

[root@activemq software]# ll
total 61200
-rw-r--r--. 1 root root 62666908 May 29 11:09 apache-activemq-5.15.12-bin.tar.gz
### 解壓
[root@activemq software]# tar -zxvf apache-activemq-5.15.12-bin.tar.gz 
apache-activemq-5.15.12/bin/activemq.jar
apache-activemq-5.15.12/activemq-all-5.15.12.jar
apache-activemq-5.15.12/lib/slf4j-api-1.7.25.jar
apache-activemq-5.15.12/lib/activemq-spring-5.15.12.jar
apache-activemq-5.15.12/lib/activemq-broker-5.15.12.jar
apache-activemq-5.15.12/lib/activemq-client-5.15.12.jar
...
[root@activemq software]# mv apache-activemq-5.15.12 activemq
[root@activemq software]# ll
total 61200
drwxr-xr-x. 10 root root      194 Mar 13 19:15 activemq
-rw-r--r--.  1 root root 62666908 May 29 11:09 apache-activemq-5.15.12-bin.tar.gz
[root@activemq software]# ll
total 61200
drwxr-xr-x. 10 root root      194 Mar 13 19:15 activemq
-rw-r--r--.  1 root root 62666908 May 29 11:09 apache-activemq-5.15.12-bin.tar.gz
[root@activemq software]# 
[root@activemq software]# 
[root@activemq software]# cd activemq/
[root@activemq activemq]# ll
total 18196
-rwxr-xr-x. 1 root root 18574637 Mar 13 18:44 activemq-all-5.15.12.jar
drwxr-xr-x. 5 root root      147 May 31 04:15 bin
drwxr-xr-x. 2 root root     4096 May 31 04:15 conf
drwxr-xr-x. 2 root root       26 May 31 04:15 data
drwxr-xr-x. 2 root root       76 May 31 04:15 docs
drwxr-xr-x. 7 root root       71 Mar 13 19:15 examples
drwxr-xr-x. 6 root root     4096 May 31 04:15 lib
-rw-r--r--. 1 root root    40580 Mar 13 19:15 LICENSE
-rw-r--r--. 1 root root     3334 Mar 13 19:15 NOTICE
-rw-r--r--. 1 root root     2610 Mar 13 19:15 README.txt
drwxr-xr-x. 6 root root       95 May 31 04:15 webapps
drwxr-xr-x. 3 root root       18 May 31 04:15 webapps-demo
[root@activemq activemq]# cp -r conf/ conf2
[root@activemq activemq]# ll
total 18200
-rwxr-xr-x. 1 root root 18574637 Mar 13 18:44 activemq-all-5.15.12.jar
drwxr-xr-x. 5 root root      147 May 31 04:15 bin
drwxr-xr-x. 2 root root     4096 May 31 04:15 conf
drwxr-xr-x. 2 root root     4096 May 31 04:16 conf2
drwxr-xr-x. 2 root root       26 May 31 04:15 data
drwxr-xr-x. 2 root root       76 May 31 04:15 docs
drwxr-xr-x. 7 root root       71 Mar 13 19:15 examples
drwxr-xr-x. 6 root root     4096 May 31 04:15 lib
-rw-r--r--. 1 root root    40580 Mar 13 19:15 LICENSE
-rw-r--r--. 1 root root     3334 Mar 13 19:15 NOTICE
-rw-r--r--. 1 root root     2610 Mar 13 19:15 README.txt
drwxr-xr-x. 6 root root       95 May 31 04:15 webapps
drwxr-xr-x. 3 root root       18 May 31 04:15 webapps-demo
[root@activemq activemq]# cd conf2
[root@activemq conf2]# ll
total 84
-rw-r--r--. 1 root root 5911 May 31 04:16 activemq.xml
-rw-r--r--. 1 root root 1370 May 31 04:16 broker.ks
-rw-r--r--. 1 root root  592 May 31 04:16 broker-localhost.cert
-rw-r--r--. 1 root root  665 May 31 04:16 broker.ts
-rw-r--r--. 1 root root 1357 May 31 04:16 client.ks
-rw-r--r--. 1 root root  665 May 31 04:16 client.ts
-rw-r--r--. 1 root root 1172 May 31 04:16 credentials-enc.properties
-rw-r--r--. 1 root root 1121 May 31 04:16 credentials.properties
-rw-r--r--. 1 root root  962 May 31 04:16 groups.properties
-rw-r--r--. 1 root root 1011 May 31 04:16 java.security
-rw-r--r--. 1 root root 1087 May 31 04:16 jetty-realm.properties
-rw-r--r--. 1 root root 8407 May 31 04:16 jetty.xml
-rw-r--r--. 1 root root  965 May 31 04:16 jmx.access
-rw-r--r--. 1 root root  964 May 31 04:16 jmx.password
-rw-r--r--. 1 root root 3628 May 31 04:16 log4j.properties
-rw-r--r--. 1 root root 1207 May 31 04:16 logging.properties
-rw-r--r--. 1 root root 1016 May 31 04:16 login.config
-rw-r--r--. 1 root root  961 May 31 04:16 users.properties
[root@activemq conf2]# 
[root@activemq conf2]# 
[root@activemq conf2]# 
## 修改brokerName,修改默認持久化位置,修改相關設置端口
[root@activemq conf2]# vim activemq.xml 
## 修改端口8161爲8191
[root@activemq conf2]# vim jetty.xml 
[root@activemq conf2]# cd ..
[root@activemq activemq]# ll
total 18200
-rwxr-xr-x. 1 root root 18574637 Mar 13 18:44 activemq-all-5.15.12.jar
drwxr-xr-x. 5 root root      147 May 31 04:15 bin
drwxr-xr-x. 2 root root     4096 May 31 04:15 conf
drwxr-xr-x. 2 root root     4096 May 31 04:19 conf2
drwxr-xr-x. 2 root root       26 May 31 04:15 data
drwxr-xr-x. 2 root root       76 May 31 04:15 docs
drwxr-xr-x. 7 root root       71 Mar 13 19:15 examples
drwxr-xr-x. 6 root root     4096 May 31 04:15 lib
-rw-r--r--. 1 root root    40580 Mar 13 19:15 LICENSE
-rw-r--r--. 1 root root     3334 Mar 13 19:15 NOTICE
-rw-r--r--. 1 root root     2610 Mar 13 19:15 README.txt
drwxr-xr-x. 6 root root       95 May 31 04:15 webapps
drwxr-xr-x. 3 root root       18 May 31 04:15 webapps-demo
[root@activemq activemq]# cd bin/
[root@activemq bin]# ll
total 144
-rwxr-xr-x. 1 root root 21535 Mar 13 19:15 activemq
-rwxr-xr-x. 1 root root  6189 Mar 13 19:15 activemq-diag
-rw-r--r--. 1 root root 16405 Mar 13 18:56 activemq.jar
-rw-r--r--. 1 root root  5607 Mar 13 19:15 env
drwxr-xr-x. 2 root root    78 May 31 04:15 linux-x86-32
drwxr-xr-x. 2 root root    78 May 31 04:15 linux-x86-64
drwxr-xr-x. 2 root root    82 May 31 04:15 macosx
-rw-r--r--. 1 root root 83820 Mar 13 18:15 wrapper.jar
[root@activemq bin]# cp activemq activemq2
[root@activemq bin]# ll
total 168
-rwxr-xr-x. 1 root root 21535 Mar 13 19:15 activemq
-rwxr-xr-x. 1 root root 21535 May 31 04:19 activemq2
-rwxr-xr-x. 1 root root  6189 Mar 13 19:15 activemq-diag
-rw-r--r--. 1 root root 16405 Mar 13 18:56 activemq.jar
-rw-r--r--. 1 root root  5607 Mar 13 19:15 env
drwxr-xr-x. 2 root root    78 May 31 04:15 linux-x86-32
drwxr-xr-x. 2 root root    78 May 31 04:15 linux-x86-64
drwxr-xr-x. 2 root root    82 May 31 04:15 macosx
-rw-r--r--. 1 root root 83820 Mar 13 18:15 wrapper.jar
## 修改pid;修改env文件路徑中的env爲env2;修改配置文件路徑位置中的conf爲conf2
[root@activemq bin]# vim activemq2 
[root@activemq bin]# 
[root@activemq bin]# 
[root@activemq bin]# cp env env2
## 將對應的env2裏面的61616修改爲conf2/activemq.xml配置的tcp端口
[root@activemq bin]# vim env2 
[root@activemq bin]# 
## 啓動測試
[root@activemq bin]# ./activemq start
INFO: Loading '/opt/software/activemq//bin/env'
INFO: Using java '/usr/local/software/jdk/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/opt/software/activemq//data/activemq.pid' (pid '3779')
[root@activemq bin]# ./activemq2 start
INFO: Loading '/opt/software/activemq//bin/env2'
INFO: Using java '/usr/local/software/jdk/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/opt/software/activemq//data/activemq2.pid' (pid '3847')
## 驗證啓動成功
[root@activemq bin]# ps -ef | grep activemq
avahi       642      1  0 03:41 ?        00:00:00 avahi-daemon: running [activemq.local]
root       3779      1 32 04:22 pts/0    00:00:03 /usr/local/software/jdk/bin/java -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/opt/software/activemq//conf/login.config -Dcom.sun.management.jmxremote -Djava.awt.headless=true -Djava.io.tmpdir=/opt/software/activemq//tmp -Dactivemq.classpath=/opt/software/activemq//conf:/opt/software/activemq//../lib/: -Dactivemq.home=/opt/software/activemq/ -Dactivemq.base=/opt/software/activemq/ -Dactivemq.conf=/opt/software/activemq//conf -Dactivemq.data=/opt/software/activemq//data -jar /opt/software/activemq//bin/activemq.jar start
root       3847      1 20 04:22 pts/0    00:00:01 /usr/local/software/jdk/bin/java -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/opt/software/activemq//conf/login.config -Dcom.sun.management.jmxremote -Djava.awt.headless=true -Djava.io.tmpdir=/opt/software/activemq//tmp -Dactivemq.classpath=/opt/software/activemq//conf:/opt/software/activemq//../lib/: -Dactivemq.home=/opt/software/activemq/ -Dactivemq.base=/opt/software/activemq/ -Dactivemq.conf=/opt/software/activemq//conf -Dactivemq.data=/opt/software/activemq//data -jar /opt/software/activemq//bin/activemq.jar start
root       3861   2629  0 04:22 pts/0    00:00:00 grep --color=auto activemq
## 停止
[root@activemq bin]# ./activemq stop
INFO: Loading '/opt/software/activemq//bin/env'
INFO: Using java '/usr/local/software/jdk/bin/java'
INFO: Waiting at least 30 seconds for regular process termination of pid '3779' : 
Java Runtime: Oracle Corporation 1.8.0_181 /usr/local/software/jdk/jre
  Heap sizes: current=63360k  free=62660k  max=1013632k
    JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/opt/software/activemq//conf/login.config -Dactivemq.classpath=/opt/software/activemq//conf:/opt/software/activemq//../lib/: -Dactivemq.home=/opt/software/activemq/ -Dactivemq.base=/opt/software/activemq/ -Dactivemq.conf=/opt/software/activemq//conf -Dactivemq.data=/opt/software/activemq//data
Extensions classpath:
  [/opt/software/activemq/lib,/opt/software/activemq/lib/camel,/opt/software/activemq/lib/optional,/opt/software/activemq/lib/web,/opt/software/activemq/lib/extra]
ACTIVEMQ_HOME: /opt/software/activemq
ACTIVEMQ_BASE: /opt/software/activemq
ACTIVEMQ_CONF: /opt/software/activemq/conf
ACTIVEMQ_DATA: /opt/software/activemq/data
Connecting to pid: 3779
Stopping broker: localhost
.. TERMINATED
[root@activemq bin]# ./activemq2 stop
INFO: Loading '/opt/software/activemq//bin/env2'
INFO: Using java '/usr/local/software/jdk/bin/java'
INFO: Waiting at least 30 seconds for regular process termination of pid '3847' : 
Java Runtime: Oracle Corporation 1.8.0_181 /usr/local/software/jdk/jre
  Heap sizes: current=63360k  free=62660k  max=1013632k
    JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/opt/software/activemq//conf/login.config -Dactivemq.classpath=/opt/software/activemq//conf:/opt/software/activemq//../lib/: -Dactivemq.home=/opt/software/activemq/ -Dactivemq.base=/opt/software/activemq/ -Dactivemq.conf=/opt/software/activemq//conf -Dactivemq.data=/opt/software/activemq//data
Extensions classpath:
  [/opt/software/activemq/lib,/opt/software/activemq/lib/camel,/opt/software/activemq/lib/optional,/opt/software/activemq/lib/web,/opt/software/activemq/lib/extra]
ACTIVEMQ_HOME: /opt/software/activemq
ACTIVEMQ_BASE: /opt/software/activemq
ACTIVEMQ_CONF: /opt/software/activemq/conf
ACTIVEMQ_DATA: /opt/software/activemq/data
Connecting to pid: 3847
Stopping broker: localhost
.. TERMINATED

二、配置靜態鏈接

配置爲單向節點一—》節點二

[root@activemq conf]# pwd
/opt/software/activemq/conf
[root@activemq conf]# 
[root@activemq conf]# 
[root@activemq conf]# vim activemq.xml 

添加內容如下:

<!-- 在activemq.xml的broker節點內添加 -->
<networkConnectors>  
<!-- duplex,默認值是false,如果爲true,則既可消費又可生產消息到網絡broker -->
<networkConnector name="local network" uri="static://(tcp://192.168.31.150:61616,tcp://192.168.31.150:61617)"/>  
</networkConnectors>

然後啓動兩個activemq節點。

三、準備測試代碼

3.1 生產者代碼

package com.burgess.net;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 測試消息丟失內容的配置
 * 創建人    BurgessLee
 * 創建時間   2020/5/30
 * 描述
 */
public class Sender {


    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.31.150:61616");
        Connection conn =connFactory.createConnection();
        conn.start();
        Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        try {
            Queue queue = session.createQueue("my-queue02");

            MessageProducer producer = session.createProducer(queue);
            for (int i = 0; i < 30; i++) {
                TextMessage message = session.createTextMessage("message--" + i);
                producer.send(message);
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            session.commit();
            session.close();
            conn.close();
        }
    }

}

3.2 消費者代碼

package com.burgess.net;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 測試消息丟失內容的配置
 * 創建人    BurgessLee
 * 創建時間   2020/5/30
 * 描述
 */
public class Receiver {

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.31.150:61617");
        Connection conn = connFactory.createConnection();
        conn.start();
        Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        try {
            Queue queue = session.createQueue("my-queue02");

            MessageConsumer consumer = session.createConsumer(queue);

            for (int i = 0; i < 30; i++) {
                Message message = consumer.receive();
                String jmsType = message.getJMSType();
                System.out.println("消息類型:"+jmsType);
                TextMessage tx=(TextMessage)message;
                session.commit();
                System.out.println("收到消息:"+tx.getText());
                //每個接收一次消息睡眠2秒
                Thread.sleep(2000);
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            session.close();
            conn.close();
        }
    }

}

四、測試

測試步驟如下:

1.首先讓發送者發送消息 “->61616”,發送30個;

2.然後讓接收者接收消息"->61617",接收3個後關閉接收者.

下面是前臺看到的結果:

4.1 生產者生產30個消息

在這裏插入圖片描述

4.2 消費者接收三個關閉

在這裏插入圖片描述

啓動消費者後,發現消息從61617節點上消費獲取到消息,消費三個後,然後關閉消費者端代碼,會發現消費者嘗試從61616上獲取消息,但是失敗了。

五、解決方式

如果是簡單的將配置中的duplex屬性設置爲true。兩臺節點添加配置信息:

<!-- conf/activemq.xml -->
<destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" >
                    <!-- The constantPendingMessageLimitStrategy is used to prevent
                         slow topic consumers to block producers and affect other consumers
                         by limiting the number of messages that are retained
                         For more information, see:

                         http://activemq.apache.org/slow-consumer-handling.html

                    -->
                  <pendingMessageLimitStrategy>
                    <constantPendingMessageLimitStrategy limit="1000"/>
                  </pendingMessageLimitStrategy>
                </policyEntry>
		       <!--說明:下面內容爲新增配置-->
                <policyEntry queue=">" enableAudit="false">
                    <networkBridgeFilterFactory>
                        <conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true" />
                    </networkBridgeFilterFactory>
                </policyEntry>
              </policyEntries>
            </policyMap>
</destinationPolicy>

然後再啓動兩臺ActiveMQ,啓動代碼,進行測試。

注意:測試效果如果沒有的話,可能是對應版本的問題,請注意多加調試!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章