gpt4 book ai didi

java - Spring应用程序不使用JmsTemplate退出

转载 作者:行者123 更新时间:2023-12-02 06:20:55 26 4
gpt4 key购买 nike

我正在集成 Spring-4 和 Active-MQ 5.8。我写了一些小测试代码。它正确发送消息但不退出。

发件人代码是

package sample.jms.activemq;

import java.util.Map;

import org.springframework.jms.core.JmsTemplate;

public class MessageSender {

private final JmsTemplate jmsTemplate;

public MessageSender(final JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void send(final Map map) {
jmsTemplate.convertAndSend(map);
}

}

** 配置文件是 **

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Activemq connection factory -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="pass"/>
<constructor-arg index="1" value="pass"/>
<constructor-arg index="2" value="tcp://queueURL:61616"/>
</bean>

<!-- ConnectionFactory Definition -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
</bean>


<!-- Default Destination Queue Definition-->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="simplesample"/>
</bean>

<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="defaultDestination" />
</bean>

<!-- Message Sender Definition -->
<bean id="messageSender" class="sample.jms.activemq.MessageSender">
<constructor-arg index="0" ref="jmsTemplate" />
</bean>

</beans>

** 主类代码是**

package sample.jms.activemq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
*/
public class JMSMain {

public static void main(String a[]) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"/sample/jms/resources/Spring-Module.xml");
MessageSender sender = (MessageSender) context.getBean("messageSender");
Map<String, String> map = new HashMap<String, String>();
map.put("Name", "SampleName");
sender.send(map);
System.out.println("Exiting");
context.registerShutdownHook();

}
}

最佳答案

尝试将 tcp://queueURL:61616?daemon=true 添加到您的配置

JMS 只是不断监听消息,因此要关闭它应该是守护进程。

关于java - Spring应用程序不使用JmsTemplate退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21042851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com