Manoj
Manoj

Reputation: 5867

How to do channel interceptor based on pattern using JAVA DSL in Spring Integration?

We are planning to migrate our code from Spring integration XML to DSL. In XML Version, we are using channel name pattern to do tracing.

For Eg: If channel name has *_EL_*, we intercept the channel and do some logging.

How to do this kind or more simpler in Java dsl.

Upvotes: 2

Views: 2360

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121542

The @GlobalChannelInterceptor is for you. And it is a part of Spring Integration Core.

So, you must do something like this:

@Bean
public MessageChannel bar() {
    return new DirectChannel();
}

@Bean
@GlobalChannelInterceptor(patterns = "*_EL_*")
public WireTap baz() {
    return new WireTap(this.bar());
}

I mean specify the ChannelInterceptor @Bean and mark it with that annotation to make pattern-based interceptions.

UPDATE

The sample test-case which demonstrate the work for @GlobalChannelInterceptor for the auto-created channel from DSL flows:

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

    @Autowired
    private TestGateway testGateway;

    @Autowired
    private PollableChannel intercepted;

    @Test
    public void testIt() {
        this.testGateway.testIt("foo");
        Message<?> receive = this.intercepted.receive(1000);
        assertNotNull(receive);
        assertEquals("foo", receive.getPayload());
    }


    @MessagingGateway
    public interface TestGateway {

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

    }

    @Configuration
    @EnableIntegration
    @IntegrationComponentScan
    public static class ContextConfiguration {

        @Bean
        public IntegrationFlow testFlow() {
            return IntegrationFlows.from("testChannel")
                    .channel("nullChannel")
                    .get();
        }

        @Bean
        public PollableChannel intercepted() {
            return new QueueChannel();
        }

        @Bean
        @GlobalChannelInterceptor(patterns = "*Ch*")
        public WireTap wireTap() {
            return new WireTap(intercepted());
        }

    }

}

Upvotes: 2

Related Questions