Spring 启动嵌入式 ActiveMQ 持久消息

Spring Boot Embedded ActiveMQ durable messages

在我的 Spring 引导应用程序中,我配置了嵌入式 Apache ActiveMQ。

@Configuration
@EnableJms
public class ActiveMQConfig {

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("import.decisions.queue");
    }

}

为了发送消息,我使用以下代码:

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

this.jmsMessagingTemplate.convertAndSend(this.queue, message);

现在我使用内存中的 ActiveMQ,这是我的 application.properties:

#ActiveMQ
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.packages.trust-all=true

因为我不想丢失已经排队的消息,例如在应用程序重新启动期间,我需要配置我的嵌入式 ActiveMQ 以保留数据。

能否请您展示一下如何使用 Spring 启动配置来完成?

BrokerService 默认是持久化的,你做过测试吗?

如果您愿意,可以将其定义为覆盖:

@Bean(initMethod = "start", destroyMethod = "stop")
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();
    //broker.addConnector("tcp://localhost:61616");
    broker.addConnector("vm://localhost");
    PersistenceAdapter persistenceAdapter = new KahaDBPersistenceAdapter();
    File dir = new File(System.getProperty("user.home") + File.separator + "kaha");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    persistenceAdapter.setDirectory(dir);
    broker.setPersistenceAdapter(persistenceAdapter);
    broker.setPersistent(true);
    return broker;
}

@Bean(initMethod = "start", destroyMethod = "stop")
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();
    //broker.addConnector("tcp://localhost:61616");
    broker.addConnector("vm://localhost");
    broker.setPersistent(true);
    // default messages store is under AMQ_HOME/data/KahaDB/
    return broker;
}
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-kahadb-store</artifactId>
</dependency>