Skip to content
This repository was archived by the owner on Dec 7, 2018. It is now read-only.

Commit e58add7

Browse files
committed
Added code samples for command interceptors.
1 parent 49c47b6 commit e58add7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

part-iii-infrastructure-components/command-dispatching.md

+45
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ There are different types of interceptors: Dispatch Interceptors and Handler Int
158158

159159
Message Dispatch Interceptors are invoked when a command is dispatched on a Command Bus. They have the ability to alter the Command Message, by adding Meta Data, for example, or block the command by throwing an Exception. These interceptors are always invoked on the thread that dispatches the Command.
160160

161+
Let's create a Message Dispatch Interceptor which log each command message being dispatched on a `CommandBus`.
162+
```java
163+
public class MyCommandDispatchInterceptor implements MessageDispatchInterceptor<CommandMessage<?>> {
164+
165+
private static final Logger LOGGER = LoggerFactory.getLogger(MyCommandDispatchInterceptor.class);
166+
167+
@Override
168+
public BiFunction<Integer, CommandMessage<?>, CommandMessage<?>> handle(
169+
List<? extends CommandMessage<?>> messages) {
170+
return (index, command) -> {
171+
LOGGER.info("Dispatching a command {}.", command);
172+
return command;
173+
};
174+
}
175+
}
176+
```
177+
We can register this interceptor with `CommandBus` by doing the following:
178+
```java
179+
commandBus.registerDispatchInterceptor(new MyCommandDispatchInterceptor());
180+
```
181+
161182
#### Structural validation
162183

163184
There is no point in processing a command if it does not contain all required information in the correct format. In fact, a command that lacks information should be blocked as early as possible, preferably even before any transaction is started. Therefore, an interceptor should check all incoming commands for the availability of such information. This is called structural validation.
@@ -180,6 +201,30 @@ Unlike Dispatch Interceptors, Handler Interceptors are invoked in the context of
180201

181202
Handler Interceptors are also typically used to manage transactions around the handling of a command. To do so, register a `TransactionManagingInterceptor`, which in turn is configured with a `TransactionManager` to start and commit \(or roll back\) the actual transaction.
182203

204+
Let's create a Message Handler Interceptor which will allow handling of commands that contain `axonUser` as a value for `userId` field in meta-data. If the `userId` is not present in the meta-data exception will be thrown which will prevent the command from being handled. If the `userId` does not match `axonUser` value, we will not proceed through the chain.
205+
206+
```java
207+
public class MyCommandHandlerInterceptor implements MessageHandlerInterceptor<CommandMessage<?>> {
208+
209+
@Override
210+
public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain)
211+
throws Exception {
212+
CommandMessage<?> command = unitOfWork.getMessage();
213+
String userId = Optional.ofNullable(command.getMetaData().get("userId"))
214+
.map(uId -> (String) uId)
215+
.orElseThrow(IllegalCommandException::new);
216+
if ("axonUser".equals(userId)) {
217+
return interceptorChain.proceed();
218+
}
219+
return null;
220+
}
221+
}
222+
```
223+
We can register the interceptor with the `CommandBus`:
224+
```java
225+
commandBus.registerHandlerInterceptor(new MyCommandHandlerInterceptor());
226+
```
227+
183228
## Distributing the Command Bus
184229

185230
The CommandBus implementations described in earlier only allow Command Messages to be dispatched within a single JVM. Sometimes, you want multiple instances of Command Buses in different JVMs to act as one. Commands dispatched on one JVM's Command Bus should be seamlessly transported to a Command Handler in another JVM while sending back any results.

0 commit comments

Comments
 (0)