Announcement Announcement Module
No announcement yet.
SPEL Assignment Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SPEL Assignment

    I am playing with SPEL inside SI.
    Recently I faced a problem that took me couple of hours, most probably caused by my misreading of reference guide...
    Anyway, if I have assignment expression on transformer, or service activator:

    <int:transformer expression="payload.setStatus(T(com.acme.model.TransactionStatus).pending)" input-channel="input" output-channel="output"/>
    where payload is of type Transaction with enumerated field TransactionStatus status, my naive assumption was that I can transform object with expression and I will get transformed object in output channel.

    Wrong, since method setStatus(TransactionStatus) is VOID, so no message is put in output channel.
    I had some difficulties to trace the problem (my next endpoint in chain could not be invoked simply because it didn't have message passed ).
    No errors, warnings, anything in logs.

    Ok, you can say, no return type in method within expression, no message passed, as stated in reference guide (spotted too late) but:
    1. how to do then SPEL assignments, beyond simple Spring concatenation in examples?
    2. All model classes have setters that are void (I am using Roo generated model, btw), so how to use SPEL to set the field in model class?

    thanks & best,

  • #2
    In this case you can try to use Groovy:
    HTML Code:
    <int:transformer input-channel="input" output-channel="output">
                      payload.status = com.acme.model.TransactionStatus.pending
                      return payload
    Good luck,
    Artem Bilan


    • #3
      Artem is correct

      Groovy would be a good solution for your use case.

      But I want to emphasize one more point to make sure you its clear.

      There are few things one must not forget when it comes to implementing messaging systems (SI included). Messages are immutable and although it may look as if the Message that came in is the same Message that came out, what came out is a whole new instance (copy) of the Message.
      So, based on the Artem's code the payload will be updated, but will also be incorporated into a whole new Message by the invoking endpoint.
      Also, in a case of the Transformer you MUST always return something (that is a contract of the transformer to always produce an output) and that is what the code is showing.

      So the bottom line is that it doesn't really matter what you use (Groovy, Java, SpEL etc.), you must understand the contract of a particular endpoint with the Messaging system and use the most appropriate tool to execute your business logic but only within the boundaries of such contract.

      Also, with ServiceActivator the contract is a bit more relaxed where if you don't return anything (void) or return 'null', the message flow will end, but if you want to enforce the same behavior as Transformer then you can set 'requires-reply' attribute to 'true'.