2
votes

I am trying to write channel adapter for JMS queue. I'd like to send message to JMS queue and receive it on Spring Integration's channel.

When I use plain JMS (with ActiveMQ), everything works properly, so I assume bug is somewhere in my Spring code.

So here's my Spring config file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:int="http://www.springframework.org/schema/integration"
        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/jms
        http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">

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

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

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

    <jms:inbound-channel-adapter id="adapters.jms.msgAdapter"
        connection-factory="jms.connectionFactory"
        destination="jms.msgQueue"
        channel="channels.jms.allMessages">
        <int:poller fixed-rate="500" />
    </jms:inbound-channel-adapter>

</beans>

Here is plain JMS sending-receiving code that works without problems:

  @Test
    public void testPlainJms() throws JMSException {
        MessageProducer producer = session.createProducer(msgQueue);
        MessageConsumer consumer = session.createConsumer(msgQueue);

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

        connection.start();
        javax.jms.Message message = consumer.receive(TIMEOUT);
        assertEquals("Message from JMS", ((TextMessage) message).getText());
        connection.stop();
    }

And here is code using Spring's MessageChannel that usually doesn't work (it sometimes does but I am not able to determine when):

@Test
public void testInboundAdapter() throws JMSException {
    MessageProducer producer = session.createProducer(msgQueue);

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

    // receive in local channel (using inbound adapter)
    Message<?> received = ((PollableChannel) msgChannel).receive(TIMEOUT);
    String payload = (String) received.getPayload();
    assertEquals("Message from JMS", payload);
}

I am getting NullPointerException on receiving message from pollable msgChannel. Here's how I autowired beans from Spring config to my test class:

@Autowired @Qualifier("jms.msgQueue")
Queue msgQueue;

@Autowired @Qualifier("channels.jms.allMessages")
MessageChannel msgChannel;

@Autowired
ConnectionFactory connectionFactory;
2
How have you injected a session ? The documention discusses only FTP based session. static.springsource.org/spring-integration/docs/latest-ga/…luksmir

2 Answers

2
votes

Strange, I am able to reproduce your issue, could be a bug with inbound-channel-adapter, I have something which consistently works though, by changing from an inbound-channel-adapter to message-driven-channel-adapter, try with this:

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

It fail when it timeout.

Message<?> received = ((PollableChannel) msgChannel).receive(TIMEOUT);

You must check received for null