【问题标题】:Spring integration: Receiving JMS messages via adapter randomly failsSpring集成:通过适配器随机接收JMS消息失败
【发布时间】:2012-06-26 04:22:30
【问题描述】:

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

当我使用纯 JMS(带有 ActiveMQ)时,一切正常,所以我认为错误在我的 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);
}

我收到来自可轮询 msgChannel 的消息时收到 NullPointerException。以下是我如何将 Spring 配置中的 bean 自动装配到我的测试类:

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

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

@Autowired
ConnectionFactory connectionFactory;

【问题讨论】:

标签: spring jms activemq spring-integration


【解决方案1】:

奇怪,我能够重现您的问题,可能是入站通道适配器的错误,但我有一些始终有效的东西,通过将 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" />

【讨论】:

  • @GaryRussell,您可能想检查一下,我尝试查看 JmsDestinationPollingSource,但无法弄清楚可能出了什么问题。
  • 谢谢,我也使用消息驱动通道适配器“解决”了它,但我仍然不明白入站适配器有什么问题。其他入站适配器(如 JDBC)对我来说很好用。
  • 我建议您使用 org.springframework 的 TRACE 级别日志记录运行;发生的事情应该很明显。我们通常建议使用消息驱动的适配器,但在某些情况下入站适配器很有用,所以我很想听听您的发现。
【解决方案2】:

超时失败。

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

您必须检查received 是否有null

【讨论】:

  • 这如何解决我的问题?我不想一开始就收到 null ...(超时足够大,我尝试尝试大值)
猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2014-01-23
  • 2017-04-29
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多