英文:
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安装。
谢谢
英文:
I want to use SpringBoot to send a message to an ActiveMQ Queue. 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:
<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)
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.
Do I need a different ConnectionFactory
? Or what can I do to terminate the application right after sending the message?
Note: Without the JMS content, the application terminates.
Note: I'm using a standard ActiveMQ installation.
Thanks
答案1
得分: 0
这是解决方案:
@Bean
public ConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
return connectionFactory;
}
英文:
This was the solution:
@Bean
public ConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
return connectionFactory;
}
答案2
得分: 0
你可以在运行程序结束后,只需调用close()
来关闭应用程序上下文。
new SpringApplicationBuilder(TestJmsClient.class).bannerMode(Mode.OFF).run(args).close();
英文:
You can just close()
the application context after the runner exits...
new SpringApplicationBuilder(TestJmsClient.class).bannerMode(Mode.OFF).run(args).close();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论