[英]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.properties
: spring.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.