Search in sources :

Example 1 with SqsException

use of software.amazon.awssdk.services.sqs.model.SqsException in project aws-doc-sdk-examples by awsdocs.

the class AddQueueTags method addTags.

// snippet-start:[sqs.java2.add_tags.main]
public static void addTags(SqsClient sqsClient, String queueName) {
    try {
        GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder().queueName(queueName).build();
        GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
        String queueUrl = getQueueUrlResponse.queueUrl();
        HashMap<String, String> addedTags = new HashMap<>();
        addedTags.put("Team", "Development");
        addedTags.put("Priority", "Beta");
        addedTags.put("Accounting ID", "456def");
        TagQueueRequest tagQueueRequest = TagQueueRequest.builder().queueUrl(queueUrl).tags(addedTags).build();
        sqsClient.tagQueue(tagQueueRequest);
        System.out.println("Tags have been applied to " + queueName);
    } catch (SqsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) TagQueueRequest(software.amazon.awssdk.services.sqs.model.TagQueueRequest) SqsException(software.amazon.awssdk.services.sqs.model.SqsException) HashMap(java.util.HashMap) GetQueueUrlResponse(software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse)

Example 2 with SqsException

use of software.amazon.awssdk.services.sqs.model.SqsException in project aws-doc-sdk-examples by awsdocs.

the class DeleteQueue method deleteSQSQueue.

// snippet-start:[sqs.java2.sqs_example.delete_queue]
public static void deleteSQSQueue(SqsClient sqsClient, String queueName) {
    try {
        GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(queueName).build();
        String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
        DeleteQueueRequest deleteQueueRequest = DeleteQueueRequest.builder().queueUrl(queueUrl).build();
        sqsClient.deleteQueue(deleteQueueRequest);
    } catch (SqsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) DeleteQueueRequest(software.amazon.awssdk.services.sqs.model.DeleteQueueRequest) SqsException(software.amazon.awssdk.services.sqs.model.SqsException)

Example 3 with SqsException

use of software.amazon.awssdk.services.sqs.model.SqsException in project aws-doc-sdk-examples by awsdocs.

the class GetQueueAttributes method getAttributes.

// snippet-start:[sqs.java2.get_attributes.main]
public static void getAttributes(SqsClient sqsClient, String queueName) {
    try {
        GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
        String queueUrl = getQueueUrlResponse.queueUrl();
        // Specify the attributes to retrieve.
        List<QueueAttributeName> atts = new ArrayList();
        atts.add(QueueAttributeName.APPROXIMATE_NUMBER_OF_MESSAGES);
        GetQueueAttributesRequest attributesRequest = GetQueueAttributesRequest.builder().queueUrl(queueUrl).attributeNames(atts).build();
        GetQueueAttributesResponse response = sqsClient.getQueueAttributes(attributesRequest);
        Map<String, String> queueAtts = response.attributesAsStrings();
        for (Map.Entry<String, String> queueAtt : queueAtts.entrySet()) System.out.println("Key = " + queueAtt.getKey() + ", Value = " + queueAtt.getValue());
    } catch (SqsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : SqsException(software.amazon.awssdk.services.sqs.model.SqsException) GetQueueAttributesRequest(software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest) GetQueueUrlResponse(software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse) ArrayList(java.util.ArrayList) QueueAttributeName(software.amazon.awssdk.services.sqs.model.QueueAttributeName) Map(java.util.Map) GetQueueAttributesResponse(software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse)

Example 4 with SqsException

use of software.amazon.awssdk.services.sqs.model.SqsException in project aws-doc-sdk-examples by awsdocs.

the class SendMessages method sendMessage.

// snippet-start:[sqs.java2.send_recieve_messages.main]
public static void sendMessage(SqsClient sqsClient, String queueName, String message) {
    try {
        CreateQueueRequest request = CreateQueueRequest.builder().queueName(queueName).build();
        CreateQueueResponse createResult = sqsClient.createQueue(request);
        GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(queueName).build();
        String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
        SendMessageRequest sendMsgRequest = SendMessageRequest.builder().queueUrl(queueUrl).messageBody(message).delaySeconds(5).build();
        sqsClient.sendMessage(sendMsgRequest);
    } catch (SqsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CreateQueueResponse(software.amazon.awssdk.services.sqs.model.CreateQueueResponse) GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) SqsException(software.amazon.awssdk.services.sqs.model.SqsException) CreateQueueRequest(software.amazon.awssdk.services.sqs.model.CreateQueueRequest) SendMessageRequest(software.amazon.awssdk.services.sqs.model.SendMessageRequest)

Example 5 with SqsException

use of software.amazon.awssdk.services.sqs.model.SqsException in project aws-doc-sdk-examples by awsdocs.

the class ListQueueTags method listTags.

// snippet-start:[sqs.java2.list_tags.main]
public static void listTags(SqsClient sqsClient, String queueName) {
    try {
        GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder().queueName(queueName).build();
        GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
        String queueUrl = getQueueUrlResponse.queueUrl();
        ListQueueTagsRequest listQueueTagsRequest = ListQueueTagsRequest.builder().queueUrl(queueUrl).build();
        ListQueueTagsResponse listQueueTagsResponse = sqsClient.listQueueTags(listQueueTagsRequest);
        System.out.println(String.format("ListQueueTags: \tTags for queue %s are %s.\n", queueName, listQueueTagsResponse.tags()));
    } catch (SqsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) SqsException(software.amazon.awssdk.services.sqs.model.SqsException) GetQueueUrlResponse(software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse) ListQueueTagsResponse(software.amazon.awssdk.services.sqs.model.ListQueueTagsResponse) ListQueueTagsRequest(software.amazon.awssdk.services.sqs.model.ListQueueTagsRequest)

Aggregations

SqsException (software.amazon.awssdk.services.sqs.model.SqsException)7 GetQueueUrlRequest (software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest)6 GetQueueUrlResponse (software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse)5 HashMap (java.util.HashMap)2 QueueAttributeName (software.amazon.awssdk.services.sqs.model.QueueAttributeName)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CreateQueueRequest (software.amazon.awssdk.services.sqs.model.CreateQueueRequest)1 CreateQueueResponse (software.amazon.awssdk.services.sqs.model.CreateQueueResponse)1 DeleteQueueRequest (software.amazon.awssdk.services.sqs.model.DeleteQueueRequest)1 GetQueueAttributesRequest (software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest)1 GetQueueAttributesResponse (software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse)1 ListQueueTagsRequest (software.amazon.awssdk.services.sqs.model.ListQueueTagsRequest)1 ListQueueTagsResponse (software.amazon.awssdk.services.sqs.model.ListQueueTagsResponse)1 SendMessageRequest (software.amazon.awssdk.services.sqs.model.SendMessageRequest)1 SetQueueAttributesRequest (software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest)1 TagQueueRequest (software.amazon.awssdk.services.sqs.model.TagQueueRequest)1 UntagQueueRequest (software.amazon.awssdk.services.sqs.model.UntagQueueRequest)1