1
votes

There're few ways one can manipulate the message in 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, e.g:

<!-- 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. 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.

Currently I use the first way for simple field enrichment and service-activator for requesting data from DB/external services. What's the right way of picking the correct approach?

1

1 Answers

0
votes

First of all, the <filter> doesn't change message at all. The given name should be read as header, looking to your case.

The <service-activator> always return a new message. Yes, you can populate new headers there as well and right you have to use 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.

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. No Java code at all. 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. Everything is Java now and that MessageBuilder is still on the horse!