Skip to content

Instantly share code, notes, and snippets.

@growse
Created September 27, 2016 10:35
Show Gist options
  • Save growse/ed8da8cc62f3e44b47e4943e9e625d9a to your computer and use it in GitHub Desktop.
Save growse/ed8da8cc62f3e44b47e4943e9e625d9a to your computer and use it in GitHub Desktop.
package WINNING;
import com.amazonaws.ResetException;
import com.amazonaws.event.ProgressEvent;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.services.logs.model.InvalidOperationException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.event.S3EventNotification;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class WinningRunnable implements Runnable {
private final String messageId;
private final String receiptHandle;
private final List<S3EventNotification.S3EventNotificationRecord> records;
private final Semaphore semaphore;
private final String queueURL;
private static final Logger LOGGER = Logger.getLogger(WinningRunnable.class);
private final Path workingDirectory;
private final String logGroupName;
private final IAmazonClientFactory amazonClientFactory;
private final BlockingQueue<Runnable> downloadThreadPoolQueue;
private WinningRunnable(Builder builder) {
if (Stream.of(
builder.messageId,
builder.queueURL,
builder.workingDirectory,
builder.semaphore,
builder.amazonClientFactory,
builder.receiptHandle,
builder.logGroupName,
builder.downloadThreadPoolQueue
).anyMatch(o -> o == null)) {
throw new IllegalArgumentException("All arguments must be non-null");
}
this.messageId = builder.messageId;
this.records = builder.records;
this.receiptHandle = builder.receiptHandle;
this.semaphore = builder.semaphore;
this.queueURL = builder.queueURL;
this.logGroupName = builder.logGroupName;
this.workingDirectory = builder.workingDirectory;
this.amazonClientFactory = builder.amazonClientFactory;
this.downloadThreadPoolQueue = builder.downloadThreadPoolQueue;
}
@Override
public void run() {
LOGGER.info("Awesome")
}
static class Builder {
private String messageId;
private List<S3EventNotification.S3EventNotificationRecord> records;
private String receiptHandle;
private Semaphore semaphore;
private String queueURL;
private String logGroupName;
private Path workingDirectory;
private IAmazonClientFactory amazonClientFactory;
private BlockingQueue<Runnable> downloadThreadPoolQueue;
Builder withMessageId(String messageId) {
this.messageId = messageId;
return this;
}
Builder withRecords(List<S3EventNotification.S3EventNotificationRecord> records) {
this.records = records;
return this;
}
Builder withReceiptHandle(String receiptHandle) {
this.receiptHandle = receiptHandle;
return this;
}
Builder withSemaphore(Semaphore semaphore) {
this.semaphore = semaphore;
return this;
}
Builder withQueueURL(String queueURL) {
this.queueURL = queueURL;
return this;
}
Builder withLogGroupName(String logGroupName) {
this.logGroupName = logGroupName;
return this;
}
Builder withWorkingDirectory(Path workingDirectory) {
this.workingDirectory = workingDirectory;
return this;
}
Builder withAmazonClientFactory(IAmazonClientFactory amazonClientFactory) {
this.amazonClientFactory = amazonClientFactory;
return this;
}
Builder withDownloadThreadPoolQueue(BlockingQueue<Runnable> queue) {
this.downloadThreadPoolQueue = queue;
return this;
}
WinningRunnable build() {
return new WinningRunnable(this);
}
}
}
/***
* A factory to create WinningRunnables with some common values
*/
class WinningRunnableFactory {
private final Semaphore semaphore;
private final Path tempRootDir;
private final String logGroupName;
private final String sqsQueueURL;
private final IAmazonClientFactory amazonClientFactory;
private final BlockingQueue<Runnable> downloadThreadPoolQueue;
WinningRunnableFactory(Semaphore semaphore, String sqsQueueURL, String logGroupName, Path tempRootDir, IAmazonClientFactory amazonClientFactory, BlockingQueue<Runnable> downloadThreadPoolQueue) {
this.semaphore = semaphore;
this.logGroupName = logGroupName;
this.sqsQueueURL = sqsQueueURL;
this.tempRootDir = tempRootDir;
this.amazonClientFactory = amazonClientFactory;
this.downloadThreadPoolQueue = downloadThreadPoolQueue;
}
WinningRunnable getInstance(String messageId, String receiptHandle, List<S3EventNotification.S3EventNotificationRecord> records) {
return new WinningRunnable.Builder()
.withMessageId(messageId)
.withRecords(records)
.withReceiptHandle(receiptHandle)
.withSemaphore(semaphore)
.withQueueURL(sqsQueueURL)
.withLogGroupName(logGroupName)
.withWorkingDirectory(tempRootDir)
.withAmazonClientFactory(amazonClientFactory)
.withDownloadThreadPoolQueue(downloadThreadPoolQueue)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment