Search in sources :

Example 1 with QueueAttributeName

use of software.amazon.awssdk.services.sqs.model.QueueAttributeName 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 2 with QueueAttributeName

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

the class SSEncryptionExample method setEncryption.

// snippet-start:[sqs.java2.sqs_sse_example.main]
public static void setEncryption(SqsClient sqsClient, String queueName, String kmsMasterKeyAlias) {
    try {
        GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder().queueName(queueName).build();
        GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
        String queueUrl = getQueueUrlResponse.queueUrl();
        HashMap<QueueAttributeName, String> attributes = new HashMap<>();
        attributes.put(QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias);
        attributes.put(QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140");
        SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder().queueUrl(queueUrl).attributes(attributes).build();
        sqsClient.setQueueAttributes(attRequest);
        System.out.println("The attributes 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) SqsException(software.amazon.awssdk.services.sqs.model.SqsException) HashMap(java.util.HashMap) GetQueueUrlResponse(software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse) QueueAttributeName(software.amazon.awssdk.services.sqs.model.QueueAttributeName) SetQueueAttributesRequest(software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest)

Aggregations

GetQueueUrlResponse (software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse)2 QueueAttributeName (software.amazon.awssdk.services.sqs.model.QueueAttributeName)2 SqsException (software.amazon.awssdk.services.sqs.model.SqsException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GetQueueAttributesRequest (software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest)1 GetQueueAttributesResponse (software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse)1 GetQueueUrlRequest (software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest)1 SetQueueAttributesRequest (software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest)1