|
| 1 | +Advanced Customizations |
| 2 | +======================= |
| 3 | + |
| 4 | +Parameter Resolvers |
| 5 | +------------------- |
| 6 | + |
| 7 | +You can configure additional `ParameterResolver`s by extending the `ParameterResolverFactory` class and creating a file named `/META-INF/service/org.axonframework.common.annotation.ParameterResolverFactory` containing the fully qualified name of the implementing class. |
| 8 | + |
| 9 | +> **Caution** |
| 10 | +> |
| 11 | +> At this moment, OSGi support is limited to the fact that the required headers are mentioned in the manifest file. The automatic detection of `ParameterResolverFactory` instances works in OSGi, but due to classloader limitations, it might be necessary to copy the contents of the `/META-INF/service/org.axonframework.common.annotation.ParameterResolverFactory` file to the OSGi bundle containing the classes to resolve parameters for (i.e. the event handler). |
| 12 | +
|
| 13 | +Meta Annotations |
| 14 | +---------------- |
| 15 | + |
| 16 | +TODO |
| 17 | + |
| 18 | +Customizing Message Handler behavior |
| 19 | +------------------------------------ |
| 20 | + |
| 21 | +HanderEnhancers |
| 22 | +--------------- |
| 23 | + |
| 24 | +It is possible to wrap handlers with custom logic. This differs from HandlerInterceptors in that you have access to the Aggregate member at the time of resolving. |
| 25 | +You can use HandlerEnhancers to intercept and perform checks on groups of `@CommandHandler`s or `@EventHandler`s. |
| 26 | + |
| 27 | +To create a HandlerEnhancer you start by implementing `HandlerEnhancerDefinition` and overriding the `wrapHandler()` method. |
| 28 | +All this method does is give you access to the `MessageHandlingMember<T>` which is an object representing any handler that is specified in the system. |
| 29 | + |
| 30 | +You can then sort these handlers based on their annotations by using the `annotationAttributes(Annotation annotation)` method. This will filter out only those handlers that use that `Annotation`. |
| 31 | + |
| 32 | +For your HandlerEnhancer to run you'll need to create a `META-INF/services/org.axonframework.messaging.annotation.HandlerEnhancerDefinition` file containing |
| 33 | +the fully qualified name of the handler enhancer you would like to use. |
| 34 | + |
| 35 | +Example: |
| 36 | + |
| 37 | +```java |
| 38 | +public class MethodCommandHandlerDefinition implements HandlerEnhancerDefinition { // 1. |
| 39 | + |
| 40 | + @Override |
| 41 | + public <T> MessageHandlingMember<T> wrapHandler(MessageHandlingMember<T> original) { // 2. |
| 42 | + return original.annotationAttributes(CommandHandler.class) // 3. |
| 43 | + .map(attr -> (MessageHandlingMember<T>) new MethodCommandMessageHandlingMember(original, attr)) |
| 44 | + .orElse(original); // 5. |
| 45 | + } |
| 46 | + |
| 47 | + private static class MethodCommandMessageHandlingMember<T> extends WrappedMessageHandlingMember<T>{ |
| 48 | + |
| 49 | + private final String commandName; |
| 50 | + |
| 51 | + private MethodCommandMessageHandlingMember(MessageHandlingMember<T> delegate, |
| 52 | + Map<String, Object> annotationAttributes) { |
| 53 | + super(delegate); |
| 54 | + |
| 55 | + if ("".equals(annotationAttributes.get("commandName"))) { |
| 56 | + commandName = delegate.payloadType().getName(); |
| 57 | + } else { |
| 58 | + commandName = (String) annotationAttributes.get("commandName"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean canHandle(Message<?> message) { |
| 64 | + return super.canHandle(message) && commandName.equals(((CommandMessage) message).getCommandName()); // 4. |
| 65 | + } |
| 66 | + |
| 67 | + public String commandName() { |
| 68 | + return commandName; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +1. Implement the `HandlerEnhancerDefinition` interface |
| 75 | +2. Override the `wrapHandler` method to perform your own logic. |
| 76 | +3. Sort out the types of messages you want to handle, for example any `CommandHandler`s or your own custom annotation even. |
| 77 | +4. Handle the method inside of a `MessageHandlingMember` |
| 78 | +5. If you would like to skip handling just return the original that was passed into the `wrapHandler` method. |
| 79 | + |
| 80 | +To skip all handling of the handler then just throw an exception. |
0 commit comments