Spring Boot Embedded ActiveMQ持久消息

ale*_*oid 3 activemq-classic spring-jms spring-boot

在我的Spring Boot应用程序中,我已经配置了嵌入式Apache ActiveMQ。

@Configuration
@EnableJms
public class ActiveMQConfig {

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

}
Run Code Online (Sandbox Code Playgroud)

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

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

this.jmsMessagingTemplate.convertAndSend(this.queue, message);
Run Code Online (Sandbox Code Playgroud)

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

#ActiveMQ
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.packages.trust-all=true
Run Code Online (Sandbox Code Playgroud)

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

您能否展示一下如何使用Spring Boot配置完成它?

Has*_*our 5

默认情况下,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;
}
Run Code Online (Sandbox Code Playgroud)

要么

@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;
}
Run Code Online (Sandbox Code Playgroud)
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-kahadb-store</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)