【问题标题】:SpringBoot: Sending a JMS Message & terminate applicationSpring Boot:发送 JMS 消息并终止应用程序
【发布时间】:2020-07-28 21:33:54
【问题描述】:

我想使用 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 安装。

谢谢:)

【问题讨论】:

    标签: java spring-boot jms spring-jms


    【解决方案1】:

    你可以在 runner 退出后 close() 应用上下文...

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

    【讨论】:

      【解决方案2】:

      这是解决方案:

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

      【讨论】:

        猜你喜欢
        • 2020-04-05
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2020-11-16
        • 1970-01-01
        • 2012-01-21
        • 1970-01-01
        • 2020-09-08
        相关资源
        最近更新 更多