2

I would like to intercept all spring integration gateways via AOP.

Is it possible to do that? If not what might be best way to do log input object coming to gateway?

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class AdviceExample {

  @Autowired
  private TestGateway testGateway;

  @Test
  public void testIt() {
    System.out.println(this.testGateway.testIt("foo"));
  }


  @MessagingGateway
  public interface TestGateway {

    @Gateway(requestChannel = "testChannel")
    @CustomAnnotation
    String testIt(String payload);

  }

  @Configuration
  @EnableIntegration
  @IntegrationComponentScan
  @EnableMessageHistory
  @EnableAspectJAutoProxy
  public static class ContextConfiguration {
    LoggingHandler logger = new LoggingHandler(LoggingHandler.Level.INFO.name());

    @Bean
    public IntegrationFlow testFlow() {
      return IntegrationFlows.from("testChannel")
                             .transform("payload.toUpperCase()")
                             .channel("testChannel")
                             .transform("payload.concat(' Manoj')")
                             .channel("testChannel")
                             .handle(logger)
                             .get();
    }

    @Bean
    public GatewayAdvice gtwyAdvice(){
      return new GatewayAdvice();
    }

  }

  @Retention(value = RetentionPolicy.RUNTIME)
  @Target(value = ElementType.METHOD)
  @Inherited
  public @interface CustomAnnotation{

  }

  @Aspect
  public static class GatewayAdvice {

    @Before("execution(* advice.AdviceExample.TestGateway.testIt(*))")
    public void beforeAdvice() {
        System.out.println("Before advice called...");
    }

    @Before("@annotation(advice.AdviceExample.CustomAnnotation)")
    public void beforeAnnotationAdvice() {
      System.out.println("Before annotation advice called...");
  }
  }

}
Manoj
  • 5,707
  • 19
  • 56
  • 86

1 Answers1

3

Yes, you can do that. Take a look to the standard Spring AOP Framework. Since all those @Gateway are beans in the end you can add for them any Advice by their bean names and for the specific method, if that. For example we often suggest to use @Transactional on gateway's methods. And this is exactly a sample "how to use AOP on integration gateway".

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you, it works, but annotation style advice not working. If you see my code above beforeAnnotationAdvice is not getting called, but beforeAdvice is called. This is because, annotations are not inheritable? – Manoj Aug 01 '15 at 22:35