activeMQ持久化到数据库配置

1.修改conf/activemq.xml文件,添加一下配置

<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#dataSource"/>
</persistenceAdapter>

 
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/activeMQ?relaxAutoCommit=true"></property>
        <property name="username" value="root"></property>
        <property name="password" value="****"></property>
        <property name="poolPreparedStatements" value="true"></property>
</bean>
2.将dbcp和jdbc驱动拷到activeMQ的lib目录下
3.以下代码实现
package com.suobei.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;

/**
 * topic消息持久化订阅,持久到mysql
 * Created by wangmin on 2018/3/12 0012.
 */
public class TopicPersistentMysqlTest {

    /**
     * 编写消息的发送方,生产者
     */
    @Test
    public void test1() throws JMSException {
        //1.创建连接工厂对象
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://www.itroot.top:61616");
        //2.获取连接对象
        Connection connection = connectionFactory.createConnection();
        System.out.println(connection);
        //3.连接MQ服务
        connection.start();
        //4.获得session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.通过session创建主题
        Topic topic = session.createTopic("itRootTopic");
        //6.通过session对象创建消息的消费者
        MessageProducer messageProducer = session.createProducer(topic);
        //7.创建一条消息
        TextMessage textMessage = session.createTextMessage("起床了...啦啦啦");
        //8.发送消息
        messageProducer.send(textMessage,DeliveryMode.PERSISTENT,1,1000*60*60*24);

        //关闭资源
        messageProducer.close();
        session.close();
        connection.close();
    }

    /**
     * 消息的消费者,接收方
     * @throws JMSException
     */
    @Test
    public void test2() throws JMSException {
        //1.创建连接工厂对象
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://www.itroot.top:61616");
        //2.获取连接对象
        Connection connection = connectionFactory.createConnection();
        //设置客户端id
        connection.setClientID("client-1");
        //3.连接MQ服务
        connection.start();
        //4.获得session
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.通过session创建主题
        Topic topic = session.createTopic("itRootTopic");
        //6.通过session对象创建持久化消息的消费者
        TopicSubscriber consumer = session.createDurableSubscriber(topic, "client1-sub");
        consumer.setMessageListener(message -> {
            //当我们监听的topic中存在消息时,这个方法自动执行
            TextMessage textMessage= (TextMessage) message;
            try {
                System.out.println("接收到消息:"+textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        });
        while (true){

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值