Spring应用程序不会使用JmsTemplate退出

时间:2014-01-10 11:12:29

标签: java spring activemq jmstemplate

我正在整合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();

    }
}

1 个答案:

答案 0 :(得分:1)

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

JMS只是一直在监听消息,所以关闭它应该是守护进程。