/** * Removes all callbacks for a given channel. Registered callbacks will receive a failure response containing a * {@link CommandBusConnectorCommunicationException}. * * @param channelId the channel identifier */ public void cancelCallbacks(A channelId) { Iterator<CommandCallbackWrapper> callbacks = this.callbacks.values().iterator(); while (callbacks.hasNext()) { CommandCallbackWrapper wrapper = callbacks.next(); if (wrapper.getChannelIdentifier().equals(channelId)) { wrapper.reportResult(asCommandResultMessage(new CommandBusConnectorCommunicationException( String.format("Connection error while waiting for a response on command %s", wrapper.getMessage().getCommandName())))); callbacks.remove(); } } }
/** * Invokes {@link CommandCallback#onResult(CommandMessage, CommandResultMessage)} with given {@code result} on * the wrapped callback. * * @param result the result of the command */ public void reportResult(CommandResultMessage<R> result) { onResult(getMessage(), result); }
/** * Removes all callbacks for a given channel. Registered callbacks will receive a failure response containing a * {@link CommandBusConnectorCommunicationException}. * * @param channelId the channel identifier */ public void cancelCallbacks(A channelId) { Iterator<CommandCallbackWrapper> callbacks = this.callbacks.values().iterator(); while (callbacks.hasNext()) { CommandCallbackWrapper wrapper = callbacks.next(); if (wrapper.getChannelIdentifier().equals(channelId)) { wrapper.fail(new CommandBusConnectorCommunicationException( String.format("Connection error while waiting for a response on command %s", wrapper.getMessage().getCommandName()))); callbacks.remove(); } } }
/** * Invokes {@link CommandCallback#onSuccess(CommandMessage, Object)} with given {@code result} on the wrapped * callback. * * @param result the result of the command */ public void success(R result) { onSuccess(getMessage(), result); }
/** * Invokes {@link CommandCallback#onFailure(CommandMessage, Throwable)} with given exception on the wrapped * callback. * * @param e cause for the failure */ public void fail(Throwable e) { onFailure(getMessage(), e); }
/** * Stores a callback * * @param callbackId The id to store the callback with * @param commandCallbackWrapper The CommandCallbackWrapper to store * @param <E> The type of the remote endpoint identifier * @param <C> The type of the command * @param <R> The type of the result */ public <E, C, R> void store(String callbackId, CommandCallbackWrapper<E, C, R> commandCallbackWrapper) { CommandCallbackWrapper previous; if ((previous = callbacks.put(callbackId, commandCallbackWrapper)) != null) { //a previous callback with the same command ID was already found, we will cancel the callback as the command //is likely to be retried, so the previous one likely failed previous.reportResult(asCommandResultMessage(new CommandBusConnectorCommunicationException( "Command-callback cancelled, a new command with the same ID is entered into the command bus"))); } }
private void processReplyMessage(JGroupsReplyMessage message) { CommandCallbackWrapper<Object, Object, Object> callbackWrapper = callbackRepository.fetchAndRemove(message.getCommandIdentifier()); if (callbackWrapper == null) { logger.warn( "Received a callback for a message that has either already received a callback, or which was not " + "sent through this node. Ignoring."); } else { if (message.isSuccess()) { callbackWrapper.success(message.getReturnValue(serializer)); } else { Throwable exception = getOrDefault(message.getError(serializer), new IllegalStateException( format("Unknown execution failure for command [%s]", message.getCommandIdentifier()))); callbackWrapper.fail(exception); } } }
@Override public <C, R> void send(Member destination, CommandMessage<C> command, CommandCallback<? super C, R> callback) throws Exception { callbackRepository.store(command.getIdentifier(), new CommandCallbackWrapper<>(destination, command, callback)); channel.send(resolveAddress(destination), new JGroupsDispatchMessage(command, serializer, true)); }
/** * Stores a callback * * @param callbackId The id to store the callback with * @param commandCallbackWrapper The CommandCallbackWrapper to store * @param <E> The type of the remote endpoint identifier * @param <C> The type of the command * @param <R> The type of the result */ public <E, C, R> void store(String callbackId, CommandCallbackWrapper<E, C, R> commandCallbackWrapper) { CommandCallbackWrapper previous; if ((previous = callbacks.put(callbackId, commandCallbackWrapper)) != null) { //a previous callback with the same command ID was already found, we will cancel the callback as the command //is likely to be retried, so the previous one likely failed previous.fail(new CommandBusConnectorCommunicationException( "Command-callback cancelled, a new command with the same ID is entered into the command bus")); } }
/** * Stores a callback * * @param callbackId The id to store the callback with * @param commandCallbackWrapper The CommandCallbackWrapper to store * @param <E> The type of the remote endpoint identifier * @param <C> The type of the command * @param <R> The type of the result */ public <E, C, R> void store(String callbackId, CommandCallbackWrapper<E, C, R> commandCallbackWrapper) { CommandCallbackWrapper previous; if ((previous = callbacks.put(callbackId, commandCallbackWrapper)) != null) { //a previous callback with the same command ID was already found, we will cancel the callback as the command //is likely to be retried, so the previous one likely failed previous.reportResult(asCommandResultMessage(new CommandBusConnectorCommunicationException( "Command-callback cancelled, a new command with the same ID is entered into the command bus"))); } }
@Override public <C, R> void send(Member destination, CommandMessage<C> command, CommandCallback<? super C, R> callback) throws Exception { callbackRepository.store(command.getIdentifier(), new CommandCallbackWrapper<>(destination, command, callback)); channel.send(resolveAddress(destination), new JGroupsDispatchMessage(command, serializer, true)); }
/** * Removes all callbacks for a given channel. Registered callbacks will receive a failure response containing a * {@link CommandBusConnectorCommunicationException}. * * @param channelId the channel identifier */ public void cancelCallbacks(A channelId) { Iterator<CommandCallbackWrapper> callbacks = this.callbacks.values().iterator(); while (callbacks.hasNext()) { CommandCallbackWrapper wrapper = callbacks.next(); if (wrapper.getChannelIdentifier().equals(channelId)) { wrapper.reportResult(asCommandResultMessage(new CommandBusConnectorCommunicationException( String.format("Connection error while waiting for a response on command %s", wrapper.getMessage().getCommandName())))); callbacks.remove(); } } }
/** * Invokes {@link CommandCallback#onResult(CommandMessage, CommandResultMessage)} with given {@code result} on * the wrapped callback. * * @param result the result of the command */ public void reportResult(CommandResultMessage<R> result) { onResult(getMessage(), result); }
private void processReplyMessage(JGroupsReplyMessage message) { CommandCallbackWrapper callbackWrapper = callbackRepository.fetchAndRemove(message.getCommandIdentifier()); if (callbackWrapper == null) { logger.warn("Received a callback for a message that has either already received a callback, " + "or which was not sent through this node. Ignoring."); } else { //noinspection unchecked callbackWrapper.reportResult(message.getCommandResultMessage(serializer)); } }