ActiveMq数据持久化进Mysql

1.activeMq配置文件/usr/local/activemq/conf/activemq.xml 下方注意红色修改

  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

   
   
       
            file:${activemq.conf}/credentials.properties
       

   

  
              lazy-init="false" scope="singleton"
          init-method="start" destroy-method="stop">
   

   
   

       
           
             
               
                   
                 
                   
                 

               

             

           

       


       
       
           
       

       
       
        
       


         
         
           
               
                   
               

               
                   
               

               
                   
               

           

       

       
       
           
            douzi8:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            douzi8:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            douzi8:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            douzi8:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            douzi8:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
       

       
       
           
       

   


    
        
        
        
        
        
        
        
        
        
    

   
   

2./usr/local/activemq/lib/ 下放mysql-connector-java-5.1.47.jar

3.mysql中建立persistence_mq数据库

4.执行程序,只发不接即可看到数据库activemq_msgs中存在数据了。

5.spring boot 项目使用

# activeMq配置
#spring.activemq.broker-url=failover:(tcp://douzi8:61616)
spring.activemq.broker-url=tcp://douzi8:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
# true表示使用连接池
spring.activemq.pool.enabled=false
# 连接池最大连接数
spring.activemq.pool.max-connections=10
# 空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000
# 强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0
# 当有"JMSException"时尝试重新连接
spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true

queue.sync=douzi-queue

import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

/**
 *

ActiveMQConfig Description: activemq配置


 * @author douzi
 * DATE 2020年3月10日 上午10:27:22
 */
@Configuration
@EnableJms
public class ActiveMQConfig {
    @Value("${queue.sync}")
    private String syncQueue;
    @Value("${spring.activemq.user}")
    private String usrName;

    @Value("${spring.activemq.password}")
    private  String password;

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;

    @Bean
    public Queue syncQueue(){
        return new ActiveMQQueue(syncQueue);
    }

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(false);
        factory.setConnectionFactory(connectionFactory);
        // 开启持久化
//        factory.setSubscriptionDurable(true);
        return factory;
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        // 开启持久化
//        bean.setSubscriptionDurable(true);
        return bean;
    }
}

生产者:

import java.util.Random;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.IntStream;

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import com.wondertek.moms.common.utils.MyThreadPool;

import lombok.extern.slf4j.Slf4j;

@Slf4j
//@Component
public class ProducerTest implements CommandLineRunner {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue syncQueue;
    
    private final static Random r = new Random(System.currentTimeMillis());

    @Override
    public void run(String... args) throws Exception {
        log.info("Message was sent to the Queue");

        for (int j = 0; j < 5; j++) {
            new Thread(() ->forData(), "t" + j).start();
        }
    }
    
    public void forData() {
        for (int i = 0; i < 10000; i++) {
            try {
                Thread.sleep(r.nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            send("douzi " + Thread.currentThread().getName() + " " + i);
        }
    }

    public void send(String msg) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.jmsMessagingTemplate.convertAndSend(this.syncQueue, msg);
    }

}

消费者:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

//@Component
@Slf4j
public class ConsumerTest {

    @JmsListener(destination = "${queue.sync}")
    public void receiveQueue(String text) {
        log.info("------- 1:" + text);
    }
    
    @JmsListener(destination = "${queue.sync}")
    public void receiveQueue1(String text) {
        log.info("------- 2:" + text);
    }
}

 

你可能感兴趣的:(技术贴)