2012-06-24 38 views
2

我正在嘗試爲JMS隊列編寫通道適配器。我想將消息發送到JMS隊列並在Spring Integration的頻道上接收它。彈簧集成:通過適配器接收JMS消息隨機失敗

當我使用普通的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); 
} 

I我收到NullPointerException收到來自pollable msgChannel的消息。下面是我如何從Spring配置自動佈線到我的測試類:

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

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

@Autowired 
ConnectionFactory connectionFactory; 
+0

你是怎麼注入一個會話?該文檔僅討論基於FTP的會話。 http://static.springsource.org/spring-integration/docs/latest-ga/reference/htmlsingle/#spring-integration-introduction – luksmir

回答

-1

它在超時時失敗。

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

您必須檢查receivednull

+0

這怎麼解決我的問題?我不想在第一個地方收到null ...(超時時間足夠長,我試圖用大的值進行試驗) – Xorty

2

奇怪,我能夠重現您的問題,可能是入站通道適配器的錯誤,我有一些東西,始終如一地努力,雖然,通過從改變inbound-channel-adaptermessage-driven-channel-adapter,試試這個:

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

@GaryRussell,你可能想檢查一下,我試着看JmsDestinationPollingSource,但不能使出了什麼可能會出錯。 –

+0

Thanx,我也使用消息驅動通道適配器「解決」它,但我仍然不明白入站適配器有什麼問題。其他入站適配器(如JDBC)適合我。 – Xorty

+0

我建議你爲org.springframework運行TRACE級別日誌記錄;它應該變得很明顯發生了什麼事情。我們通常推薦使用消息驅動的適配器,但有些情況下入站適配器很有用,所以我很樂意聽到您找到的東西。 –

相關問題