Spring application does not exit using JmsTemplate

652 views Asked by At

I am integrating Spring-4 and Active-MQ 5.8. I have written small test code. It send message properly but it does not exit.

The Sender Code is

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);
    }

}

** Configuration file is **

<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> 

** The Main Class code is **

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

There are 1 answers

0
StanislavL On BEST ANSWER

Try to add tcp://queueURL:61616?daemon=true to you config

The JMS just keeps listening messages so to close it it should be daemon.