@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsOneMessage() { final AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); final String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .take(1) // .test() // .awaitDone(10, TimeUnit.SECONDS) // .assertComplete() // .assertValue("body1"); final InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(1)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsOneMessageAndHonoursBackpressure() { final AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); final String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .test(0) // .requestMore(1) // .assertValue("body1")// .assertNotComplete() // .cancel(); final InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(1)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsNoMessagesThenSecondCallReturnsTwoMessages() { final AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); final String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())).thenReturn(new ReceiveMessageResult()) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"), new Message().withBody("body2"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .take(2) // .test() // .awaitDone(10, TimeUnit.SECONDS) // .assertComplete() // .assertValues("body1", "body2"); final InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(2)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@Test public void shutdownOfSqsAndS3FactoryCreatedClientsOccursWhenS3DeleteObjectFails() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); String s3Id = "123"; SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.of(s3Id), new SqsMessage.Service(Optional.of(() -> s3), () -> sqs, Optional.of(s3), sqs, QUEUE, Optional.of(BUCKET))); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)) .thenReturn(new DeleteMessageResult()); Mockito.doThrow(new RuntimeException()).when(s3).deleteObject(BUCKET, s3Id); try { m.deleteMessage(Client.FROM_FACTORY); Assert.fail(); } catch (RuntimeException e) { // do nothing } InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).deleteObject(BUCKET, s3Id); inorder.verify(s3, Mockito.times(1)).shutdown(); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs, s3); }
public Processor(ProcessorConfig config) { ProfileCredentialsProvider creds = new ProfileCredentialsProvider(config.profile()); creds.getCredentials(); // credible credential criteria if (config.disableCerts()) System.setProperty("com.amazonaws.sdk.disableCertChecking", "true"); // Rekognition init rek = new AmazonRekognitionClient(creds); if (config.endpointOverride()) rek.setEndpoint(config.endpoint()); minConfidence = Integer.parseInt(config.confidence()); // The SQS queue to find jobs on sqs = new AmazonSQSClient(creds); queueUrl = sqs.createQueue(config.queue()).getQueueUrl(); // Processors if (config.wantCloudSearch()) processors.add(new CloudSearchIndexer(creds, config.cloudSearch())); if (config.wantDynamo()) processors.add(new DynamoWriter(creds, config.dynamo())); if (config.wantTags3()) processors.add(new S3ObjectTagger(creds, config.tagPrefix())); // Executor Service int maxWorkers = Integer.parseInt(config.concurrency()); executor = new ThreadPoolExecutor( 1, maxWorkers, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxWorkers * 2, false), new CallerRunsPolicy() // prevents backing up too many jobs ); maxImagesToProcess = Long.parseLong(config.max()); }
Service(Optional<Func0<AmazonS3Client>> s3Factory, Func0<AmazonSQSClient> sqsFactory, Optional<AmazonS3Client> s3, AmazonSQSClient sqs, String queueName, Optional<String> bucketName) { Preconditions.checkNotNull(s3Factory); Preconditions.checkNotNull(sqsFactory); Preconditions.checkNotNull(s3); Preconditions.checkNotNull(sqs); Preconditions.checkNotNull(queueName); Preconditions.checkNotNull(bucketName); this.s3Factory = s3Factory; this.sqsFactory = sqsFactory; this.s3 = s3; this.sqs = sqs; this.queueName = queueName; this.bucketName = bucketName; }
private static Observable<SqsMessage> get(AmazonSQSClient sqs, String queueName, Optional<String> bucketName, Optional<AmazonS3Client> s3, Service service, int waitTimeSeconds) { return Observable.defer(() -> { String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl(); return Observable .just(sqs.receiveMessage(request(queueName, waitTimeSeconds)) // .getMessages() // .stream() // .map(m -> Sqs.getNextMessage(m, queueUrl, bucketName, s3, sqs, service)) // .collect(Collectors.toList())) // .concatWith(Observable .defer(() -> Observable.just(sqs.receiveMessage(request(queueName, 0)) // .getMessages() // .stream() // .map(m -> Sqs.getNextMessage(m, queueUrl, bucketName, s3, sqs, service)) // .collect(Collectors.toList()))) // .repeat()) .takeWhile(list -> !list.isEmpty()) // .flatMapIterable(Functions.identity()) // .filter(opt -> opt.isPresent()).map(opt -> opt.get()); });// }
@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsOneMessage() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .take(1) // .to(test()) // .awaitTerminalEvent() // .assertCompleted() // .assertValue("body1"); InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(1)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsOneMessageAndHonoursBackpressure() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .to(testWithRequest(0)) // .requestMore(1) // .assertValue("body1")// .assertNotCompleted() // .unsubscribe(); InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(1)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@Test(timeout = 5000) public void testFirstCallToReceiveMessagesReturnsNoMessagesThenSecondCallReturnsTwoMessages() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); String queueName = "queue"; Mockito.when(sqs.getQueueUrl(queueName)).thenAnswer(x -> new GetQueueUrlResult().withQueueUrl(queueName)); Mockito.when(sqs.receiveMessage(Mockito.<ReceiveMessageRequest>any())).thenReturn(new ReceiveMessageResult()) .thenReturn(new ReceiveMessageResult().withMessages(new Message().withBody("body1"), new Message().withBody("body2"))); Sqs.queueName(queueName) // .sqsFactory(() -> sqs) // .messages() // .map(m -> m.message()) // .doOnError(Throwable::printStackTrace) // .take(2) // .to(test()) // .awaitTerminalEvent() // .assertCompleted() // .assertValues("body1", "body2"); InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.atLeastOnce()).getQueueUrl(queueName); inorder.verify(sqs, Mockito.times(2)).receiveMessage(Mockito.<ReceiveMessageRequest>any()); inorder.verify(sqs, Mockito.times(1)).shutdown(); inorder.verifyNoMoreInteractions(); }
@SuppressWarnings("unchecked") @Test public void ensureIfSendToSqsFailsThatS3ObjectIsDeleted() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); Mockito.when(sqs.sendMessage(Mockito.anyString(), Mockito.anyString())).thenThrow(RuntimeException.class); try { Sqs.sendToQueueUsingS3(sqs, "queueUrl", s3, "bucket", new byte[] { 1, 2 }); } catch (RuntimeException e) { assertTrue(e instanceof CompositeException); InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any()); inorder.verify(sqs, Mockito.times(1)).sendMessage(Mockito.anyString(), Mockito.anyString()); inorder.verify(s3, Mockito.times(1)).deleteObject(Mockito.anyString(), Mockito.anyString()); inorder.verifyNoMoreInteractions(); } }
@Test public void deleteMessageFromFactoryWhenS3FactoryExists() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); String s3Id = "123"; SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.of(s3Id), new SqsMessage.Service(Optional.of(() -> s3), () -> sqs, Optional.of(s3), sqs, QUEUE, Optional.of(BUCKET))); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)) .thenReturn(new DeleteMessageResult()); m.deleteMessage(Client.FROM_FACTORY); InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).deleteObject(BUCKET, s3Id); inorder.verify(sqs, Mockito.times(1)).deleteMessage(QUEUE, RECEIPT_HANDLE); inorder.verify(s3, Mockito.times(1)).shutdown(); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs, s3); }
private void initQueue() { this.sqs = new AmazonSQSClient(); // Do we need to use new // ClientConfiguration().withMaxConnections(256) // ? this.sqs.configureRegion(region); try { // Check to see if queue exists GetQueueUrlResult queueUrlResult = this.sqs.getQueueUrl(getSqsQueueName()); this.queueUrl = queueUrlResult.getQueueUrl(); } catch (QueueDoesNotExistException queueDoesNotExist) { // Queue does not exist, need to create one CreateQueueRequest createQueueRequest = new CreateQueueRequest(); createQueueRequest.setQueueName(getSqsQueueName()); createQueueRequest.addAttributesEntry("VisibilityTimeout", "" + getVisibilityTimeout()); CreateQueueResult createQueueResult = this.sqs.createQueue(createQueueRequest); this.queueUrl = createQueueResult.getQueueUrl(); } }
@Override public Properties send(Properties properties, Object message) throws ConnectorException { String access_key_id = properties.getProperty("AccessKeyId"); String secret_access_key = properties.getProperty("SecretAccessKey"); BasicAWSCredentials credentials = new BasicAWSCredentials(access_key_id, secret_access_key); AmazonSQS sqs = new AmazonSQSClient(credentials); //System.out.println(properties.getProperty("region")); // Region selection Region region = Region.getRegion(Regions.fromName(properties.getProperty("region"))); sqs.setRegion(region); GetQueueUrlResult queueUrl = sqs.getQueueUrl(properties.getProperty("Queue")); String messageStr = new String((byte[])message); sqs.sendMessage(new SendMessageRequest(queueUrl.getQueueUrl(), messageStr)); return properties; }
/** * Collect data for SQS. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanSQS(AwsStats stats, AwsAccount account, Regions region) { LOG.debug("Scan for SQS in region " + region.getName() + " in account " + account.getAccountId()); try { AmazonSQSClient sqs = new AmazonSQSClient(account.getCredentials()); sqs.setRegion(Region.getRegion(region)); int totalQueues = 0; for (String queueUrl : sqs.listQueues().getQueueUrls()) { AwsResource res = new AwsResource(queueUrl, account.getAccountId(), AwsResourceType.SQS, region); stats.add(res); totalQueues++; } LOG.info(totalQueues + " SQS queues in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of SQS: " + ase.getMessage()); } catch (Exception ex) { LOG.error("Exception of SQS: " + ex.getMessage()); } }
@Override public void run(final AppConfiguration configuration, final Environment environment) throws Exception { if(configuration!=null && configuration.getAws() !=null && configuration.getAws().getSqs() !=null){ final AmazonSQSClient client = new AmazonSQSClient(); try{ LOGGER.info("Executing configured sqs bundle"); final SqsQueueAdmin admin = new SqsQueueAdmin(client); final SqsConfig sqsConfig = configuration.getAws().getSqs(); admin.initializeQueues(sqsConfig.getQueues(), sqsConfig.isBlockUntilReady()); LOGGER.info("Completed sqs bundle execution"); }catch(Exception ex){ LOGGER.error(ex.getMessage(), ex); }finally{ client.shutdown(); } } }
public TOCQueue(boolean isConsumer, String awsAccessKey, String awsSecretKey, String sqsQueueName, TOCPayloadHandler tocPayloadHandler) throws Exception { super(); mySourceIdentifier = determineHostName() + "-" + UUID.randomUUID().toString().replace("-", "").substring(0,4); this.sqsQueueName = sqsQueueName; this.tocPayloadHandler = tocPayloadHandler; if (!isConsumer) { // then I am the master... canDestroyQueue = true; this.sqsQueueName += "-" + mySourceIdentifier; } sqsClient = new AmazonSQSClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey)); connectToQueue(isConsumer, 1000); if (isConsumer) { this.consumerThread = new Thread(this,"TOCQueue["+myId+"] msg consumer thread"); } logger.info("\n-------------------------------------------\n" + "TOC Queue["+myId+"]: ALL SQS resources hooked up OK: "+this.tocQueueUrl+"\n" + "-------------------------------------------\n"); }
private SQSConnectionFactory(final Builder builder) { this.providerConfiguration = builder.providerConfiguration; this.amazonSQSClientSupplier = new AmazonSQSClientSupplier() { @Override public AmazonSQS get() { AmazonSQSClient amazonSQSClient = new AmazonSQSClient(builder.awsCredentialsProvider, builder.clientConfiguration); if (builder.region != null) { amazonSQSClient.setRegion(builder.region); } if (builder.endpoint != null) { amazonSQSClient.setEndpoint(builder.endpoint); } if (builder.signerRegionOverride != null) { amazonSQSClient.setSignerRegionOverride(builder.signerRegionOverride); } return amazonSQSClient; } }; }
@Test public void testAdapterSend() throws Exception { AmazonSQSClient mockClient = mock(AmazonSQSClient.class); SendMessageResult mockResult = mock(SendMessageResult.class); when(mockClient.sendMessage(any(SendMessageRequest.class))).thenReturn(mockResult); when(mockResult.getMessageId()).thenReturn("TEST"); SQSAdapter sqs = new SQSAdapter(mockClient, "some queue"); assertNotNull("sqs object was null", sqs); final boolean[] wasReceived = {false}; sqs.send("Message", new MessageSentCallback() { @Override public void onSend(String messageId) { wasReceived[0] = true; assertEquals("should be TEST", "TEST", messageId); } }); Thread.sleep(2000); assertEquals("wasReceived was not true", true, wasReceived[0]); }
public static void main(String... args) throws IOException { AWSCredentialsProvider credentials = CredentialsUtils.getAwsCredentials(); AmazonS3 storage = new AmazonS3Client(credentials); storage.setRegion(Region.getRegion(Regions.EU_WEST_1)); AmazonSQS queue = new AmazonSQSClient(credentials); queue.setRegion(Region.getRegion(Regions.EU_WEST_1)); AmazonEC2 machines = new AmazonEC2Client(credentials); machines.setRegion(Region.getRegion(Regions.EU_WEST_1)); AmazonIdentityManagement identityManagement = new AmazonIdentityManagementClient(credentials); identityManagement.setRegion(Region.getRegion(Regions.EU_WEST_1)); if ("launch".equals(args[0])) { setup(storage, queue, machines, identityManagement); manageAnswers(queue); } else if ("run".equals(args[0])) { run(storage, queue); } else { System.out.println("Huh hoh! Don't know what you intended to do..."); } }
@Before public void setup() { injector = Guice.createInjector( new SuroPlugin() { @Override protected void configure() { this.addSinkType("TestSink", TestSinkManager.TestSink.class); this.addNoticeType(NoNotice.TYPE, NoNotice.class); this.addNoticeType(QueueNotice.TYPE, QueueNotice.class); this.addNoticeType(SQSNotice.TYPE, SQSNotice.class); } }, new AbstractModule() { @Override protected void configure() { bind(ObjectMapper.class).to(DefaultObjectMapper.class); bind(AWSCredentialsProvider.class).to(PropertyAWSCredentialsProvider.class); bind(AmazonSQSClient.class).toProvider(AmazonSQSClientProvider.class).asEagerSingleton(); } } ); }
public static String getQueueArnByUrl( final AmazonSQSClient sqs, final String queueUrl ) throws Exception { try { GetQueueAttributesRequest queueAttributesRequest = new GetQueueAttributesRequest( queueUrl ).withAttributeNames( "All" ); GetQueueAttributesResult queueAttributesResult = sqs.getQueueAttributes( queueAttributesRequest ); Map<String, String> sqsAttributeMap = queueAttributesResult.getAttributes(); return sqsAttributeMap.get( "QueueArn" ); } catch ( Exception e ) { logger.error( "Failed to get queue URL from service", e ); throw e; } }
public void initalize(URI uri, Configuration conf) { this.conf = conf; String keyId = conf.get("fs."+uri.getScheme()+".awsAccessKeyId"); String keySecret = conf.get("fs."+uri.getScheme()+".awsSecretAccessKey"); //An override option for accessing across accounts keyId = conf.get("fs."+uri.getScheme()+".override.awsAccessKeyId", keyId); keySecret = conf.get("fs."+uri.getScheme()+".override.awsSecretAccessKey", keySecret); sqs = new AmazonSQSClient(new BasicAWSCredentials(keyId, keySecret)); //SQS Consistency Queue consistencyQueue = conf.get("fs"+uri.getScheme()+".alert.sqs.queue", consistencyQueue); consistencyQueue = sqs.getQueueUrl(new GetQueueUrlRequest(consistencyQueue)).getQueueUrl(); //SQS Timeout Queue timeoutQueue = conf.get("fs"+uri.getScheme()+".timeout.sqs.queue", timeoutQueue); timeoutQueue = sqs.getQueueUrl(new GetQueueUrlRequest(timeoutQueue)).getQueueUrl(); }
private void initSqs(String keyId, String keySecret) { log.debug("Initializing SQS Client"); sqs = new AmazonSQSClient(new BasicAWSCredentials(keyId, keySecret)); //SQS Consistency Queue consistencyQueue = conf.get("s3mper.alert.sqs.queue", consistencyQueue); consistencyQueueUrl = sqs.getQueueUrl(new GetQueueUrlRequest(consistencyQueue)).getQueueUrl(); //SQS Timeout Queue timeoutQueue = conf.get("s3mper.timeout.sqs.queue", timeoutQueue); timeoutQueueUrl = sqs.getQueueUrl(new GetQueueUrlRequest(timeoutQueue)).getQueueUrl(); //SQS Notification Queue notificationQueue = conf.get("s3mper.notification.sqs.queue", notificationQueue); notificationQueueUrl = sqs.getQueueUrl(new GetQueueUrlRequest(notificationQueue)).getQueueUrl(); //Disable reporting (Testing purposes mostly) reportingDisabled = conf.getBoolean("s3mper.reporting.disabled", reportingDisabled); }
public S3SQSClient(Region region, String queueName, String accessKey, String secretKey) { ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setRequestTimeout(5000); clientConfiguration.setRetryPolicy(new RetryPolicy(null, null, 3, true)); if (accessKey.isEmpty() && secretKey.isEmpty()) { this.sqs = new AmazonSQSClient(new InstanceProfileCredentialsProvider(), clientConfiguration); } else { this.sqs = new AmazonSQSClient(new BasicAWSCredentials(accessKey, secretKey), clientConfiguration); } this.sqs.setRegion(region); this.queueName = queueName; }
@SuppressWarnings("unchecked") @Test public void ensureIfSendToSqsFailsThatS3ObjectIsDeleted() { final AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); final AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); Mockito.when(sqs.sendMessage(Mockito.anyString(), Mockito.anyString())).thenThrow(RuntimeException.class); try { Sqs.sendToQueueUsingS3(sqs, "queueUrl", s3, "bucket", new byte[] { 1, 2 }); } catch (final RuntimeException e) { assertTrue(e instanceof CompositeException); final InOrder inorder = Mockito.inOrder(sqs, s3); inorder.verify(s3, Mockito.times(1)).putObject(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any()); inorder.verify(sqs, Mockito.times(1)).sendMessage(Mockito.anyString(), Mockito.anyString()); inorder.verify(s3, Mockito.times(1)).deleteObject(Mockito.anyString(), Mockito.anyString()); inorder.verifyNoMoreInteractions(); } }
@Test public void test() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); SqsMessage m = createMessage(sqs); assertEquals(1000L, m.lastModifiedTime()); assertTrue(m.toString().contains("123")); }
@Test public void testClientFromFactory() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.empty(), new SqsMessage.Service(Optional.empty(), () -> sqs, Optional.empty(), sqs, QUEUE, Optional.empty())); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)) .thenReturn(new DeleteMessageResult()); m.deleteMessage(Client.FROM_FACTORY); InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.times(1)).deleteMessage(QUEUE, RECEIPT_HANDLE); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs); }
@Test public void testClientFromSourceFailsThenFailsOverToFromFactory() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.empty(), new SqsMessage.Service(Optional.empty(), () -> sqs, Optional.empty(), sqs, QUEUE, Optional.empty())); Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE)).thenThrow(new RuntimeException()) .thenReturn(new DeleteMessageResult()); m.deleteMessage(); InOrder inorder = Mockito.inOrder(sqs); inorder.verify(sqs, Mockito.times(2)).deleteMessage(QUEUE, RECEIPT_HANDLE); inorder.verify(sqs, Mockito.times(1)).shutdown(); Mockito.verifyNoMoreInteractions(sqs); }
@Test(expected = IllegalArgumentException.class) public void s3IdAndS3FactoryConflictThrowsException() { AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class); AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class); SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.empty(), new SqsMessage.Service(Optional.empty(), () -> sqs, Optional.empty(), sqs, QUEUE, Optional.empty())); m.deleteMessage(Optional.of(s3), sqs); }
/** * instantiates a AmazonSQSClient * Currently using BasicAWSCredentials to pass on the credentials. * For SQS you need to set your regions endpoint for sqs. */ public AWSSQSUtil(String accessKey, String secreteKey, String name, String endPoint){ try{ this.credentials = new BasicAWSCredentials(accessKey,secreteKey); this.simpleQueue = name; this.sqs = new AmazonSQSClient(this.credentials); this.sqs.setEndpoint(endPoint); }catch(Exception e){ System.out.println("exception while creating connection wit AWS : " + e); } }
@Provides public Map<Status, ObservableQueue> getQueues(Configuration config, AWSCredentialsProvider acp) { String stack = ""; if(config.getStack() != null && config.getStack().length() > 0) { stack = config.getStack() + "_"; } Status[] statuses = new Status[]{Status.COMPLETED, Status.FAILED}; Map<Status, ObservableQueue> queues = new HashMap<>(); for(Status status : statuses) { String queueName = config.getProperty("workflow.listener.queue.prefix", config.getAppId() + "_sqs_notify_" + stack + status.name()); AmazonSQSClient client = new AmazonSQSClient(acp); Builder builder = new SQSObservableQueue.Builder().withClient(client).withQueueName(queueName); String auth = config.getProperty("workflow.listener.queue.authorizedAccounts", ""); String[] accounts = auth.split(","); for(String accountToAuthorize : accounts) { accountToAuthorize = accountToAuthorize.trim(); if(accountToAuthorize.length() > 0) { builder.addAccountToAuthorize(accountToAuthorize.trim()); } } ObservableQueue queue = builder.build(); queues.put(status, queue); } return queues; }
private SQSObservableQueue(String queueName, AmazonSQSClient client, int visibilityTimeout, int batchSize, int pollTimeInMS, List<String> accountsToAuthorize) { this.queueName = queueName; this.client = client; this.visibilityTimeout = visibilityTimeout; this.batchSize = batchSize; this.pollTimeInMS = pollTimeInMS; this.queueURL = getOrCreateQueue(); addPolicy(accountsToAuthorize); }
public Scanner(ScanConfig config) { opt = config; ProfileCredentialsProvider creds = new ProfileCredentialsProvider(opt.profile()); creds.getCredentials(); // credible credential criteria s3 = new AmazonS3Client(creds); sqs = new AmazonSQSClient(creds); CreateQueueResult queueResult = sqs.createQueue(opt.queue()); queueUrl = queueResult.getQueueUrl(); filter = Pattern.compile(opt.filter(), Pattern.CASE_INSENSITIVE); max = Long.parseLong(opt.max()); }
public static void sendMessageRange(String url, int from, AmazonSQSClient client) { ObjectMapper jsonMapper = new ObjectMapper(); IntStream.range(from, from + 100) .mapToObj(i -> new Message(i,"from: hogehoge", "to: piyopiyo", "body: fugafugafuga")) .map(message -> { try { return jsonMapper.writeValueAsString(message); } catch (JsonProcessingException e) { return ""; } }) .map(json -> new SendMessageRequest(url, json)) .forEach(client::sendMessage); }
@Provides @Singleton protected AmazonSQS provideAmazonSQS(Region region, AWSCredentialsProvider credentialsProvider) { AmazonSQS amazonSQS = new AmazonSQSClient(credentialsProvider); amazonSQS.setRegion(region); return amazonSQS; }
private void deleteMessageUsingFactory(Optional<Func0<AmazonS3Client>> s3Factory, Func0<AmazonSQSClient> sqsFactory) { Optional<AmazonS3Client> s3 = s3Factory.map(Func0::call); AmazonSQSClient sqs = sqsFactory.call(); try { deleteMessage(s3, sqs); } finally { s3.ifPresent(SqsMessage::shutdown); shutdown(sqs); } }
public void deleteMessage(Optional<AmazonS3Client> s3, AmazonSQSClient sqs) { Preconditions.checkArgument(!s3.isPresent() || s3Id.isPresent(), "s3Id must be present"); if (s3Id.isPresent()) { s3.get().deleteObject(service.bucketName.get(), s3Id.get()); } sqs.deleteMessage(service.queueName, messageReceiptHandle); }