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,启动代码,进行测试。
注意:测试效果如果没有的话,可能是对应版本的问题,请注意多加调试!