activemq mysql ha_ActiveMQ持久化到mysql实现消息永不丢失

ActiveMQ持久化到mysql实现消息永不丢失

配置

1.找到apache-activemq-5.15.2/examples/conf下面的activemq-jdbc-performance.xml

20180504174949369081.png

2.打开activemq-jdbc-performance.xml,在persistenceAdapter节点后面添加dataSource="#mysql-ds"

并配置你的数据库

其实可以直接更改apache-activemq-5.15.2/conf/activemq.xml的persistenceAdapter节点.配置下数据库也是可以的,用

用activemq-jdbc-performance.xml没有localhost:8161的管理页面,并且只能用openwire传输协议,默认是全开的,transportConnectors节点为开启的传输协议

20180504174949583933.png

3.把activemq-jdbc-performance.xml复制到apache-activemq-5.15.2/conf目录下,从命名为activemq.xml,覆盖原来的activemq.xml

20180504174949667921.png

4.在对应的数据库创建activemq库,然后重启ActiveMQ

我们这里用debug模式启动,提示没有mysql的jar包

20180504174949734330.png

5.我们在apache-activemq-5.15.2/lib下面添加mysql的jar包,再次启动,就不会报错了

20180504174949849568.png

20180504174949917930.png

6.这时可以看到刚才创建的activemq库多了三张表,说明配置成功了

20180504174950004848.png

点对点测试

生产者

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {

public static void main(String[] args) {

// String user = ActiveMQConnection.DEFAULT_USER;

// String password = ActiveMQConnection.DEFAULT_PASSWORD;

// String url = ActiveMQConnection.DEFAULT_BROKER_URL;

String subject = "test.queue";

ConnectionFactory contectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.109:61616");

// ConnectionFactory contectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

try{

Connection connection = contectionFactory.createConnection();

connection.start();

Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(subject);

MessageProducer producer = session.createProducer(destination);

// producer.setDeliveryMode(DeliveryMode.PERSISTENT);//设置为持久化

for(int i = 0; i < 20;) {

TextMessage createTextMessage = session.createTextMessage("这是要发送的第"+ ++i +"条消息消息");

producer.send(createTextMessage);

System.out.println("第"+ i +"条消息已发送");

}

Thread.sleep(2000);

session.commit();

session.close();

connection.close();

}catch (JMSException e) {

// e.printStackTrace();

}catch (InterruptedException e) {

// e.printStackTrace();

}

}

}

消费者

import java.util.Date;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.MessageListener;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Customer {

public static void main(String[] args) {

// String user = ActiveMQConnection.DEFAULT_USER;

//

// String password = ActiveMQConnection.DEFAULT_PASSWORD;

//

// String url = ActiveMQConnection.DEFAULT_BROKER_URL;

String subject = "test.queue";

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.109:61616");

// ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

Connection connection;

try {

connection= connectionFactory.createConnection();

connection.start();

final Session session =connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(subject);

MessageConsumer message = session.createConsumer(destination);

message.setMessageListener(new MessageListener() {

public void onMessage(Message msg){

TextMessage message = (TextMessage) msg;

try {

System.out.println("--收到消息:" +new Date()+message.getText());

session.commit();

}catch(JMSException e) {

// e.printStackTrace();

}

}

});

// Thread.sleep(30000);

//

// session.close();

//

// Thread.sleep(30000);

//

// connection.close();

//

// Thread.sleep(30000);

}catch(Exception e) {

// e.printStackTrace();

}

}

}

这时生产者生产数据,消费者一直不在线,数据就会持久化到数据库的activemq_msgs表,就算ActiveMQ的服务挂了,再次启动后,等消费者在线了就可以再次获取生产者生产的数据(消费之后数据库的数据会自动删除),达到不丢失的效果

20180504174950090789.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值