Long polling is not enabled in Amazon SQS Medium

Enable long polling to reduce the number of empty responses when no messages are available in reply to a ReceiveMessage request and to eliminate false empty responses.

Detector ID
java/amazon-sqs-enable-long-polling@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1public class AmazonSqsEnableLongPollingNoncompliant {
2
3    public static void main(String[] args) {
4
5        final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
6        String queueNamePattern = "sqs-poc-fifo-queue";
7        int numOfQueues = 6;
8        String queue_url="QUEUE_URL";
9        createQueuesNoncompliant(sqs, queueNamePattern, numOfQueues, queue_url);
10    }
11
12    /**
13     * Create FIFO queues based on the Queue Name pattern and the number of queues
14     * needed
15     *
16     * @param sqs
17     * @param queueNamePattern
18     * @param numOfQueues
19     */
20    public static void createQueuesNoncompliant(AmazonSQS sqs,
21                                                String queueNamePattern,
22                                                int numOfQueues,
23                                                String queue_url) {
24
25        Map<String, String> attributes = new HashMap<String, String>();
26        attributes.put("FifoQueue", "true");
27        attributes.put("ContentBasedDeduplication", "true");
28        attributes.put("DelaySeconds", "0");
29        attributes.put("MessageRetentionPeriod", "86400"); // this is 24 shours
30
31        for (int i = 1; i < numOfQueues + 1; i++) {
32            String queueName = queueNamePattern + "-" + i + ".fifo";
33            // Noncompliant: avoids enabling long polling.
34            CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
35            try {
36                CreateQueueResult res = sqs.createQueue(createQueueRequest);
37                System.out.println("SQS Queue created, queue url: " + res.getQueueUrl());
38            } catch (QueueNameExistsException e) {
39                System.out.println("A queue with name " + queueName
40                        + " already exist. Renaming it with _v1 suffix before next attempt");
41                queueName = queueNamePattern + "-" + i + "-v1" + "-src.fifo";
42                createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
43                sqs.createQueue(createQueueRequest);
44
45            } catch (QueueDeletedRecentlyException e) {
46                try {
47                    System.out.println(e.getMessage());
48                    System.out.println("Queue recently deleted. Sleeping for 65 seconds before recreating the queue");
49                    Thread.sleep(65000);
50                    createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
51                    sqs.createQueue(createQueueRequest);
52
53                } catch (InterruptedException e1) {
54                    e1.printStackTrace();
55                }
56            }
57            ReceiveMessageRequest receive_request = new ReceiveMessageRequest()
58                    .withQueueUrl(queue_url);
59            sqs.receiveMessage(receive_request);
60
61        }
62
63    }
64}

Compliant example

1public class AmazonSqsEnableLongPollingCompliant {
2
3    public static void main(String[] args) {
4
5        final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
6        String queueNamePattern = "sqs-poc-fifo-queue";
7        int numOfQueues = 6;
8        String queue_url="QUEUE_URL";
9        createQueues(sqs, queueNamePattern, numOfQueues, queue_url);
10    }
11
12    /**
13     * Create FIFO queues based on the Queue Name pattern and the number of queues
14     * needed
15     *
16     * @param sqs
17     * @param queueNamePattern
18     * @param numOfQueues
19     */
20    public static void createQueues(AmazonSQS sqs,
21                                    String queueNamePattern,
22                                    int numOfQueues,
23                                    String queue_url) {
24
25        Map<String, String> attributes = new HashMap<String, String>();
26        attributes.put("FifoQueue", "true");
27        attributes.put("ContentBasedDeduplication", "true");
28        attributes.put("DelaySeconds", "0");
29        attributes.put("MessageRetentionPeriod", "86400"); // this is 24 shours
30        attributes.put("ReceiveMessageWaitTimeSeconds", "20"); // enables long polling
31
32        for (int i = 1; i < numOfQueues + 1; i++) {
33            String queueName = queueNamePattern + "-" + i + ".fifo";
34            // Compliant: enables long polling for efficiency.
35            CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
36            try {
37                CreateQueueResult res = sqs.createQueue(createQueueRequest);
38                System.out.println("SQS Queue created, queue url: " + res.getQueueUrl());
39            } catch (QueueNameExistsException e) {
40                System.out.println("A queue with name " + queueName
41                        + " already exist. Renaming it with _v1 suffix before next attempt");
42                queueName = queueNamePattern + "-" + i + "-v1" + "-src.fifo";
43                createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
44                sqs.createQueue(createQueueRequest);
45
46            } catch (QueueDeletedRecentlyException e) {
47                try {
48                    System.out.println(e.getMessage());
49                    System.out.println("Queue recently deleted. Sleeping for 65 seconds before recreating the queue");
50                    Thread.sleep(65000);
51                    createQueueRequest = new CreateQueueRequest(queueName).withAttributes(attributes);
52                    sqs.createQueue(createQueueRequest);
53
54                } catch (InterruptedException e1) {
55                    e1.printStackTrace();
56                }
57            }
58            ReceiveMessageRequest receive_request = new ReceiveMessageRequest()
59                    .withQueueUrl(queue_url);
60            sqs.receiveMessage(receive_request);
61
62        }
63
64    }
65}