Skip to content

Commit

Permalink
- Renamed CommandResponseMessage to CommandResultMessage.
Browse files Browse the repository at this point in the history
- asCommandResultMessage now checks whether the result is a Message already.
  • Loading branch information
m1l4n54v1c committed Sep 18, 2018
1 parent d36edbf commit 480449e
Show file tree
Hide file tree
Showing 43 changed files with 362 additions and 357 deletions.
Expand Up @@ -18,7 +18,7 @@

/**
* Interface describing a callback that is invoked when command handler execution has finished. Depending of the outcome
* of the execution, either the {@link #onSuccess(CommandMessage, CommandResponseMessage)} or the {@link
* of the execution, either the {@link #onSuccess(CommandMessage, CommandResultMessage)} or the {@link
* #onFailure(CommandMessage, Throwable)} is called.
*
* @param <R> the type of result of the command handling
Expand All @@ -31,11 +31,11 @@ public interface CommandCallback<C, R> {
/**
* Invoked when command handling execution was successful.
*
* @param commandMessage The message that was dispatched
* @param commandResponseMessage The result message of the command handling execution, if any.
* @param commandMessage The message that was dispatched
* @param commandResultMessage The result message of the command handling execution, if any.
*/
void onSuccess(CommandMessage<? extends C> commandMessage,
CommandResponseMessage<? extends R> commandResponseMessage);
CommandResultMessage<? extends R> commandResultMessage);

