简体   繁体   English

在Spring Integration中调用服务激活器和bean方法

[英]Calling service activator vs bean methods in Spring Integration

There're few ways one can manipulate the message in Spring Integration. 在Spring Integration中,很少有人能够操纵消息。 One way is calling a bean's method inside <int:enricher> that will return an object and assign it to the given name, eg: 一种方法是在<int:enricher>中调用bean的方法,该方法将返回一个对象并将其分配给给定的名称,例如:

<!-- calls getKey method of IdGenerator bean which returns String with some value -->
<int:enricher input-channel="a.channel" output-channel="b.channel" id="preTranslator">
   <int:header name="Key" expression="@IdGenerator.getId(payload)"/> 
</int:enricher>

Same can be utilized in filtering: 可以在过滤中使用相同的内容:

int:filter discard-channel="d.channel" input-channel="b.channel" output-channel="c.channel" 
     expression="@Cache.hasKey(headers.Key) == false"/>

On the other hand I can call the <int:service-activator> on a class implementing MessageProcessor interface. 另一方面,我可以在实现MessageProcessor接口的类上调用<int:service-activator> It would take the original message and return a copy with a new header field. 它将采用原始消息并返回带有新标头字段的副本。 That requires my class's method to always build a new message with MessageBuilder though. 这需要我的类的方法始终使用MessageBuilder构建新消息。

Currently I use the first way for simple field enrichment and service-activator for requesting data from DB/external services. 目前,我使用第一种方式进行简单的字段丰富和服务激活,以便从DB /外部服务请求数据。 What's the right way of picking the correct approach? 选择正确方法的正确方法是什么?

First of all, the <filter> doesn't change message at all. 首先, <filter>根本不会改变消息。 The given name should be read as header , looking to your case. given name应该作为header读取,看你的情况。

The <service-activator> always return a new message. <service-activator>始终返回新消息。 Yes, you can populate new headers there as well and right you have to use MessageBuilder . 是的,你也可以在那里填充新的headers ,你必须使用MessageBuilder

It is fully unclear what is your problem. 完全不清楚你的问题是什么。 If you can achieve the solution with expressions in the config, so be that. 如果你可以在配置中使用表达式来实现解决方案,那就这样吧。 But if you do something with the message in the code and would like to add/edit/remove headers you use MessageBuilder . 但是如果您对代码中的消息执行某些操作并希望添加/编辑/删除headers ,则使用MessageBuilder

That's really fine. 那真的很好。 I think you should just read more documentations and right more code in your application. 我认为您应该在应用程序中阅读更多文档和更多代码。

Eventually you will find the most convenient style for yourself. 最终,您将找到最适合自己的风格。 Fro example I end up once with expressions and Groovy scripts. 例如,我最终使用表达式和Groovy脚本。 No Java code at all. 根本没有Java代码。 But right now I prefer Spring Integration Java DSL , because it is much faster, cleaner, fluent and lets get rid of any other configs like XML or Groovy. 但是现在我更喜欢Spring Integration Java DSL ,因为它更快,更干净,更流畅,并且可以摆脱任何其他配置,如XML或Groovy。 Everything is Java now and that MessageBuilder is still on the horse! 现在一切都是Java, MessageBuilder仍然是马!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM