尚硅谷ActiveMQ学习笔记(5)-- ActiveMQ的消息存储与持久化
一、官网
http://activemq.apache.org/persistence
二、是什么
1、面试题
ActiveMQ持久化机制
2、说明
一句话,MQ服务器宕机,消息不会丢失的机制
三、有哪些
1、KahaDB 消息存储(默认)
迷你版的文件数据库
①、基于日志文件,从ActiveMQ5.4开始默认的持久化插件
②、官网
kahaDB官网
③、验证
④、说明
http://activemq.apache.org/kahadb
⑤、kahaDB的存储原理
2、JDBC 消息存储
消息基于JDBC存储的
3、LevelDB消息存储(了解)
4、JDBC Message store with ActiveMQ Journal
四、JDBC消息存储
1、MQ+Mysql
2、添加mysql数据库的驱动包到lib文件夹
3、jdbcPersistanceAdapter配置
4、数据库连接池配置
注意要和上面的引用ID一致
<bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://10.112.70.211:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
5、建库建表说明
activemq_msgs
activemq_acks
activemq_lock
6、代码运行验证
①、一定要开启持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
②、队列
生产者
package com.atguigu.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* @ClassName JmsProduce
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsProduce {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String queue_name = "topic_p_mysql";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queue_name);
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
for (int i = 1; i < 3; i++) {
TextMessage textMessage = session.createTextMessage("mag2..." + i);
producer.send(textMessage);
}
producer.close();
// session.commit();
session.close();
connection.close();
System.out.println("....");
}
}
消费者
package com.atguigu.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
/**
* @ClassName JmsConsumer
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsConsumer {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String queue_name = "topic_p_mysql";
public static void main(String[] args) throws JMSException, IOException, InterruptedException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false,Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = session.createQueue(queue_name);
MessageConsumer consumer = session.createConsumer(queue);
while (true){
TextMessage receive = (TextMessage) consumer.receive(1000);
if (null != receive){
System.out.println("消息:"+receive.getText());
}
else {
System.out.println("结束");
break;
}
}
// System.in.read();
// Thread.sleep(1000);
consumer.close();
// session.commit();
session.close();
connection.close();
System.out.println("完成....");
}
}
③、topic
生产者
package com.atguigu.activemq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* @ClassName JmsProduce
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsProduce_Topic_p {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String topic_name = "topic_p_mysql";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topic_name);
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
for (int i = 4; i <=6; i++) {
TextMessage textMessage = session.createTextMessage("Topic mag..." + i);
producer.send(textMessage);
}
producer.close();
session.close();
connection.close();
System.out.println("完成....");
}
}
消费者
package com.atguigu.activemq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.component.test.TestComponent;
import javax.jms.*;
import java.io.IOException;
/**
* @ClassName JmsConsumer
* @Description TODO
* @Author DuanYueFeng
* @Version 1.0
**/
public class JmsConsumer_Topic_p {
public static final String URL = "tcp://10.112.70.211:61616";
public static final String topic_name = "topic_p_mysql";
public static void main(String[] args) throws JMSException, IOException, InterruptedException {
System.out.println("我是3号消费者。。。");
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
Connection connection = factory.createConnection();
connection.setClientID("z3");
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topic_name);
TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, "remark..");
connection.start();
Message message = durableSubscriber.receive();
while (null!=message){
TextMessage textMessage = (TextMessage)message;
System.out.println("收到的持久化:"+textMessage.getText());
message = durableSubscriber.receive(1000L);
}
// Thread.sleep(1000);
session.close();
connection.close();
System.out.println("完成....");
}
}
7、数据库情况
8、小总结
9、开发有坑
五、JDBC Message store with ActiveMQ Journal
六、ActiveMQ持久化机制小总结
本文来自博客园,作者:暗影月色程序猿,转载请注明原文链接:https://www.cnblogs.com/Geneling/p/15249058.html