2
votes

I am new to this Spring Integration and JMS and i started playing with it. In here i want to create plain jms message via activemq and receive it through spring inbound adapter(message driven).

following is my spring config file

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/stream
        http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd>
        http://www.springframework.org/schema/integration/jms
        http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">

<!-- jms beans -->
<beans:bean id="jms.msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <beans:constructor-arg value="MSG_QUEUE" />
</beans:bean>

<beans:bean name="jms.connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <beans:property name="brokerURL" value="tcp://localhost:61616" />
</beans:bean>


 <!-- spring integration beans -->
<channel id="channels.jms.allMessages">
    <queue capacity="1000" />
</channel>

<jms:message-driven-channel-adapter id="adapters.jms.msgAdapter"
            connection-factory="jms.connectionFactory"
            destination="jms.msgQueue"
             channel="channels.jms.allMessages" />

and this is my testing class

package com.bst.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;

public class TestActiveMQ {

public static void main(String[] args){

    try{
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");
        ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("jms.connectionFactory");
        Destination destination = (Destination)context.getBean("jms.msgQueue");
        PollableChannel msgChannel = (PollableChannel) context.getBean("channels.jms.allMessages", PollableChannel.class );

        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);


        MessageProducer producer = session.createProducer(destination);

         TextMessage textMessage = session.createTextMessage();
         textMessage.setText("Message from JMS");
         producer.send(textMessage);


         System.out.println("--------------- Message Sending ------------------------");

         Message<?> received = msgChannel.receive();
         String payload = (String) received.getPayload();

         System.out.println("Receving message = " + payload);
    }catch(JMSException ex){
        System.out.println("----------- JMS Exception --------------");
    }

}

}

But the thing is i can not guarantee the delivery. some times the program can not receive the message and some tomes it succeeds with some warnings like

Setup of JMS message listener invoker failed for destination 'queue://MSG_QUEUE' -   trying to recover. Cause: Connection reset
Could not refresh JMS Connection for destination 'queue://MSG_QUEUE' - retrying in 5000 ms. Cause: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection refused: connect

Could not refresh JMS Connection for destination 'queue://MSG_QUEUE' - retrying in 5000 ms. Cause: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection refused: connect

This occurs few times before it succeeds.

Do you guys have any idea about this.

appreciate your help.

thanks, keth

2

2 Answers

3
votes

This just means the broker isn't running when the listener container starts. When using a tcp:// URL you should run the broker in it's own context (or another JVM) before creating this context.

2
votes

I have tested these code in my STS its working fine .

The only problem in your side is , first start message Broker (say ActiveMQ) then run your project, you can get your required output.

Thanks.