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

Commit 62511b2

Browse files
committed
Make some minor adjustments to increase readability
Make some minor adjustments to increase readability #30
1 parent e58add7 commit 62511b2

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

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

+26-10
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,33 @@ 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`.
161+
Let's create a Message Dispatch Interceptor which logs each command message being dispatched on a `CommandBus`.
162162
```java
163163
public class MyCommandDispatchInterceptor implements MessageDispatchInterceptor<CommandMessage<?>> {
164164

165165
private static final Logger LOGGER = LoggerFactory.getLogger(MyCommandDispatchInterceptor.class);
166166

167167
@Override
168-
public BiFunction<Integer, CommandMessage<?>, CommandMessage<?>> handle(
169-
List<? extends CommandMessage<?>> messages) {
168+
public BiFunction<Integer, CommandMessage<?>, CommandMessage<?>> handle( List<? extends CommandMessage<?>> messages) {
170169
return (index, command) -> {
171170
LOGGER.info("Dispatching a command {}.", command);
172171
return command;
173172
};
174173
}
175174
}
176175
```
177-
We can register this interceptor with `CommandBus` by doing the following:
176+
We can register this dispatch interceptor with a `CommandBus` by doing the following:
178177
```java
179-
commandBus.registerDispatchInterceptor(new MyCommandDispatchInterceptor());
178+
public class CommandBusConfiguration {
179+
180+
public CommandBus configureCommandBus() {
181+
CommandBus commandBus = new SimpleCommandBus();
182+
183+
commandBus.registerDispatchInterceptor(new MyCommandDispatchInterceptor());
184+
185+
return commandBus;
186+
}
187+
}
180188
```
181189

182190
#### Structural validation
@@ -201,14 +209,13 @@ Unlike Dispatch Interceptors, Handler Interceptors are invoked in the context of
201209

202210
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.
203211

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.
212+
Let's create a Message Handler Interceptor which will allow handling of commands that contain `axonUser` as a value for the `userId` field contained in the `MetaData`. If the `userId` is not present in the meta-data an exception will be thrown which will prevent the command from being handled. If the `userId`'s value does not match `axonUser`, we will not proceed through the chain.
205213

206214
```java
207215
public class MyCommandHandlerInterceptor implements MessageHandlerInterceptor<CommandMessage<?>> {
208216

209217
@Override
210-
public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain)
211-
throws Exception {
218+
public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception {
212219
CommandMessage<?> command = unitOfWork.getMessage();
213220
String userId = Optional.ofNullable(command.getMetaData().get("userId"))
214221
.map(uId -> (String) uId)
@@ -220,9 +227,18 @@ public class MyCommandHandlerInterceptor implements MessageHandlerInterceptor<Co
220227
}
221228
}
222229
```
223-
We can register the interceptor with the `CommandBus`:
230+
We can register the handler interceptor with a `CommandBus` like so:
224231
```java
225-
commandBus.registerHandlerInterceptor(new MyCommandHandlerInterceptor());
232+
public class CommandBusConfiguration {
233+
234+
public CommandBus configureCommandBus() {
235+
CommandBus commandBus = new SimpleCommandBus();
236+
237+
commandBus.registerHandlerInterceptor(new MyCommandHandlerInterceptor());
238+
239+
return commandBus;
240+
}
241+
}
226242
```
227243

228244
## Distributing the Command Bus

0 commit comments

Comments
 (0)