-
Notifications
You must be signed in to change notification settings - Fork 108
Adding two features, retain S3 objects after messages deletion and custom S3 object key generation. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glidester
wants to merge
2
commits into
awslabs:master
Choose a base branch
from
glidester:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adding two features, retain S3 objects after messages deletion and custom S3 object key generation. #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
target/ | ||
*.iml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/amazon/sqs/javamessaging/S3KeyGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.sqs.javamessaging; | ||
|
||
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry; | ||
import com.amazonaws.services.sqs.model.SendMessageRequest; | ||
|
||
/** | ||
* Defines the contract of a S3 object key generator to use when persisting | ||
* SQS messages to an S3 bucket. | ||
* | ||
* Your implementation must always return a unique key, even if it is passed | ||
* the same SendMessageRequest or SendMessageBatchRequestEntry instance. | ||
*/ | ||
public interface S3KeyGenerator { | ||
/** | ||
* Method to generate a unique S3 object key from a SendMessageRequest instance. | ||
* | ||
* @param sendMessageRequest the request object for the new message. | ||
* @return a unique S3 object key. | ||
*/ | ||
String generateObjectKey(SendMessageRequest sendMessageRequest); | ||
|
||
/** | ||
* Method to generate a unique S3 object key from a SendMessageBatchRequestEntry | ||
* instance. | ||
* | ||
* @param batchEntry the batch request object for the new message. | ||
* @return a unique S3 object key. | ||
*/ | ||
String generateObjectKey(SendMessageBatchRequestEntry batchEntry); | ||
} |
146 changes: 146 additions & 0 deletions
146
src/test/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClientIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.sqs.javamessaging; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.sqs.AmazonSQS; | ||
import com.amazonaws.services.sqs.AmazonSQSClient; | ||
import com.amazonaws.services.sqs.model.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
|
||
|
||
/** | ||
* Tests the AmazonSQSExtendedClient class. | ||
*/ | ||
public class AmazonSQSExtendedClientIntegrationTest { | ||
|
||
private AmazonSQSClient client; | ||
private AmazonSQS sqs; | ||
private AmazonS3 s3; | ||
private static final AWSCredentials AWS_CREDS = new BasicAWSCredentials("[YOUR_AWS_KEY]", "[YOUR_AWS_SECRET]"); | ||
private static final String S3_BUCKET_NAME = "[YOUR_EXISTING_S3_BUCKET_NAME]"; | ||
private static final String SQS_QUEUE_URL = "[YOUR_EXISTING_SQS_QUEUE_NAME]"; | ||
private static final int SQS_SIZE_LIMIT = 262144; | ||
|
||
/** | ||
* A simple S3KeyGenerator implementation that pre-pends the current date in front of the unique S3 key name. | ||
*/ | ||
class DateKeyGenerator implements S3KeyGenerator { | ||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
@Override | ||
public String generateObjectKey(SendMessageRequest sendMessageRequest) { | ||
return formatter.format(Calendar.getInstance().getTime()) + "/" + UUID.randomUUID().toString(); | ||
} | ||
|
||
@Override | ||
public String generateObjectKey(SendMessageBatchRequestEntry batchEntry) { | ||
return formatter.format(Calendar.getInstance().getTime()) + "/" + UUID.randomUUID().toString(); | ||
} | ||
} | ||
|
||
/** | ||
* A S3KeyGenerator implementation that uses the message meta data to construct a unique S3 key name. | ||
*/ | ||
class MetaBasedKeyGenerator implements S3KeyGenerator { | ||
|
||
private String metaKey; | ||
|
||
public MetaBasedKeyGenerator(String metaKey) { | ||
this.metaKey = metaKey; | ||
} | ||
|
||
private String getPrefix(Map<String, MessageAttributeValue> attribsMap) { | ||
if (attribsMap.containsKey(metaKey)) | ||
return attribsMap.get(metaKey).getStringValue(); | ||
else | ||
return "NOT_SPECIFIED"; | ||
} | ||
|
||
@Override | ||
public String generateObjectKey(SendMessageRequest sendMessageRequest) { | ||
return getPrefix(sendMessageRequest.getMessageAttributes()) + "/" + UUID.randomUUID().toString(); | ||
} | ||
|
||
@Override | ||
public String generateObjectKey(SendMessageBatchRequestEntry batchEntry) { | ||
return getPrefix(batchEntry.getMessageAttributes()) + "/" + UUID.randomUUID().toString(); | ||
} | ||
} | ||
|
||
static final String EVENT_TYPE = "EVENT_TYPE"; | ||
|
||
@Before | ||
public void setupClient() { | ||
|
||
s3 = new AmazonS3Client(AWS_CREDS); | ||
s3.setEndpoint("s3-eu-west-1.amazonaws.com"); | ||
|
||
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration() | ||
.withLargePayloadSupportEnabled(s3, S3_BUCKET_NAME) | ||
//.withS3KeyGenerator(new DateKeyGenerator()); | ||
.withS3KeyGenerator(new MetaBasedKeyGenerator(EVENT_TYPE)) | ||
.withRetainS3Messages(true); | ||
|
||
client = new AmazonSQSClient(AWS_CREDS); //mock(AmazonSQSClient.class) | ||
sqs = new AmazonSQSExtendedClient(client, extendedClientConfiguration); | ||
} | ||
|
||
/* UNCOMMENT THIS TEST ONCE YOU HAVE FILLED IN YOUR AWS DETAILS IN THE STATIC VARS ABOVE | ||
|
||
@Test | ||
public void exercise() { | ||
|
||
int messageLength = SQS_SIZE_LIMIT + 1; | ||
String messageBody = generateString(messageLength); | ||
|
||
MessageAttributeValue messageAttributeValue = new MessageAttributeValue(); | ||
messageAttributeValue.setDataType("String"); | ||
messageAttributeValue.setStringValue("Payment_Success"); | ||
|
||
Map<String,MessageAttributeValue> metaData = new HashMap<String,MessageAttributeValue>(); | ||
metaData.put(EVENT_TYPE, messageAttributeValue); | ||
|
||
SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody) | ||
.withMessageAttributes(metaData); | ||
|
||
SendMessageResult sendResult = sqs.sendMessage(messageRequest); | ||
System.out.println(sendResult); | ||
|
||
ReceiveMessageRequest requestMsg = new ReceiveMessageRequest(SQS_QUEUE_URL).withMaxNumberOfMessages(1); | ||
|
||
ReceiveMessageResult receiveResult = sqs.receiveMessage(requestMsg); | ||
//System.out.println(receiveResult); | ||
|
||
DeleteMessageRequest deleteRequest = new DeleteMessageRequest(SQS_QUEUE_URL, receiveResult.getMessages().get(0).getReceiptHandle()); | ||
|
||
sqs.deleteMessage(deleteRequest); | ||
}*/ | ||
|
||
|
||
private String generateString(int messageLength) { | ||
char[] charArray = new char[messageLength]; | ||
Arrays.fill(charArray, 'x'); | ||
return new String(charArray); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name is
retain
but the description isdelete
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that is because this method controls if the S3 objects are either 'retained' or 'deleted', so yes the description uses the word 'deleted'. Not sure what the issue is tbh? 🤔