Crowie
Crowie

Reputation: 3272

How should I build my Messages in Spring Integration?

I have an application I coded which I am refactoring to make better use of Spring Integration. The application processes the contents of files.

The problem (as I see it) is that my current implementation passes Files instead of Messages, i.e. Spring Integration Messages.


In order to avoid further rolling my own code, which I then have to maintain later, I'm wondering if there is a recommended structure for constructing Messages in Spring Integration. What I wonder is if there is some recommended combination of channel with something like MessageBuilder that I should use.


Process/Code (eventually)

I don't yet have the code to configure it but I would like to end up with the following components/processes:

  1. Receive a file, remove header and footer of the file, take each line and convert it into a Message<String> (This it seems will actually be a Splitter) which I send on to...
  2. Channel/Endpoint sends message to Router
  3. Router detects format String in Payload and routes to the appropriate channel similar to Order Router here...
  4. Selected channel then builds appropriate type of Message, specifically typed messages. For example I have the following builder to build a Message...

    public class ShippedBoxMessageBuilder implements CustomMessageBuilder {

    @Override public Message buildMessage(String input) { ShippedBox shippedBox = (ShippedBox) ShippedBoxFactory.manufactureShippedFile(input); return MessageBuilder.withPayload(shippedBox).build(); } ...

  5. Message is routed by type to the appropriate processing channel

My intended solution does seem like I've complicated it. However, I've purposefully separated two tasks 1) Breaking a file into many lines of Messages<String> and 2) Converting Messages<String> into Messages<someType>. Because of that I think I need an additional router/Message builder for the second task.

Upvotes: 2

Views: 6369

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

Actually, there is MessageBuilder support in the Spring Integration.

The general purpose of such Frameworks is to help back-end developers to decouple their domain code from messaging infrastructure. Finally, to work with Spring Integration you need to follow the POJO and Method Invocation principles.

You write your own services, transformers and domain models. Then you just use some out of the box compoenents (e.g. <int-file:inbound-channel-adapter>) and just refer from there to your POJOs, but not vise versa.

I recommend you to read Spring Integration in Action book to have more pictures on the matter.

Can you explain the reason to get deal with Spring Integration components directly?

UPDATE

1) Breaking a file into many lines of Messages

The <splitter> is for you. You should write some POJO which returns List<String> - the lines from your file without header and footer. How to read lines from File isn't a task of Spring Integration. Especially, if the "line" is something logical, not the real file line.

2) Converting Messages into Messages

One more time: there is no reason to build Message object. It's just enough to build new payload in some transformer (again POJO) and framework wrap to its Message to send.

Payload Type Router speaks for itself: it checks a payload type, but not Message type. Of course, payload can be Message too, and even any header can be as well.

Anyway your Builder snapshot shows exactly a creation of plain Spring Integration Message in the end. And as I said: it will be enough just to transform one payload to another and return it from some POJO, which you will use as a transformer reference.

Upvotes: 5

Related Questions