We have a case where in our spring integration splitter logic where it could return an empty ArrayList
thus providing no data to be routed. How should we handle this scenario and reroute it to a different channel when there is an empty ArrayList
returned from the splitter.
UPDATE :
As shown by Artem I have created a custom advice and have got the rerouting to be done successfully however there are still two issues, where the second one describe is the blocker issue listed
The current class that I have is as follows
public class EmptyListRequestHandlerAdvice extends AbstractRequestHandlerAdvice {
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
public EmptyListRequestHandlerAdvice(MessageChannel emptyListChannel) {
this.messagingTemplate.setDefaultChannel(emptyListChannel);
}
@Override
protected Object doInvoke(ExecutionCallback callback, Object target,
Message<?> message) throws Exception {
Object result = callback.execute();
if (result==null) {
this.messagingTemplate.convertAndSend(new ArrayList<InventoryHotel>());
}
return result;
}
Whenever the splitter returns a new empty array list the result of "callback.execute() is null. However whenever the returned array list from the splitter is not empty the returned result is of type org.springframework.integration.util.FunctionIterator and not a collection type. Any idea on if it is possible to get a collection returned in both cases instead of null or function iterator? Atleast avoid getting a function iterator and get an array list.
Additionally when a valid channel name is set to the constructor and a message is sent through that channel to the transformer method the transformer method is successfully executed however when it returned a value the following error occurs
org.springframework.messaging.MessagingException: org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available
at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
Any suggestions on why this occurs and how to resolve it.
Regards, Milinda