1.3-ActiveMQ消息持久化(队列)

  • Post author:
  • Post category:其他




ActiveMQ消息持久化(队列)

生产者 ActiveMQMessageProducer (继承自 ActiveMQMessageProducerSupport)下有一个 defaultDeliveryMode (默认传送模式)属性,通过以下方法可以设置生产者生产的消息默认是否持久化。

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
package javax.jms;
public interface DeliveryMode {
    int NON_PERSISTENT = 1;    //非持久化
    int PERSISTENT = 2;        //持久化
}



队列模式下测试非持久化:

①启动service.queue.QueueProducer

访问

http://192.168.136.135:8161/admin/queues.jsp

可以看到一共生产100条消息



②此时停止 activemq 服务

[root@localhost bin]# pwd
/usr/local/apache-activemq-5.16.1/bin
[root@localhost bin]# ./activemq stop
INFO: Loading '/usr/local/apache-activemq-5.16.1//bin/env'
INFO: Using java '/usr/local/jdk/bin/java'
INFO: Waiting at least 30 seconds for regular process termination of pid '16775' :
Java Runtime: Oracle Corporation 1.8.0_144 /usr/local/jdk/jre
  Heap sizes: current=63360k  free=62303k  max=1013632k
    JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/usr/local/apache-activemq-5.16.1//conf/login.config -Dactivemq.classpath=/usr/local/apache-activemq-5.16.1//conf:/usr/local/apache-activemq-5.16.1//../lib/: -Dactivemq.home=/usr/local/apache-activemq-5.16.1/ -Dactivemq.base=/usr/local/apache-activemq-5.16.1/ -Dactivemq.conf=/usr/local/apache-activemq-5.16.1//conf -Dactivemq.data=/usr/local/apache-activemq-5.16.1//data
Extensions classpath:
  [/usr/local/apache-activemq-5.16.1/lib,/usr/local/apache-activemq-5.16.1/lib/camel,/usr/local/apache-activemq-5.16.1/lib/optional,/usr/local/apache-activemq-5.16.1/lib/web,/usr/local/apache-activemq-5.16.1/lib/extra]
ACTIVEMQ_HOME: /usr/local/apache-activemq-5.16.1
ACTIVEMQ_BASE: /usr/local/apache-activemq-5.16.1
ACTIVEMQ_CONF: /usr/local/apache-activemq-5.16.1/conf
ACTIVEMQ_DATA: /usr/local/apache-activemq-5.16.1/data
Connecting to pid: 16775
.Stopping broker: localhost
. TERMINATED

③启动后再次访问 activemq 后台管理地址

[root@localhost bin]# ./activemq start



可见消息丢失



队列模式下测试持久化:

更改持久化模式

producer.setDeliveryMode(DeliveryMode.PERSISTENT);

①启动service.queue.QueueProducer

访问 [http://192.168.136.135:8161/admin/queues.jsp]



②停止 activemq 服务后再启动



可见待发送消息还是100条,但是入队消息变为 0 。



版权声明:本文为sql2008help原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。