Issue
I have created DirectChannel and have sent an object to my channel and want to receive it abck to store it in DB and send it in another service bus queue. Can you suggest how to receive the object from channel?
My Channel -
@Bean("tempChannel")
public MessageChannel tempChannel() {
return new DirectChannel();
}
Integration flow -
@Bean
public IntegrationFlow tempMessageFlow() {
return IntegrationFlows.from("tempChannel").handle().get();
}
For handle method I need to pass MessageHandler, how to I declare it and pass here?
I am sending message to channel using below piece of code, please do tell if this is alright-
tempChannel().send(messageObj);
Solution
The DirectChannel
implements a SubscribableChannel
. So, to get messages sent to this channel you need to subscribe(MessageHandler handler)
. What you have so far with that IntegrationFlow
definition is OK: adding that handle()
you subscribe to the tempChannel
. Just handle message and forget you can do this:
.handle(m - > System.out.println("Processed message: " + m))
This is a lambda for that MessageHandler
functional interface. There are many other handle()
variants for other use-cases. For example process-n-reply is like this:
.handle((p, h) - > {
System.out.println("Processed message: " + m);
return "My new payload";
})
If you say that you need to do several operations on the same message, then look into a PublishSubscribeChannel
. In Java DSL we have a publishSubscribeChannel(Consumer<PublishSubscribeSpec> publishSubscribeChannelConfigurer)
to configure several subscribers as sub-flows.
To store into DB, you can use a JdbcMessageHandler
: https://docs.spring.io/spring-integration/docs/current/reference/html/jdbc.html#jdbc-outbound-channel-adapter
Answered By - Artem Bilan
Answer Checked By - David Goodson (JavaFixing Volunteer)