/**
* Invoked when command handling execution resulted in an error.
Expand Down
Expand Up @@ -21,17 +21,17 @@
import java.util.Map;

/**
* Message that represents a response from handling a {@link CommandMessage}.
* Message that represents a result from handling a {@link CommandMessage}.
*
* @param <R> The type of payload contained in this Message
* @author Milan Savic
* @since 4.0
*/
public interface CommandResponseMessage<R> extends Message<R> {
public interface CommandResultMessage<R> extends Message<R> {

@Override
CommandResponseMessage<R> withMetaData(Map<String, ?> metaData);
CommandResultMessage<R> withMetaData(Map<String, ?> metaData);

@Override
CommandResponseMessage<R> andMetaData(Map<String, ?> metaData);
CommandResultMessage<R> andMetaData(Map<String, ?> metaData);
}

This file was deleted.

@@ -0,0 +1,103 @@
/*
* Copyright (c) 2010-2018. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.axonframework.commandhandling;

import org.axonframework.messaging.GenericMessage;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.MessageDecorator;
import org.axonframework.messaging.MetaData;

import java.util.Map;

/**
* Generic implementation of {@link CommandResultMessage}.
*
* @param <R> The type of the payload contained in this Message
* @author Milan Savic
* @since 4.0
*/
public class GenericCommandResultMessage<R> extends MessageDecorator<R> implements CommandResultMessage<R> {

private static final long serialVersionUID = 9013948836930094183L;

/**
* Returns the given {@code commandResult} as a {@link CommandResultMessage} instance. If {@code commandResult}
* already implements {@link CommandResultMessage}, it is returned as-is. If {@code commandResult} implements {@link
* Message}, payload and meta data will be used to construct new {@link GenericCommandResultMessage}. Otherwise, the
* given {@code commandResult} is wrapped into a {@link GenericCommandResultMessage} as its payload.
*
* @param commandResult the command result to be wrapped as {@link CommandResultMessage}
* @param <T> The type of the payload contained in returned Message
* @return a Message containing given {@code commandResult} as payload, or {@code commandResult} if already
* implements {@link CommandResultMessage}
*/
@SuppressWarnings("unchecked")
public static <T> CommandResultMessage<T> asCommandResultMessage(Object commandResult) {
if (CommandResultMessage.class.isInstance(commandResult)) {
return (CommandResultMessage<T>) commandResult;
} else if (Message.class.isInstance(commandResult)) {
Message<?> commandResultMessage = (Message<?>) commandResult;
return new GenericCommandResultMessage<>((T) commandResultMessage.getPayload(),
commandResultMessage.getMetaData());
}
return new GenericCommandResultMessage<>((T) commandResult);
}

/**
* Creates a Command Result Message with the given {@code commandResult} as the payload.
*
* @param commandResult the payload for the Message
*/
public GenericCommandResultMessage(R commandResult) {
this(commandResult, MetaData.emptyInstance());
}

/**
* Creates a Command Result Message with the given {@code commandResult} as the payload and {@code metaData} as
* the meta data.
*
* @param commandResult the payload for the Message
* @param metaData the meta data for the Message
*/
public GenericCommandResultMessage(R commandResult, Map<String, ?> metaData) {
this(new GenericMessage<>(commandResult, metaData));
}

/**
* Creates a new Command Result Message with given {@code delegate} message.
*
* @param delegate the message delegate
*/
public GenericCommandResultMessage(Message<R> delegate) {
super(delegate);
}

@Override
public GenericCommandResultMessage<R> withMetaData(Map<String, ?> metaData) {
return new GenericCommandResultMessage<>(getDelegate().withMetaData(metaData));
}

@Override
public GenericCommandResultMessage<R> andMetaData(Map<String, ?> metaData) {
return new GenericCommandResultMessage<>(getDelegate().andMetaData(metaData));
}

@Override
protected String describeType() {
return "GenericCommandResultMessage";
}
}
Expand Up @@ -41,10 +41,10 @@ public MonitorAwareCallback(CommandCallback<C, R> delegate, MessageMonitor.Monit

@Override
public void onSuccess(CommandMessage<? extends C> commandMessage,
CommandResponseMessage<? extends R> commandResponseMessage) {
CommandResultMessage<? extends R> commandResultMessage) {
messageMonitorCallback.reportSuccess();
if (delegate != null) {
delegate.onSuccess(commandMessage, commandResponseMessage);
delegate.onSuccess(commandMessage, commandResultMessage);
}
}

Expand Down
Expand Up @@ -36,7 +36,7 @@
import java.util.concurrent.CopyOnWriteArrayList;

import static java.lang.String.format;
import static org.axonframework.commandhandling.GenericCommandResponseMessage.asCommandResponseMessage;
import static org.axonframework.commandhandling.GenericCommandResultMessage.asCommandResultMessage;

/**
* Implementation of the CommandBus that dispatches commands to the handlers subscribed to that specific command's name.
Expand Down Expand Up @@ -147,8 +147,8 @@ protected <C, R> void handle(CommandMessage<C> command, MessageHandler<? super C
unitOfWork.attachTransaction(transactionManager);
InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors, handler);

CommandResponseMessage<R> result =
asCommandResponseMessage(unitOfWork.executeWithResult(chain::proceed, rollbackConfiguration));
CommandResultMessage<R> result =
asCommandResultMessage(unitOfWork.executeWithResult(chain::proceed, rollbackConfiguration));

callback.onSuccess(command, result);
} catch (Exception e) {
Expand Down
Expand Up @@ -17,7 +17,7 @@

import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.CommandResponseMessage;
import org.axonframework.commandhandling.CommandResultMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -57,8 +57,8 @@ public FailureLoggingCallback(Logger logger, CommandCallback<C, R> delegate) {

@Override
public void onSuccess(CommandMessage<? extends C> commandMessage,
CommandResponseMessage<? extends R> commandResponseMessage) {
delegate.onSuccess(commandMessage, commandResponseMessage);
CommandResultMessage<? extends R> commandResultMessage) {
delegate.onSuccess(commandMessage, commandResultMessage);
}

@Override
Expand Down
Expand Up @@ -19,15 +19,15 @@
import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandExecutionException;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.CommandResponseMessage;
import org.axonframework.commandhandling.CommandResultMessage;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static java.util.Objects.requireNonNull;
import static org.axonframework.commandhandling.GenericCommandResponseMessage.asCommandResponseMessage;
import static org.axonframework.commandhandling.GenericCommandResultMessage.asCommandResultMessage;

/**
* Command Handler Callback that allows the dispatching thread to wait for the result of the callback, using the Future
Expand All @@ -38,13 +38,13 @@
* @author Allard Buijze
* @since 0.6
*/
public class FutureCallback<C, R> extends CompletableFuture<CommandResponseMessage<? extends R>>
public class FutureCallback<C, R> extends CompletableFuture<CommandResultMessage<? extends R>>
implements CommandCallback<C, R> {

@Override
public void onSuccess(CommandMessage<? extends C> commandMessage,
CommandResponseMessage<? extends R> commandResponseMessage) {
super.complete(commandResponseMessage);
CommandResultMessage<? extends R> commandResultMessage) {
super.complete(commandResultMessage);
}

@Override
Expand All @@ -66,12 +66,12 @@ public void onFailure(CommandMessage commandMessage, Throwable cause) {
*
* @see #get()
*/
public CommandResponseMessage<? extends R> getResult() {
public CommandResultMessage<? extends R> getResult() {
try {
return get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return asCommandResponseMessage(null);
return asCommandResultMessage(null);
} catch (ExecutionException e) {
throw asRuntime(e);
}
Expand All @@ -92,14 +92,14 @@ public CommandResponseMessage<? extends R> getResult() {
* @param unit the time unit of the timeout argument
* @return the result of the command handler execution.
*/
public CommandResponseMessage<? extends R> getResult(long timeout, TimeUnit unit) {
public CommandResultMessage<? extends R> getResult(long timeout, TimeUnit unit) {
try {
return get(timeout, unit);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return asCommandResponseMessage(null);
return asCommandResultMessage(null);
} catch (TimeoutException e) {
return asCommandResponseMessage(null);
return asCommandResultMessage(null);
} catch (ExecutionException e) {
throw asRuntime(e);
}
Expand Down
Expand Up @@ -18,7 +18,7 @@

import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.CommandResponseMessage;
import org.axonframework.commandhandling.CommandResultMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -41,7 +41,7 @@ private LoggingCallback() {
}

@Override
public void onSuccess(CommandMessage message, CommandResponseMessage commandResponseMessage) {
public void onSuccess(CommandMessage message, CommandResultMessage commandResultMessage) {
logger.info("Command executed successfully: {}", message.getCommandName());
}

Expand Down

0 comments on commit 480449e

Please sign in to comment.