Skip to content
Advertisement

AWS SQS – MessageConsumer stops to receive messages after a while

My application registers a listeners to a SQS queue (queue itself is populated by a SNS topic). When I start the application, message consumer is working as expected but after a while it stops to receive any messages. Can it be that consumer is shutting down after a while?

Suggestions or comments would be much appreciated.

SQSConnection:

@Bean
public SQSConnection amazonSQSConnection(
      @Value("${aws.access.key}") String accessKey,
      @Value("${aws.secret.key}") String secretKey) throws JMSException {


BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonSQSClientBuilder client = AmazonSQSClientBuilder
        .standard()
        .withRegion(Regions.GovCloud)
        .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials));
SQSConnectionFactory connectionFactory = new SQSConnectionFactory(new ProviderConfiguration(), client);

return connectionFactory.createConnection();
}

Consummer:

 @Bean
 public MessageConsumer workOrderChangeConsumer(
      SQSConnection connection,
      WorkOrderKittingService workOrderKittingService,
      AuthenticationProvider authProvider,
      @Value("${app.user.name}") String appUserName,
      @Value("${aws.sqs.workorder.change.queue}") String woChangeQueue) throws JMSException {

Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Queue queue = session.createQueue(woChangeQueue);

WorkOrderChangeIngestor workOrderChangeIngestor = new WorkOrderChangeIngestor(
        workOrderKittingService,
        authProvider,
        appUserName);

MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(workOrderChangeIngestor);
connection.start();

return consumer;
}

Advertisement

Answer

You’re trying to manage the connection lifecycle by yourself.

I recommend you to let spring manage that for you, by using spring-cloud-aws

https://docs.spring.io/spring-cloud-aws/docs/2.2.3.RELEASE/reference/html/#receiving-a-message

You can create a listener through annotations:

@Component
public class MyMessageHandler {

    @SqsListener("queueName")
    void handle(String message) {
        ...
        throw new MyException("something went wrong");
    }

    @MessageExceptionHandler(MyException.class)
    void handleException(MyException e) {
        ...
    }
}
Advertisement