Annotation Interface EnableRedisListeners


@Target(TYPE) @Retention(RUNTIME) @Documented @Import(RedisListenerBootstrapConfiguration.class) public @interface EnableRedisListeners
Enable Redis Pub/Sub listener annotated endpoints that are created under the cover by a RedisListenerAnnotationBeanPostProcessor. To be used on @Configuration classes as follows:
@Configuration
@EnableRedisListeners
public class AppConfig {

        @Bean
        public RedisMessageListenerContainer myRedisListenerContainer() {
                RedisMessageListenerContainer factory = new RedisMessageListenerContainer();
                factory.setConnectionFactory(connectionFactory());
                return factory;
        }

        // other @Bean definitions
}

The RedisListenerAnnotationBeanPostProcessor is responsible for creating endpoints.

@EnableRedisListeners enables detection of @RedisListener annotations on any Spring-managed bean in the container. For example, given a class MyService:

package com.acme.foo;

public class MyService {

        @RedisListener(container = "myRedisListenerContainer", topic = "myChannel")
        public void process(String msg) {
                // process incoming message
        }
}

The container to use is identified by the container attribute defining the name of the RedisMessageListenerContainer bean to use. When none is set a RedisMessageListenerContainer bean named redisMessageListenerContainer is assumed to be present.

The following configuration would ensure that every time a Pub/Sub is received on the topic channel named "myChannel", MyService.process() is invoked with the content of the message:

@Configuration
@EnableRedisListeners
public class AppConfig {

        @Bean
        public MyService myService() {
                return new MyService();
        }

        // Redis infrastructure setup
}

Alternatively, if MyService were annotated with @Component, the following configuration would ensure that its @EnableRedisListeners annotated method is invoked with a matching incoming message:

@Configuration
@EnableRedisListeners
@ComponentScan(basePackages = "com.acme.foo")
public class AppConfig {}

Annotated methods can use flexible signature; in particular, it is possible to use the Message abstraction and related annotations, see RedisListener Javadoc for more details. For instance, the following would inject the content of the message and the "channel" name header:

@RedisListener(container = "myRedisListenerContainer", topic = "myChannel")
public void process(String msg, @Header("channel") String channel) {
        // process incoming message
}

These features are abstracted by the MessageHandlerMethodFactory that is responsible for building the necessary invoker to process the annotated method. By default, DefaultMessageHandlerMethodFactory is used.

Implementing RedisListenerConfigurer allows for fine-grained control over endpoint registration via the RedisListenerEndpointRegistrar. For example, the following configures an extra endpoint:

@Configuration
@EnableRedisListeners
public class AppConfig implements RedisListenerConfigurer {

        @Override
        public void configureRedisListeners(RedisListenerEndpointRegistrar registrar) {
                SimpleRedisListenerEndpoint myEndpoint = new SimpleRedisListenerEndpoint();
                // ... configure the endpoint
                registrar.registerEndpoint(endpoint, anotherRedisMessageListenerContainer());
        }

        @Bean
        public MyService myService() {
                return new MyService();
        }

        @Bean
        public RedisMessageListenerContainer anotherRedisMessageListenerContainer() {
                // ...
        }

        // Redis infrastructure setup
}

Beans implementing RedisListenerConfigurer can configure various aspects of annotation-driven endpoints including converter registration, configuration of a Validator, and configuration of HandlerMethodArgumentResolvers. For example, the following configures the charset for a string message converter and disables built-in converter registration:

@Configuration
@EnableRedisListeners
public class AppConfig implements RedisListenerConfigurer {

        @Override
        public void configureMessageConverters(RedisMessageConverters.Builder builder) {
                builder.withStringConverter(StandardCharsets.US_ASCII).registerDefaults(false);
        }

        // Redis infrastructure setup
}

Note that all beans implementing RedisListenerConfigurer will be detected and invoked in a similar fashion. The example above can be translated into a regular bean definition registered in the context in case you use the XML configuration.

Since:
4.1
Author:
Ilyass Bougati
See Also: