4
votes

In my Spring Boot application I have configured Embedded Apache ActiveMQ.

@Configuration
@EnableJms
public class ActiveMQConfig {

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

}

In order to send the messages I use the following code:

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

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

Right now I use in-memory ActiveMQ, this is my application.properties:

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

Because I don't want to lose the messages that was already enqueued, for example during the application restarts I need to configure my Embedded ActiveMQ to persist the data.

Could you please show how it can be done with Spring Boot configuration ?

1

1 Answers

6
votes

BrokerService by default is persistent, have you done some tests ?

if you want you can define it to override :

@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;
}

or

@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>