@Override public <C> void dispatch(CommandMessage<C> command) { LoggingCallback loggingCallback = LoggingCallback.INSTANCE; if (NoOpMessageMonitor.INSTANCE.equals(messageMonitor)) { CommandMessage<? extends C> interceptedCommand = intercept(command); Optional<Member> optionalDestination = commandRouter.findDestination(interceptedCommand); if (optionalDestination.isPresent()) { Member destination = optionalDestination.get(); try { connector.send(destination, interceptedCommand); } catch (Exception e) { destination.suspect(); loggingCallback.onResult(interceptedCommand, asCommandResultMessage( new CommandDispatchException(DISPATCH_ERROR_MESSAGE + ": " + e.getMessage(), e) )); } } else { loggingCallback.onResult(interceptedCommand, asCommandResultMessage(new NoHandlerForCommandException( format("No node known to accept [%s]", interceptedCommand.getCommandName()) ))); } } else { dispatch(command, loggingCallback); } }
/** * {@inheritDoc} * <p/> * In the DistributedCommandBus, the handler is subscribed to the local segment only. */ @Override public Registration subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) { Registration reg = connector.subscribe(commandName, handler); updateFilter(commandFilter.get().or(new CommandNameFilter(commandName))); return () -> { updateFilter(commandFilter.get().and(new DenyCommandNameFilter(commandName))); return reg.cancel(); }; }
/** * Initializes a {@link DistributedCommandBus} as specified through this Builder. * * @return a {@link DistributedCommandBus} as specified through this Builder */ public DistributedCommandBus build() { return new DistributedCommandBus(this); }
CommandMessage<? extends C> interceptedCommand = intercept(command); MessageMonitor.MonitorCallback messageMonitorCallback = messageMonitor.onMessageIngested(interceptedCommand); Optional<Member> optionalDestination = commandRouter.findDestination(interceptedCommand);
@Bean @Primary @ConditionalOnBean(CommandBusConnector.class) @ConditionalOnMissingBean public DistributedCommandBus distributedCommandBus(CommandRouter commandRouter, CommandBusConnector commandBusConnector, DistributedCommandBusProperties distributedCommandBusProperties) { DistributedCommandBus commandBus = DistributedCommandBus.builder() .commandRouter(commandRouter) .connector(commandBusConnector) .build(); commandBus.updateLoadFactor(distributedCommandBusProperties.getLoadFactor()); return commandBus; } }
/** * {@inheritDoc} * * @throws CommandDispatchException when an error occurs while dispatching the command to a segment */ @Override public void dispatch(CommandMessage<?> command) { command = intercept(command); String routingKey = routingStrategy.getRoutingKey(command); try { connector.send(routingKey, command); } catch (Exception e) { logger.error(DISPATCH_ERROR_MESSAGE, e); } }
@Override public <C> void dispatch(CommandMessage<C> command) { if (NoOpMessageMonitor.INSTANCE.equals(messageMonitor)) { CommandMessage<? extends C> interceptedCommand = intercept(command); Member destination = commandRouter.findDestination(interceptedCommand) .orElseThrow(() -> new NoHandlerForCommandException( format("No node known to accept [%s]", interceptedCommand.getCommandName()))); try { connector.send(destination, interceptedCommand); } catch(Exception e) { destination.suspect(); throw new CommandDispatchException(DISPATCH_ERROR_MESSAGE + ": " + e.getMessage(), e); } } else { dispatch(command, null); } }
/** * {@inheritDoc} * * @throws CommandDispatchException when an error occurs while dispatching the command to a segment */ @Override public <R> void dispatch(CommandMessage<?> command, CommandCallback<R> callback) { command = intercept(command); String routingKey = routingStrategy.getRoutingKey(command); try { connector.send(routingKey, command, callback); } catch (Exception e) { callback.onFailure(new CommandDispatchException(DISPATCH_ERROR_MESSAGE + ": " + e.getMessage(), e)); } }
/** * {@inheritDoc} * <p/> * In the DistributedCommandBus, the handler is subscribed to the local segment only. */ @Override public Registration subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) { Registration reg = connector.subscribe(commandName, handler); updateFilter(commandFilter.get().or(new CommandNameFilter(commandName))); return () -> { updateFilter(commandFilter.get().and(new DenyCommandNameFilter(commandName))); return reg.cancel(); }; }
/** * Initializes a {@link DistributedCommandBus} as specified through this Builder. * * @return a {@link DistributedCommandBus} as specified through this Builder */ public DistributedCommandBus build() { return new DistributedCommandBus(this); }
@Override public <C> void dispatch(CommandMessage<C> command) { LoggingCallback loggingCallback = LoggingCallback.INSTANCE; if (NoOpMessageMonitor.INSTANCE.equals(messageMonitor)) { CommandMessage<? extends C> interceptedCommand = intercept(command); Optional<Member> optionalDestination = commandRouter.findDestination(interceptedCommand); if (optionalDestination.isPresent()) { Member destination = optionalDestination.get(); try { connector.send(destination, interceptedCommand); } catch (Exception e) { destination.suspect(); loggingCallback.onResult(interceptedCommand, asCommandResultMessage( new CommandDispatchException(DISPATCH_ERROR_MESSAGE + ": " + e.getMessage(), e) )); } } else { loggingCallback.onResult(interceptedCommand, asCommandResultMessage(new NoHandlerForCommandException( format("No node known to accept [%s]", interceptedCommand.getCommandName()) ))); } } else { dispatch(command, loggingCallback); } }
/** * {@inheritDoc} * * @throws CommandDispatchException when an error occurs while dispatching the command to a segment */ @Override public <C, R> void dispatch(CommandMessage<C> command, CommandCallback<? super C, R> callback) { CommandMessage<? extends C> interceptedCommand = intercept(command); MessageMonitor.MonitorCallback messageMonitorCallback = messageMonitor.onMessageIngested(interceptedCommand); Member destination = commandRouter.findDestination(interceptedCommand) .orElseThrow(() -> { NoHandlerForCommandException exception = new NoHandlerForCommandException( format("No node known to accept [%s]", interceptedCommand.getCommandName())); messageMonitorCallback.reportFailure(exception); return exception; }); try { connector.send(destination, interceptedCommand, new MonitorAwareCallback<>(callback, messageMonitorCallback)); } catch(Exception e) { messageMonitorCallback.reportFailure(e); destination.suspect(); throw new CommandDispatchException(DISPATCH_ERROR_MESSAGE + ": " + e.getMessage(), e); } }
/** * {@inheritDoc} * <p/> * In the DistributedCommandBus, the handler is subscribed to the local segment only. */ @Override public Registration subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) { Registration reg = connector.subscribe(commandName, handler); updateFilter(commandFilter.get().or(new CommandNameFilter(commandName))); return () -> { updateFilter(commandFilter.get().and(new DenyCommandNameFilter(commandName))); return reg.cancel(); }; }
CommandMessage<? extends C> interceptedCommand = intercept(command); MessageMonitor.MonitorCallback messageMonitorCallback = messageMonitor.onMessageIngested(interceptedCommand); Optional<Member> optionalDestination = commandRouter.findDestination(interceptedCommand);