Spring集成:通过适配器接收JMS消息随机失败

时间:2012-06-24 19:11:11

标签: spring jms activemq spring-integration

我正在尝试为JMS队列编写通道适配器。我想将消息发送到JMS队列并在Spring Integration的通道上接收它。

当我使用普通的JMS(使用ActiveMQ)时,一切正常,所以我认为bug在我的Spring代码中。

所以这是我的Spring配置文件:

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

这是普通的JMS发送接收代码,可以正常工作:

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

这里是使用Spring的MessageChannel的代码,它通常不起作用(有时会这样做,但我无法确定何时):

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

我从pollable msgChannel接收消息时收到 NullPointerException 。这是我从Spring配置到我的测试类的自动装配bean的方式:

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

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

@Autowired
ConnectionFactory connectionFactory;

2 个答案:

答案 0 :(得分:2)

奇怪,我能够重现你的问题,可能是入站通道适配器的错误,我有一些一贯的工作,通过从inbound-channel-adapter更改为message-driven-channel-adapter,尝试这样:

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

答案 1 :(得分:-1)

超时时失败。

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

您必须检查received null