13.5.13

Channel Channel Channel!!

DirectChannel

First of all, DirectChannel is not Pollable Channel. Since it is not Pollable Channel, it is not based on BlockingQueue. I was wrong about that before.

DirectChannel is a Subscribable Channel but with Point-to-point Semantics. It would be easy to understand from the implementation's point of view.

private boolean doDispatch(Message<?> message) {
boolean success = false;
Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message);
if (!handlerIterator.hasNext()) {
throw new MessageDispatchingException(message, "Dispatcher has no subscribers");
}
List<RuntimeException> exceptions = new ArrayList<RuntimeException>();
while (success == false && handlerIterator.hasNext()) {
MessageHandler handler = handlerIterator.next();
try {
handler.handleMessage(message);
success = true; // we have a winner.
}
catch (Exception e) {
RuntimeException runtimeException = (e instanceof RuntimeException)
? (RuntimeException) e
: new MessageDeliveryException(message,
"Dispatcher failed to deliver Message.", e);
if (e instanceof MessagingException &&
((MessagingException) e).getFailedMessage() == null) {
((MessagingException) e).setFailedMessage(message);
}
exceptions.add(runtimeException);
this.handleExceptions(exceptions, message, !handlerIterator.hasNext());
}
}
return success;
}

DirectChannel picks up the next available Handler and calls its handleMessage() method to send out the message. And that's it.
  • It's point-to-point.
    • It only call one handler successfully.
  • And it won't queue!
    • When the message arrives and the handler is not there, it will throw an exception. 

No comments: