1

I have an application that tries to change the @StreamListener approach to a functional approach, as @StreamListener is deprecated and might be removed someday soon.

The old approach allowed us to use the following conception:

@StreamListener(target = Channel.INPUT_ENROLL, condition = "headers['type']=='Type1'" ...)
handleType1{...}

@StreamListener(target = Channel.INPUT_ENROLL, condition = "headers['type']=='Type2'" ...)
handleType2{...}

...

@StreamListener(target = Channel.INPUT_ENROLL, condition = "headers['type']=='Typen'" ...)
handleTypen{...}

In the project, the routing expression would become something like this, which seems imposible to maintain:

routing-expression: "header['type'] == 'type1' ? 'handleType1' : "header['type'] == 'type2' ? 'handleType2':..."header['type'] == 'typen' ? 'handleTypen' : 'handleUnsupportedType'"

Is there any way other than this to maintain the code?

ouflak
  • 2,458
  • 10
  • 44
  • 49

1 Answers1

1

You can implement MessageRoutingCallback - https://docs.spring.io/spring-cloud-function/docs/3.2.0-SNAPSHOT/reference/html/spring-cloud-function.html#_function_routing_and_filtering

Oleg Zhurakousky
  • 5,820
  • 16
  • 17
  • Hello, it seems like a great solution. Is there any way to assign multiple routings made this way to multiple topics? In my example, the type is just a single event that may happen with the object. Type1Created, Type2Deleted etc, are all on different topics with different names but same header type. I might be misunderstanding how it works. If there is, how do i assign that routing to the binding, and not for all of the bindings? – Bartłomiej Paluch Nov 29 '21 at 12:31
  • 1
    No, but we have an issue open to do that – Oleg Zhurakousky Nov 29 '21 at 18:21
  • Thank you very much. That sounds great. Can't wait for it! It might be a little too much to ask, but I couldn't find any, do You think there is any workaround to it till then? Or the routing expressions are the way to go for now? – Bartłomiej Paluch Nov 30 '21 at 00:29
  • A similar problem I am facing. Did you get the workaround Bartłomiej Paluch ? – secret129 Oct 06 '22 at 10:56