ActiveMQ消息持久化到mysql

ActiveMQ的持久化机制包含JDBC,KahaDB(默认)、LevelDB

在activemq.xml中查看默认的broker持久化机制。

  <persistenceAdapter>
          <kahaDB directory="${activemq.data}/kahadb"/> 
        </persistenceAdapter>

改成mysql:

  <persistenceAdapter>
			<jdbcPersistenceAdapter dataSource="#mysql-mq" createTablesOnStartup="true" />
  </persistenceAdapter>

备注:createTablesOnStartup 第一次启动改为true,后面改成false,不然每一次启动都要创建数据表。

同时在broker标签外面配置mysql连接:

<bean id="mysql-mq" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true"/>

<property name="username" value="root"/>

<property name="password" value="root"/>

<property name="maxActive" value="200"/>

<property name="poolPreparedStatements" value="true"/>

</bean>

将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标示当前消息已经传送过一次,客户端需要进行消息的重复处理控制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值