question

Upvotes
Accepted
1 1 1 2

Timeout when connecting to SQS queue in Java

I'm trying to write a simple sample program in Java which connects to an Amazon SQS queue for news story subscriptions:

API: /alerts/v1/news-stories-subscriptions

I can't seem to connect and keep running into timeouts. I'm trying to work out if this is a firewall / connectivity problem, or something wrong with my code.

Has anyone any experience on connecting and polling messages from news-stories-subscriptions API using Java?

I'm able to generate a token and retrieve cloud credentials, just not connect to an AWS queue

#technologyjavanewsaws
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
23.7k 61 15 21

From the error message, it does look like a network issue. Are you able to use network tools like telnet to rule it out.

telnet sqs.us-east-1.amazonaws.com 443
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
23.7k 61 15 21

Hi @Chris Thomson,

I was able to subscribe to alerts in Java couple of years ago. I used the AWS SDK for Java for this. You can look at AWS code sample on how to use SDK for Java.

If you are still having difficulty to get it to work, let us know and we can try to prepare a Java alerts sample.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
83.1k 281 53 77

@Chris Thomson

The code should look like this:

    public void RetrieveMessage(String accesssKeyId, String secretKey, String sessionToken, String endpoint, String cryptographyKey) {
        
        AwsCredentials credentials = AwsSessionCredentials.create(accesssKeyId, secretKey, sessionToken); 

        SqsClient sqsClient = SqsClient.builder()
          .region(Region.US_EAST_1)        
          .credentialsProvider(()->credentials)    
          .build();
        // Receive messages from the queue
        for(int i=1;i<=10;i++) {
        
            ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
                .queueUrl(endpoint)
                .maxNumberOfMessages(10)
                .waitTimeSeconds(20)
                .build();
            
            List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
            for (Message m : messages) {
...
...
}

The full code is on GitHub.

The dependencies are:

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
 <groupId>software.amazon.awssdk</groupId>
  <artifactId>aws-sdk-java</artifactId>
  <version>2.20.87</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.mashape.unirest/unirest-java -->
<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.