2012-12-20 71 views
2

我想閱讀RabbitMQ隊列中的消息>使用Service-Activator調用服務。SpringIntegration:Service-Activator未調用方法

相關配置部分:

<int-amqp:inbound-channel-adapter channel="fromRabbit"           queue-names="si.test.queue" mapped-request-headers="whatever" 
            connection-factory="connectionFactory" /> 
<int:service-activator input-channel="fromRabbit" output-channel="whatever" 
       ref="msgService" method="checkMsg"/> 

<bean id="msgService" class="com.whatever.MsgService"/> 

的MsgService類是:

public class MsgService{ 
//Does not work! 
public void checkMsg(@Payload String s) { 
    System.out.println("The Payload is: " +s);  
    } 

} 

,但我得到以下錯誤信息:
... 引起:java.lang.IllegalArgumentException異常:[class org.springframework.integration.service.MessageExaminer]類型的目標對象沒有合適的方法來處理消息。

我在這做錯了什麼?

但如果我只是用這個作爲ServiceMsg類中的方法 - 的作品:

public void seeMessage(String m) 
    { 
     System.out.println(m); 
    } 

我的目標是獲得消息本身,負載和標頭在服務的激活方法的保持。

回答

2

,如果你想通過規劃環境地政司正確的信息,您可以使用

#root 

一個例子是

<int:channel id="quick.channel"/> 

    <int:service-activator input-channel="quick.channel" 
     expression="@quickService.process(#root)"/> 

這將允許你在你的服務

訪問消息
public void process(Message<?> msg) { 
     //do something with it 
    } 

注意:這使得您的服務代碼與Spring Integration緊密結合 - 您確定要這麼做嗎? ?如果您需要頭部和負載的組合,爲什麼不將它們作爲單獨的參數傳遞給服務,從而將服務從Spring Integration消息對象中解耦?

<int:service-activator input-channel="quick.channel" 
expression="@quickService.process(headers['headerValue'],payload)"/> 

和服務將是

public void process(String headerValue,Object payload) { 
    //use them together 
}