简体   繁体   中英

SpringBoot: Sending a JMS Message & terminate application

I want to use SpringBoot to send a message to an ActiveMQ Queue. The application should terminate after sending it, but it keeps alive.

Here is my application code:

@SpringBootApplication
public class TestJmsClient implements CommandLineRunner {

    private static final String QUEUE_NAME = "myQueue";

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        new SpringApplicationBuilder(TestJmsClient.class).bannerMode(Mode.OFF).run(args);
    }

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.BYTES);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }

    @Override
    public void run(String... args) throws Exception {
        jmsTemplate.convertAndSend(QUEUE_NAME, new MyObject());
    }
}

Using following dependencies (Maven) without any parent:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

And a single line application.properties : spring.activemq.broker-url=failover:(tcp://localhost:61616)

The message was sent to the Queue, but the application doesn't stop. A thread dump shows me, that a ActiveMQ Transport: tcp://localhost/127.0.0.1:61616 thread is running.

Do I need a different ConnectionFactory ? Or what can I do to terminate the application right after sending the message?

Note: Without the JMS content, the application terminates. Note: I'm using a standard ActiveMQ installation.

Thanks:)

This was the solution:

@Bean
public ConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    return connectionFactory;
}

You can just close() the application context after the runner exits...

new SpringApplicationBuilder(TestJmsClient.class).bannerMode(Mode.OFF).run(args).close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM