Reputation: 4747
Let's imagine I have a processor that should filter out messages that are not important. How can I do that using Spring cloud streams?
I tried this example, but I get a null point exception:
@Bean
Function<String, String> upper() {
return s -> s.length() < 5 ? null : s;
}
I could change my processor to return a List<Messages<String>>
or similar, but this would mean that my processor could produce 0, 1 or N messages, and I want it to be able to produce only 0 or 1.
Is there a way to do it?
Upvotes: 0
Views: 62
Reputation: 6126
For this case you can use Consumer. The analogy of a glass half-full or half-empty. Basically you want to "occasionally" send output message. So you can do this:
@Bean
Consumer<String> upper(StreamBridge streamBridge) {
return v -> {
if (v.length() > 5) {
streamBridge.send("destination_name", v);
}
};
}
NOTE: The "destination_name" is the name of your destination. If it doesn't exist it will be auto-provisioned on the first send. The second argument is the data you are sending. Could be Message could be POJO. . . up to you .
For more information on StreamBridge - https://docs.spring.io/spring-cloud-stream/docs/3.1.3/reference/html/spring-cloud-stream.html#_sending_arbitrary_data_to_an_output_e_g_foreign_event_driven_sources.
Now, returning 'null' as in your case should actually work, so please raise and issue - https://github.com/spring-cloud/spring-cloud-stream/issues
Upvotes: 1