【问题标题】:Spring Integration - Invoking Methods in Application CodeSpring Integration - 在应用程序代码中调用方法
【发布时间】:2015-07-11 17:18:18
【问题描述】:

我有一个outbound-channel-adapter,相关配置如下所示。

<int:outbound-channel-adapter channel="foo-fileChannel" ref="foo-handlerTarget" method="handleFeedFile">
    <int:poller fixed-delay="5000" receive-timeout="1000" max-messages-per-poll="10" />
</int:outbound-channel-adapter>
<int:channel id="foo-fileChannel">
    <int:queue />
</int:channel>


<bean id="foo-handlerTarget" class="com.abc.FooFeedHandlerImpl">
    <property name="fooDescriptorFile" value="${feed.foo.fooDescriptorFile}" />
    <property name="fileIdRegex" ref="foo-fileRegex" />
    <property name="processId" value="${feed.processId}" />
    <property name="workingLocation" value="${feed.foo.workingLocation}" />
    <property name="remoteLocation" value="${feed.foo.remoteLocation}" />
    <property name="stalenessThreshold" value="${feed.foo.stalenessThreshold}" />
</bean>

而在 FooFeedHandlerImpl...

public void handleFeedFile(File retrievedFile) {
    handleFeedFile(retrievedFile, null);
}


public void handleFeedFile(File retrievedFile, String processKey) {
    if (isHandlerForFileName(retrievedFile.getName())) {
        processFeed(retrievedFile, processKey);
    }
}

问题:

通道适配器调用哪个 handleFeedFile 方法?

当我使用 Spring 集成在应用程序代码中调用方法时,方法参数是如何确定的?

感谢您的帮助!

编辑:

我在本地运行我的进程(下载了本地 SFTP 服务器 - http://www.coreftp.com/server/index.html)并确定调用了 handleFeedFile(File file) 方法。

【问题讨论】:

  • 目前发生了什么?

标签: java spring spring-integration enterprise-integration


【解决方案1】:

您可能想参考F.6 Message Mapping rules and conventions

在确定适当的映射时,多个参数可能会产生很多歧义。一般建议是使用@Payload 和/或@Header/@Headers 注释您的方法参数。下面是一些导致异常的模棱两可条件的示例。

和:

多种方法:

具有多个方法的消息处理程序根据上述相同的规则进行映射,但是某些场景可能看起来仍然令人困惑。

如果您无法注释您的目标方法,那么您可以使用 SpEL 表达式来调用您想要的方法:

3.3.2 Configuring An Outbound Channel Adapter

与许多其他 Spring Integration 组件一样,它也提供对 SpEL 表达式评估的支持。要使用 SpEL,请通过 'expression' 属性提供表达式字符串,而不是提供用于 bean 上的方法调用的 'ref' 和 'method' 属性。当计算表达式时,它遵循与方法调用相同的约定,其中:每当计算结果为非空值时,an 的表达式将生成一条消息,而 an 的表达式必须等效于返回 void 的方法调用。

【讨论】:

  • 谢谢忍者。在您的帮助下,我肯定对现在发生的事情有了更好的了解。但是,我仍然不确定在我的情况下会调用哪种方法。它看起来与消息映射规则中提供的倒数第二个示例非常相似,例如foo(String str, Map m) vs foo(String str),但我没有收到异常...你知道我的情况的规则吗?
  • 我在调试过程并确定方法后编辑了我的答案。毫不奇怪,只调用了一个参数的方法。就我而言,文件是有效负载,所以我猜 handleFeedFile(File file) 是唯一有效的方法。
【解决方案2】:

根据 Spring 集成中的 documentation,在您的情况下,POJO(bean foo-handlerTarget)将使用包含有效负载的 Message 对象调用。你执行过你的代码吗?我希望它会生成一个NoSuchMethodError

你需要一个

public void handleFeedFile(Message<?> message);

方法。

【讨论】:

  • 感谢您的回复。是的,代码已执行。它实际上已经在我们的生产环境中运行了几年,没有出现任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 2010-12-12
  • 2019-02-05
  • 1970-01-01
  • 2014-10-30
  • 2019-06-12
相关资源
最近更新 更多