How to properly use Interceptors on Axon 3?

1,977 views
Skip to first unread message

Mike

unread,
Mar 23, 2017, 6:03:58 PM3/23/17
to Axon Framework Users

Hello everybody,

How exactly I can create one HandlerInterceptor for a specific command on axon 3? Someone suggest me in another topic use HandlerInterceptor to prevalidate my commands before applying the event.


Also, how I could add some metadata (tenant name) to all my commands and events?

I saw some people using CommandHandlerInterceptor and UnitOfWorkListenerAdapter to do that but I dont find those classes on AXON3.

Mike

unread,
Mar 24, 2017, 11:41:36 AM3/24/17
to Axon Framework Users
is this right?


Im using axon 3 , spring-boot-auto-configuration. Looks so weird to me.


@Configuration
public static class CustomAxonConfiguration {

@Autowired
public void registerInterceptors(@Autowired CommandBus commandBus) {
if (commandBus instanceof SimpleCommandBus) {
SimpleCommandBus simpleCommandBus = (SimpleCommandBus) commandBus;

simpleCommandBus.registerHandlerInterceptor(((unitOfWork, interceptorChain) -> {

unitOfWork.transformMessage(message ->
message.andMetaData(Collections.singletonMap("tenant", TenantUtil.get()))
);

return interceptorChain.proceed();
}));
}
}
}

Allard Buijze

unread,
Mar 27, 2017, 8:48:32 AM3/27/17
to Axon Framework Users
Hi Mike,

unfortunately, that's right. RegisterHandlerInterceptor is not (yet)  part of the CommandBus interface.

By the way, you probably want to use the Dispatch Interceptor here, instead of the HandlerInterceptor. The Dispatch Interceptor is always invoked in the thread that dispatches the event (the thread that will also have the ThreadLocal TenantUtil).

Cheers,

Allard

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike

unread,
Mar 27, 2017, 9:48:32 AM3/27/17
to Axon Framework Users
Hi Allard,

Ty so much for the help. Really. Makes all difference know from you that I'm doing something right.

I will look the Dispatch now.

One last question:

am I missing some documentation for this kind of customization in axon 3? I'm talking about code snippet.

Right now, I'm looking the source code to try figure out this.

Mike

unread,
Mar 27, 2017, 11:22:55 AM3/27/17
to Axon Framework Users

Using dispatch:

@Configuration
public class AxonConfiguration {

@Autowired
public void registerInterceptors(CommandBus commandBus) {
if (commandBus instanceof SimpleCommandBus) {

((SimpleCommandBus) commandBus).registerDispatchInterceptor(
(messages) ->
(index, message) ->
message.andMetaData(
ImmutableMap.<String, String>builder()
.put("tenant", TenantUtil.get())
.put("useruid", UserUtil.getUID())
.build()
)
);
}
}

}

Fabrice Mercier

unread,
Sep 18, 2017, 8:13:58 AM9/18/17
to Axon Framework Users
For exemple with Distributed commandBus :

@Autowired
public void configureBeanValidation(DistributedCommandBus commandBus, @Qualifier("localSegment") SimpleCommandBus localSegment) {
commandBus.registerDispatchInterceptor(new BeanValidationInterceptor());
localSegment.registerHandlerInterceptor(new BeanValidationInterceptor());
}

Albert Attard

unread,
Feb 20, 2018, 11:10:18 AM2/20/18
to Axon Framework Users
Hi everyone,

Were there any updates regarding the use of command intercepters (or command interceptors)?

Is it possible to create beans of type MessageDispatchInterceptor and have these picked up by the auto configure automatically (similar to the CorrelationDataProvider for example)?

The examples shown here work, but these are depending on an instance of the command bus to be ready, which may cause cyclic dependencies when putting other AXON related stuff in the same configuration file.

Thank you in advance,
Albert Attard

Allard Buijze

unread,
Feb 23, 2018, 7:16:40 AM2/23/18
to axonfr...@googlegroups.com
Hi Albert,

interceptors aren't automatically picked up from the application context, because there are so many places where interceptors can be configured, that it's unlikely you want them everywhere.

The cyclic dependency problem is a known one. I have some ideas to work around them, which I hope to be able to implement in upcoming releases. I don't expect it will make it in 3.2.

Cheers,

Allard

Op di 20 feb. 2018 om 17:10 schreef Albert Attard <albert...@gmail.com>:
--
Allard Buijze
CTO

E: allard...@axoniq.io
T: +31 6 34 73 99 89

Albert Attard

unread,
Feb 23, 2018, 9:53:57 AM2/23/18
to Axon Framework Users
Thank you, Allard for your feedback.

Here I am spesifically referring to Spring and Spring boot configuration.  Can we have a configurer instead (https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html) of using autowiring?  I am no spring boot expert but used it in the past and works very well.  In the spring boot project, you create a configurer which allows developers using the libraries to customise things as they need.


Following is a basic example


In the xxx-spring-boot-autoconfigure project, you create an interface which developers can implement.

public interface XxxConfigurationCustomizer {
  void customize(XxxBuilder builder);
}


Then you pick it up in the auto-configuration and use it


@Configuration
@ConditionalOnClass({ Xxx.class })
public class XxxAutoConfiguration {

  @Autowired(required = false)
  private List<XxxConfigurationCustomizer> configurationCustomizers;

  @Bean
  @ConditionalOnMissingBean
  public XxxBuilder xxxBuilder(/* Pass other services here */) {
    final DefaultXxx.Builder builder = DefaultXxx.builder();
    /* Initilaise it with the defaults */
    return builder;
  }

  @Bean
  @ConditionalOnMissingBean
  public Xxx xxx(final XxxBuilder builder) {
    customize(builder);
    return builder.build();
  }

  /* Loop thourgh each customizer */
  private XxxBuilder customize(final XxxBuilder builder) {
    if (this.configurationCustomizers != null) {
      for (final XxxConfigurationCustomizer customizer : this.configurationCustomizers) {
        customizer.customize(builder);
      }
    }
    return builder;
  }
}

I do not know if this fits your scenario or not, but we can take it off-line if you like to discuss it further.

Allard Buijze

unread,
Feb 23, 2018, 10:15:49 AM2/23/18
to axonfr...@googlegroups.com
Hi Albert,

that was exactly the approach we were planning to take. Such a Configurer allows us to better control when the exact configuration happens.

Cheers,

Allard

Op vr 23 feb. 2018 om 15:53 schreef Albert Attard <albert...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages