Spring Boot:发送 JMS 消息并终止应用程序

SpringBoot: Sending a JMS Message & terminate application

我想使用 SpringBoot 向 ActiveMQ 队列发送消息。应用程序应该在发送后终止,但它保持活动状态。

这是我的申请代码:

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

在没有任何父项的情况下使用以下依赖项 (Maven):

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

还有一行application.propertiesspring.activemq.broker-url=failover:(tcp://localhost:61616)

消息已发送至队列,但应用程序并未停止。线程转储显示 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616 线程是 运行.

我需要另一个 ConnectionFactory 吗?或者如何在发送消息后立即终止应用程序?

注意:如果没有 JMS 内容,应用程序将终止。 注意:我使用的是标准的 ActiveMQ 安装。

谢谢:)

这是解决方案:

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

您可以 close() 运行程序退出后的应用程序上下文...

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