3、activemq 持久化|8月更文挑战

这是我参与8月更文挑战的第3天,活动详情查看:8月更文挑战” ​java

这里用mysql进行持久化,其余的都差很少mysql

第一步:将MySQL的数据库驱动复制到activeMQ的lib目录下 (须要jar包找我)

第二步:在${activemq.base}/conf/activemq.xml文件中配置持久化适配器

createTablesOnStartup="false" 建立表sql

useDatabaseLock="false" 上锁(问题2)数据库

<persistenceAdapter>

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

</persistenceAdapter>
复制代码

第三步:在${activemq.base}/conf/activemq.xml文件中配置数据源

<bean id="derby-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">

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

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

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

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

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

</bean>
复制代码

配置会出现的问题:apache

1. Caused by: org.xml.sax.SAXParseException; lineNumber: 92; columnNumber: 92; cvc-complex-type.2.4.a: 发现了以 元素 'bean' 开头的无效内容。应以 '{"activemq.apache.org/schema/core…' 之一开头。

解决方案:markdown

把bean放到 bean 标签那块session

2. Failed to acquire lock. Sleeping for 10000 milli(s) before trying again...

解决方案:app

useDatabaseLock="false" 配置oop

<persistenceAdapter>

<jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#derby-ds" useDatabaseLock="false" />

</persistenceAdapter>
复制代码

3.ActiveMQ添加了mysql的持久化后,发了消息,可是MSGS表中没有记录

 1.持久化之后 activemq数据库 会建立3张表post

在 producer 生产者 中须要设置

producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//            producer.setTimeToLive(10);

//            发送
            producer.send(textMessage);
//            producer.send(textMessage, DeliveryMode.PERSISTENT, 1, 60 * 60 * 24);
复制代码

在consumer 消费者中 添加 注意 设置客户端id 须要在开启连接 前面

//设置客户端id

//设置客户端id
connection.setClientID("client-1");

connection.start();

// final MessageConsumer messageConsumer = session.createConsumer(topic);//普通订阅
MessageConsumer messageConsumer = session.createDurableSubscriber(topic,"bb"); //持久订阅
复制代码

代码:

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author Yang
 * 描述: 生产者
 */
public class TopicProducer {

    /**
     * 用户名
     */
    private static final String userName = ActiveMQConnection.DEFAULT_USER;
    /**
     * 密码
     */
    private static final String passWord = ActiveMQConnection.DEFAULT_PASSWORD;
    /**
     * url
     */
    private static final String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;

    public void send(String message) {


        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(userName, passWord, brokerUrl);

            final Connection connection = connectionFactory.createConnection();

            connection.start();

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


            //建立队列
            final Topic topic = session.createTopic("topic");
            final MessageProducer producer = session.createProducer(topic);

            TextMessage textMessage = session.createTextMessage(message);

            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//            producer.setTimeToLive(10);

//            发送
            producer.send(textMessage);
//            producer.send(textMessage, DeliveryMode.PERSISTENT, 1, 60 * 60 * 24);

//            session.commit();

            producer.close();

            session.close();

            connection.close();

        } catch (JMSException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        TopicProducer producer = new TopicProducer();
        producer.send("hello world");

    }


}
复制代码

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author Yang
 * 描述: 消费者
 */


public class TopicConsumer {

    /**
     * 用户名
     */
    private static final String userName = ActiveMQConnection.DEFAULT_USER;
    /**
     * 密码
     */
    private static final String passWord = ActiveMQConnection.DEFAULT_PASSWORD;
    /**
     * url
     */
    private static final String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;

    public void receive() {


        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(userName, passWord, brokerUrl);

            final Connection connection = connectionFactory.createConnection();
             //设置客户端id
            connection.setClientID("client-1");

            connection.start();

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

            //建立队列
            final Topic topic = session.createTopic("topic");

//            final MessageConsumer messageConsumer = session.createConsumer(topic);//普通订阅
            MessageConsumer messageConsumer = session.createDurableSubscriber(topic,"bb"); //持久订阅

            messageConsumer.setMessageListener(n -> {

                try {
                    TextMessage msg = (TextMessage) n;

                    final String text = msg.getText();

                    if (text.equalsIgnoreCase("hello world")) {
                        System.out.println("               接受信息:         " + msg.getText());
                    } else {
                        System.out.println("     测试重发次数 ");
                        int i = 1 / 0;
                    }


                } catch (JMSException e) {
//                    e.printStackTrace();
                }

            });

        } catch (JMSException e) {
//            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        TopicConsumer consumer = new TopicConsumer();
        consumer.receive();
    }


}
复制代码