Skip to content

Latest commit

 

History

History
61 lines (46 loc) · 2.01 KB

spring-how-to-add-custom-message-processing-decorators.md

File metadata and controls

61 lines (46 loc) · 2.01 KB

Spring - How to add Custom MessageProcessingDecorators

When you add a MessageProcessingDecorator as a bean to the Spring application, it will automatically be attached to all the core message listeners.

@Configuration
public class MyConfiguration {

    @Bean
    public MessageProcessingDecorator myDecorator() {
        return new MyMessageProcessingDecorator();
    }
}

Per Message Listener decorators

It may not be desirable to have a MessageProcessingDecorator attached to every listener and instead you want to apply it to only a subset of listeners. This can be achieved implementing a MessageProcessingDecoratorFactory which will have the opportunity to wrap a message listener, e.g. by looking for an annotation.

An example decorator which will wrap any message listener that has the MyAnnotation annotation.

public class MyDecoratorFactory implements MessageProcessingDecoratorFactory<MyDecorator> {

    @Override
    public Optional<MyDecorator> buildDecorator(
        SqsAsyncClient sqsAsyncClient,
        QueueProperties queueProperties,
        String identifier,
        Object bean,
        Method method
    ) {
        return AnnotationUtils
            .findMethodAnnotation(method, MyAnnotation.class)
            .map(annotation -> new MyDecoratorProperties(annotation.value()))
            .map(properties -> new MyDecorator(properties));
    }
}

you then need to provide the factory as a bean in the application:

@Configuration
class MyConfiguration {

    @Bean
    MyDecoratorFactory myDecoratorFactory() {
        return new MyDecoratorFactory();
    }
}