【问题标题】:SpringIntegration: Service-Activator not invoking methodSpringIntegration:服务激活器不调用方法
【发布时间】:2012-12-20 11:20:42
【问题描述】:

我想从 RabbitMQ 队列中读取消息 > 使用 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);
    }

我的目标是在 Service-Activator 方法中获取消息本身、有效负载和标头。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    如果您想通过 SpEL 正确获取消息,可以使用

    #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
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多