简体   繁体   English

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

[英]SpringBoot: Sending a JMS Message & terminate application

I want to use SpringBoot to send a message to an ActiveMQ Queue.我想使用 SpringBoot 向 ActiveMQ 队列发送消息。 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:在没有任何父级的情况下使用以下依赖项(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>

And a single line application.properties : spring.activemq.broker-url=failover:(tcp://localhost:61616)和单行application.propertiesspring.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.线程转储显示, ActiveMQ Transport: tcp://localhost/127.0.0.1:61616线程正在运行。

Do I need a different ConnectionFactory ?我需要不同的ConnectionFactory吗? Or what can I do to terminate the application right after sending the message?或者我可以做些什么来在发送消息后立即终止应用程序?

Note: Without the JMS content, the application terminates.注意:没有 JMS 内容,应用程序将终止。 Note: I'm using a standard ActiveMQ installation.注意:我使用的是标准 ActiveMQ 安装。

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...您可以在跑步者退出后close()应用程序上下文......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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