How to override/replace SqsListenerAnnotationBeanPostProcessor? #1407
-
I have a requirement to intercept processing of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Here's what I did to accomplish my goal. Hopefully is can be a useful example for others to follow.
Here's the outline of the code: public class MySQSListenerAnnotationBeanPostProcessor extends SqsListenerAnnotationBeanPostProcessor {
private final boolean disableCertainEndpoints;
public MySQSListenerAnnotationBeanPostProcessor(Environment environment) {
this.disableCertainEndpoints = /* check Environment for config property */ ;
}
@Override
protected Endpoint createEndpoint(SqsListener annotation) {
String[] queueNames = annotation.queueNames();
boolean anyMatchingQueues = Arrays.stream(queueNames).anyMatch( /* predicate to match queues I'm interested in */);
if (anyMatchingQueues && disableCertainEndpoints) {
return null;
}
return super.createEndpoint(annotation);
}
@Override
protected EndpointRegistrar createEndpointRegistrar() {
// This override is necessary to ignore when createEndpoint() returns null. Maybe this should be a PR in awspring's default implementation?
return new EndpointRegistrar() {
@Override
public void registerEndpoint(Endpoint endpoint) {
if (endpoint != null) {
super.registerEndpoint(endpoint);
}
}
};
}
} @Configuration
@AutoConfigureBefore(SqsAutoConfiguration.class)
public class AnnotationProcessorsConfiguration {
@Bean(SqsBeanNames.SQS_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_BEAN_NAME)
SqsListenerAnnotationBeanPostProcessor sqsListenerProcessor(Environment environment) {
return new MySQSListenerAnnotationBeanPostProcessor(environment);
}
} File META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
|
Beta Was this translation helpful? Give feedback.
-
Hey @erizzo-cfa, sorry for the delay, happy you found the solution, that's indeed the correct way to handle it. This sounds like an interesting use case - we could have an Would you like to open an issue and contribute a PR for that? |
Beta Was this translation helpful? Give feedback.
Here's what I did to accomplish my goal. Hopefully is can be a useful example for others to follow.
SqsListenerAnnotationBeanPostProcessor
, overriding the methodscreateEndpointRegistrar()
andcreateEndpoint(SqsListener annotation)
to avoid creatingEndpoint
s based on my requirement.@Configuration
class that registers my custom processor as a@Bean
using the nameio.awspring.cloud.sqs.config.SqsBeanNames.SQS_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_BEAN_NAME
.I made sure to use
@AutoConfigureBefore(SqsAutoConfiguration.clas})
to make my configuration class process before the built-in awspring auto-configure class.