繁体   English   中英

SpringBoot:发送 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();

暂无
暂无

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

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