Skip to content

Make the key generator configurable #21

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;

import com.amazonaws.AmazonClientException;
Expand Down Expand Up @@ -1250,7 +1249,7 @@ private SendMessageBatchRequestEntry storeMessageInS3(SendMessageBatchRequestEnt

checkMessageAttributes(batchEntry.getMessageAttributes());

String s3Key = UUID.randomUUID().toString();
String s3Key = clientConfiguration.getKeyGenerator().generate();

// Read the content of the message from message body
String messageContentStr = batchEntry.getMessageBody();
Expand Down Expand Up @@ -1283,7 +1282,7 @@ private SendMessageRequest storeMessageInS3(SendMessageRequest sendMessageReques

checkMessageAttributes(sendMessageRequest.getMessageAttributes());

String s3Key = UUID.randomUUID().toString();
String s3Key = clientConfiguration.getKeyGenerator().generate();

// Read the content of the message from message body
String messageContentStr = sendMessageRequest.getMessageBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.NotThreadSafe;

import java.util.List;

/**
* Amazon SQS extended client configuration options such as Amazon S3 client,
* bucket name, and message size threshold for large-payload messages.
Expand All @@ -36,6 +34,7 @@ public class ExtendedClientConfiguration {
private boolean largePayloadSupport = false;
private boolean alwaysThroughS3 = false;
private int messageSizeThreshold = SQSExtendedClientConstants.DEFAULT_MESSAGE_SIZE_THRESHOLD;
private KeyGenerator keyGenerator = KeyGenerator.DEFAULT_KEY_GENERATOR;

public ExtendedClientConfiguration() {
s3 = null;
Expand All @@ -48,6 +47,7 @@ public ExtendedClientConfiguration(ExtendedClientConfiguration other) {
this.largePayloadSupport = other.largePayloadSupport;
this.alwaysThroughS3 = other.alwaysThroughS3;
this.messageSizeThreshold = other.messageSizeThreshold;
this.keyGenerator = other.keyGenerator;
}

/**
Expand Down Expand Up @@ -178,6 +178,40 @@ public int getMessageSizeThreshold() {
return messageSizeThreshold;
}

/**
* Sets the key generator for storing message payloads in Amazon S3.
*
* @param keyGenerator
* The key generator to be used for storing in Amazon S3.
* Default: {@link KeyGenerator#DEFAULT_KEY_GENERATOR}.
*/
public void setKeyGenerator(KeyGenerator keyGenerator) {
this.keyGenerator = keyGenerator;
}

/**
* Sets the key generator for storing message payloads in Amazon S3.
*
* @param keyGenerator
* The key generator to be used for storing in Amazon S3.
* Default: {@link KeyGenerator#DEFAULT_KEY_GENERATOR}.
* @return the updated ExtendedClientConfiguration object.
*/
public ExtendedClientConfiguration withKeyGenerator(KeyGenerator keyGenerator) {
setKeyGenerator(keyGenerator);
return this;
}

/**
* Gets the key generator for storing message payloads in Amazon S3.
*
* @return The key generator to be used for storing in Amazon S3.
* Default: {@link KeyGenerator#DEFAULT_KEY_GENERATOR}.
*/
public KeyGenerator getKeyGenerator() {
return keyGenerator;
}

/**
* Sets whether or not all messages regardless of their payload size should
* be stored in Amazon S3.
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/amazon/sqs/javamessaging/KeyGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.amazon.sqs.javamessaging;

import java.util.UUID;

/**
* Class for generating keys for storing messages to Amazon S3.
*/
public class KeyGenerator {

public static final KeyGenerator DEFAULT_KEY_GENERATOR = new KeyGenerator();

/**
* Generates a unique for use when storing to Amazon S3.
*
* @return a unique key for writing to S3.
*/
public String generate() {
return UUID.randomUUID().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public void testWhenSendLargeMessageThenPayloadIsStoredInS3() {
verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}

@Test
public void testWhenSendLargeMessageThenKeyIsFromKeyGenerator() {
int messageLength = LESS_THAN_SQS_SIZE_LIMIT;
String messageBody = generateStringWithLength(messageLength);
KeyGenerator keyGenerator = spy(new KeyGenerator());
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withAlwaysThroughS3(true)
.withKeyGenerator(keyGenerator);
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));

SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
sqsExtended.sendMessage(messageRequest);

verify(keyGenerator, times(1)).generate();
}

@Test
public void testWhenSendSmallMessageThenS3IsNotUsed() {
int messageLength = SQS_SIZE_LIMIT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public void testCopyConstructor() {

boolean alwaysThroughS3 = true;
int messageSizeThreshold = 500;
KeyGenerator keyGenerator = new KeyGenerator();

ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration();

extendedClientConfig.withLargePayloadSupportEnabled(s3, s3BucketName)
.withAlwaysThroughS3(alwaysThroughS3).withMessageSizeThreshold(messageSizeThreshold);
.withAlwaysThroughS3(alwaysThroughS3).withMessageSizeThreshold(messageSizeThreshold)
.withKeyGenerator(keyGenerator);

ExtendedClientConfiguration newExtendedClientConfig = new ExtendedClientConfiguration(extendedClientConfig);

Expand All @@ -56,6 +58,7 @@ public void testCopyConstructor() {
Assert.assertTrue(newExtendedClientConfig.isLargePayloadSupportEnabled());
Assert.assertEquals(alwaysThroughS3, newExtendedClientConfig.isAlwaysThroughS3());
Assert.assertEquals(messageSizeThreshold, newExtendedClientConfig.getMessageSizeThreshold());
Assert.assertEquals(keyGenerator, newExtendedClientConfig.getKeyGenerator());

Assert.assertNotSame(newExtendedClientConfig, extendedClientConfig);
}
Expand Down