ActiveMQ的持久化机制包含JDBC,KahaDB(默认)、LevelDB
在activemq.xml中查看默认的broker持久化机制。
改成mysql:
备注:createTablesOnStartup 第一次启动改为true,后面改成false,不然每一次启动都要创建数据表。
同时在broker标签外面配置mysql连接:
将mysql的jdbc jar包放置到activemq的lib目录下。同时,将commons-pool.jar、commons-dbcp.jar和commons-collections.jar放置到activemq的lib目录下。如下:
启动activemq,因为使用了JDBC持久化方式,数据库会创建3个表:activemq_msgs,activemq_acks和activemq_lock。activemq_msgs用于存储消息,Queue和Topic都存储在这个表中。
有效期设置:
package com.queue.onetoone;
import java.awt.font.TextMeasurer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息发布者
*
* @author tlk
*
*/
public class JMSProducer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
private static final int SENDNUM = 10;
public static void main(String[] args) {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;// 消息的目的地
MessageProducer messageProducer = null; // 消息生产者
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,
PASSWORD, BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("time");
messageProducer = session.createProducer(destination);
sendMessage(session, messageProducer);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (connection != null)
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
public static void sendMessage(Session session,
MessageProducer messageProducer) {
TextMessage textMessage = null;
try {
for (int i = 0; i < SENDNUM; i++) {
textMessage = session.createTextMessage("ActiveMQ 发送的消息" + i);
System.out.println("ActiveMQ 发送的消息" + i);
// messageProducer.send(textMessage);
//第2个参数:是否持久化;第3个参数:优先级(0~4普通 5~9加急);第4个参数:消息在ActiveMQ中间件中存放的有效期
messageProducer.send(textMessage,DeliveryMode.PERSISTENT, 4, 1000*60*10);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.queue.onetoone;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者一 不断从队列出口获取消息 单对单
*
* @author tlk
*
*/
public class JMSConsumer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
public static void main(String[] args) {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;// 消息的目的地
MessageConsumer messageConsumer = null; // 消息消费者
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,
PASSWORD, BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("time");
messageConsumer = session.createConsumer(destination);
TextMessage textMessage=null;
while (true) {
textMessage=(TextMessage) messageConsumer.receive(1000);
if(textMessage!=null)
System.out.println("收到的消息:" +textMessage.getText());
else break;
}
} catch (JMSException e) {
e.printStackTrace();
}finally {
try {
if (connection != null)
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
主要是生产者这一步:
//第2个参数:是否持久化;第3个参数:优先级(0~4普通 5~9加急);第4个参数:消息在ActiveMQ中间件中存放的有效期
messageProducer.send(textMessage,DeliveryMode.PERSISTENT, 4, 1000*60*10);
若这条消息发送到了ActiveMQ消息中间件但一直未被消费,直到10分钟的时间到,消息则过期。
activemq一些注意点,也记录一下:
1,消费者的session与生产者的session签收模式保持一致,注意,生产者启用事务了,但是消费者这里事务不要启用,不然会一直消费
Session.AUTO_ACKNOWLEDGE 消息自动签收
Session.CLIENT_ACKNOWLEDGE 客戶端调用acknowledge方法手动签收
Session.DUPS_OK_ACKNOWLEDGE 不是必须签收,消息可能会重复发送。在第二次重新传送消息的时候,消息
头的JmsDelivered会被置为true标示当前消息已经传送过一次,客户端需要进行消息的重复处理控制。