Search code examples
regexspringspring-integrationspring-el

Kafka channel routing using SpEL and regex


Would like to route to the channel based on the SpEL regex combination(so that multiple message from different topic/payload can be routed to same channel). Tried as above and other combination using different router types available.Not working.

<int:recipient-list-router  input-channel="receiveMessageChannel">
 <int:recipient channel="in_channel" selector-expression="headers['topic']  matches  #{systemProperties['env'] + '${in.msge.topic}' + '.*'}"/>
 <int:recipient channel="email_channel" selector-expression="headers['topic']  matches #{systemProperties['env'] + '${email.msge.topic}' + '.*'}"/>
 </int:recipient-list-router>

getting below error:

Caused by: org.springframework.expression.spel.SpelParseException: EL1049E:(pos 37): Unexpected data after '.': 'star(*)'

headers['topic'] = mesge.getPayload().setHeaders("topic","testTopic");

Can anyone provide some suggestion.Thanks.


Solution

  • The matches argument must be a String. Here's the correct syntax...

    selector-expression="headers['topic']  matches '#{systemProperties[env]}${in.msge.topic}.*'"
    

    Note that it won't work with dotted systemProperties. You need to add quotes for that...

    selector-expression="headers['topic']  matches '#{systemProperties[&quot;env.foo&quot;]}${in.msge.topic}.*'"/>
    

    The reason this is a little different to this answer is because, in that case, the SpEL expression evaluates to a value that is used as a simple String during context initialization.

    In this case, we need to evaluate to a String that's used at runtime as part of another SpEL expression - hence the whole thing needs to be surrounded by '...'.