|
| 1 | +/* |
| 2 | + * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.sqs.javamessaging; |
| 17 | + |
| 18 | +import com.amazonaws.auth.AWSCredentials; |
| 19 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 20 | +import com.amazonaws.services.s3.AmazonS3; |
| 21 | +import com.amazonaws.services.s3.AmazonS3Client; |
| 22 | +import com.amazonaws.services.sqs.AmazonSQS; |
| 23 | +import com.amazonaws.services.sqs.AmazonSQSClient; |
| 24 | +import com.amazonaws.services.sqs.model.*; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.text.SimpleDateFormat; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests the AmazonSQSExtendedClient class. |
| 34 | + */ |
| 35 | +public class AmazonSQSExtendedClientIntegrationTest { |
| 36 | + |
| 37 | + private AmazonSQSClient client; |
| 38 | + private AmazonSQS sqs; |
| 39 | + private AmazonS3 s3; |
| 40 | + private static final AWSCredentials AWS_CREDS = new BasicAWSCredentials("[YOUR_AWS_KEY]", "[YOUR_AWS_SECRET]"); |
| 41 | + private static final String S3_BUCKET_NAME = "[YOUR_EXISTING_S3_BUCKET_NAME]"; |
| 42 | + private static final String SQS_QUEUE_URL = "[YOUR_EXISTING_SQS_QUEUE_NAME]"; |
| 43 | + private static final int SQS_SIZE_LIMIT = 262144; |
| 44 | + |
| 45 | + /** |
| 46 | + * A simple S3KeyGenerator implementation that pre-pends the current date in front of the unique S3 key name. |
| 47 | + */ |
| 48 | + class DateKeyGenerator implements S3KeyGenerator { |
| 49 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 50 | + |
| 51 | + @Override |
| 52 | + public String generateObjectKey(SendMessageRequest sendMessageRequest) { |
| 53 | + return formatter.format(Calendar.getInstance().getTime()) + "/" + UUID.randomUUID().toString(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String generateObjectKey(SendMessageBatchRequestEntry batchEntry) { |
| 58 | + return formatter.format(Calendar.getInstance().getTime()) + "/" + UUID.randomUUID().toString(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * A S3KeyGenerator implementation that uses the message meta data to construct a unique S3 key name. |
| 64 | + */ |
| 65 | + class MetaBasedKeyGenerator implements S3KeyGenerator { |
| 66 | + |
| 67 | + private String metaKey; |
| 68 | + |
| 69 | + public MetaBasedKeyGenerator(String metaKey) { |
| 70 | + this.metaKey = metaKey; |
| 71 | + } |
| 72 | + |
| 73 | + private String getPrefix(Map<String, MessageAttributeValue> attribsMap) { |
| 74 | + if (attribsMap.containsKey(metaKey)) |
| 75 | + return attribsMap.get(metaKey).getStringValue(); |
| 76 | + else |
| 77 | + return "NOT_SPECIFIED"; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String generateObjectKey(SendMessageRequest sendMessageRequest) { |
| 82 | + return getPrefix(sendMessageRequest.getMessageAttributes()) + "/" + UUID.randomUUID().toString(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String generateObjectKey(SendMessageBatchRequestEntry batchEntry) { |
| 87 | + return getPrefix(batchEntry.getMessageAttributes()) + "/" + UUID.randomUUID().toString(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + static final String EVENT_TYPE = "EVENT_TYPE"; |
| 92 | + |
| 93 | + @Before |
| 94 | + public void setupClient() { |
| 95 | + |
| 96 | + s3 = new AmazonS3Client(AWS_CREDS); |
| 97 | + s3.setEndpoint("s3-eu-west-1.amazonaws.com"); |
| 98 | + |
| 99 | + ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration() |
| 100 | + .withLargePayloadSupportEnabled(s3, S3_BUCKET_NAME) |
| 101 | + //.withS3KeyGenerator(new DateKeyGenerator()); |
| 102 | + .withS3KeyGenerator(new MetaBasedKeyGenerator(EVENT_TYPE)) |
| 103 | + .withRetainS3Messages(true); |
| 104 | + |
| 105 | + client = new AmazonSQSClient(AWS_CREDS); //mock(AmazonSQSClient.class) |
| 106 | + sqs = new AmazonSQSExtendedClient(client, extendedClientConfiguration); |
| 107 | + } |
| 108 | + |
| 109 | + /* UNCOMMENT THIS TEST ONCE YOU HAVE FILLED IN YOUR AWS DETAILS IN THE STATIC VARS ABOVE |
| 110 | +
|
| 111 | + @Test |
| 112 | + public void exercise() { |
| 113 | +
|
| 114 | + int messageLength = SQS_SIZE_LIMIT + 1; |
| 115 | + String messageBody = generateString(messageLength); |
| 116 | +
|
| 117 | + MessageAttributeValue messageAttributeValue = new MessageAttributeValue(); |
| 118 | + messageAttributeValue.setDataType("String"); |
| 119 | + messageAttributeValue.setStringValue("Payment_Success"); |
| 120 | +
|
| 121 | + Map<String,MessageAttributeValue> metaData = new HashMap<String,MessageAttributeValue>(); |
| 122 | + metaData.put(EVENT_TYPE, messageAttributeValue); |
| 123 | +
|
| 124 | + SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody) |
| 125 | + .withMessageAttributes(metaData); |
| 126 | +
|
| 127 | + SendMessageResult sendResult = sqs.sendMessage(messageRequest); |
| 128 | + System.out.println(sendResult); |
| 129 | +
|
| 130 | + ReceiveMessageRequest requestMsg = new ReceiveMessageRequest(SQS_QUEUE_URL).withMaxNumberOfMessages(1); |
| 131 | +
|
| 132 | + ReceiveMessageResult receiveResult = sqs.receiveMessage(requestMsg); |
| 133 | + //System.out.println(receiveResult); |
| 134 | +
|
| 135 | + DeleteMessageRequest deleteRequest = new DeleteMessageRequest(SQS_QUEUE_URL, receiveResult.getMessages().get(0).getReceiptHandle()); |
| 136 | +
|
| 137 | + sqs.deleteMessage(deleteRequest); |
| 138 | + }*/ |
| 139 | + |
| 140 | + |
| 141 | + private String generateString(int messageLength) { |
| 142 | + char[] charArray = new char[messageLength]; |
| 143 | + Arrays.fill(charArray, 'x'); |
| 144 | + return new String(charArray); |
| 145 | + } |
| 146 | +} |
0 commit comments