private void scheduleNext() { for (Map.Entry<ChannelHandler, Object> entry : planner.nextChannels().entrySet()) { final String channelName = entry.getKey().getChannelName(); try { entry.getKey().write(entry.getValue(), new ChannelWriteCallback() { AtomicInteger counter = new AtomicInteger(); @Override public void channelWritten(Exception ex) { planner.removeChannel(channelName); // If there was an error, notify the exception // and don't schedule anything else if (ex != null) { exceptionHandler.handleException(ex); return; } // Notify only when the last channel was written if (planner.isDone()) { callback.run(); } else { scheduleNext(); } } }); } catch (RuntimeException ex) { exceptionHandler.handleException(ex); } } }