I have a spring boot war project and have configured a method with @SqsListener, when run locally, the @SqsListener annotated method works fine and able to poll messages from SQS queue. when the app is built and deployed as part of beanstalk(web environment) it is not receiving the messages anymore. I am not sure why.
The method:
@SqsListener(value = "rishi-demo-queue", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void getSqsMessage(String message) {
logger.info("inside sqs listener, message: {}",message);
}
SQS policy(ec2 role is correct, I have verified):
{
"Version": "2008-10-17",
"Id": "__default_policy_ID",
"Statement": [
{
"Sid": "__owner_statement",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<accountid>:root"
},
"Action": "SQS:*",
"Resource": "arn:aws:sqs:ap-south-1:<accountid>6:rishi-demo-queue"
},
{
"Sid": "__EC2_statement",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<accountid>:role/aws-elasticbeanstalk-ec2-role"
},
"Action": "SQS:*",
"Resource": "arn:aws:sqs:ap-south-1:<accountid>:rishi-demo-queue"
}
]
}
2
Answers
when I moved the method from
@RestController
annotated class to@Service
annotated class, it started working.@SqsListener annotation is processed by Spring’s bean scanning engine, which scans your application’s classpath for classes annotated with certain annotations, including @SqsListener. However, for the annotation to work properly, the class containing the @SqsListener method must be managed by the Spring container.
If the class containing the @SqsListener method is not annotated with @Component or one of its variants(@Repository or @Service) it may not be detected by the Spring bean scanning engine and the @SqsListener annotation will have no effect.