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

huangapple go评论91阅读模式
英文:

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安装。

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

英文:

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 = &quot;myQueue&quot;;

	@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(&quot;_type&quot;);
		return converter;
	}

	@Override
	public void run(String... args) throws Exception {
		jmsTemplate.convertAndSend(QUEUE_NAME, new MyObject());
	}
}

Using following dependencies (Maven) without any parent:

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-activemq&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
	&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
&lt;/dependency&gt;

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 SpringBoot:发送JMS消息并终止应用程序

答案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();

huangapple
  • 本文由 发表于 2020年7月28日 14:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定