Search in sources :

Example 6 with GetQueueUrlRequest

use of software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest 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)

Example 7 with GetQueueUrlRequest

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

the class SendReceiveMessages method main.

public static void main(String[] args) {
    SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_2).build();
    try {
        CreateQueueRequest request = CreateQueueRequest.builder().queueName(QUEUE_NAME).build();
        CreateQueueResponse createResult = sqsClient.createQueue(request);
        GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build();
        String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
        SendMessageRequest sendMsgRequest = SendMessageRequest.builder().queueUrl(queueUrl).messageBody("hello world").delaySeconds(5).build();
        sqsClient.sendMessage(sendMsgRequest);
        // Send multiple messages to the queue
        SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder().queueUrl(queueUrl).entries(SendMessageBatchRequestEntry.builder().messageBody("Hello from message 1").id("msg_1").build(), SendMessageBatchRequestEntry.builder().messageBody("Hello from message 2").delaySeconds(10).id("msg_2").build()).build();
        sqsClient.sendMessageBatch(sendBatchRequest);
        // Receive messages from the queue
        ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
        List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
        // Print out the messages
        for (Message m : messages) {
            System.out.println("\n" + m.body());
        }
    } catch (QueueNameExistsException e) {
        throw e;
    }
}
Also used : CreateQueueResponse(software.amazon.awssdk.services.sqs.model.CreateQueueResponse) GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) QueueNameExistsException(software.amazon.awssdk.services.sqs.model.QueueNameExistsException) SendMessageBatchRequest(software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest) ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) Message(software.amazon.awssdk.services.sqs.model.Message) CreateQueueRequest(software.amazon.awssdk.services.sqs.model.CreateQueueRequest) SqsClient(software.amazon.awssdk.services.sqs.SqsClient) SendMessageRequest(software.amazon.awssdk.services.sqs.model.SendMessageRequest)

Aggregations

GetQueueUrlRequest (software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest)7 SqsException (software.amazon.awssdk.services.sqs.model.SqsException)6 GetQueueUrlResponse (software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse)4 HashMap (java.util.HashMap)2 CreateQueueRequest (software.amazon.awssdk.services.sqs.model.CreateQueueRequest)2 CreateQueueResponse (software.amazon.awssdk.services.sqs.model.CreateQueueResponse)2 SendMessageRequest (software.amazon.awssdk.services.sqs.model.SendMessageRequest)2 SqsClient (software.amazon.awssdk.services.sqs.SqsClient)1 DeleteQueueRequest (software.amazon.awssdk.services.sqs.model.DeleteQueueRequest)1 ListQueueTagsRequest (software.amazon.awssdk.services.sqs.model.ListQueueTagsRequest)1 ListQueueTagsResponse (software.amazon.awssdk.services.sqs.model.ListQueueTagsResponse)1 Message (software.amazon.awssdk.services.sqs.model.Message)1 QueueAttributeName (software.amazon.awssdk.services.sqs.model.QueueAttributeName)1 QueueNameExistsException (software.amazon.awssdk.services.sqs.model.QueueNameExistsException)1 ReceiveMessageRequest (software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest)1 SendMessageBatchRequest (software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest)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