use of com.amazonaws.handlers.AsyncHandler in project photon-model by vmware.
the class AWSTaskStatusChecker method runSearch.
private void runSearch(T type) {
AmazonWebServiceRequest descRequest = buildRequest(type);
AsyncHandler describeHandler = buildHandler(type);
if (type instanceof Instance) {
this.amazonEC2Client.describeInstancesAsync((DescribeInstancesRequest) descRequest, describeHandler);
} else if (type instanceof NatGateway) {
this.amazonEC2Client.describeNatGatewaysAsync((DescribeNatGatewaysRequest) descRequest, describeHandler);
} else if (type instanceof Volume) {
this.amazonEC2Client.describeVolumesAsync((DescribeVolumesRequest) descRequest, describeHandler);
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
}
}
use of com.amazonaws.handlers.AsyncHandler in project photon-model by vmware.
the class AWSClientManager method getOrCreateCloudWatchClient.
/**
* Get or create a CloudWatch Client instance that will be used to get stats from AWS.
*
* Note: ARN-based credentials will not be accepted unless they have already been exchanged to
* AWS for session credentials. If unset, this method will throw a
* {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
* credentials, migrate to {@link #getOrCreateCloudWatchClientAsync(AuthCredentialsServiceState,
* String, StatelessService, boolean)}.
*
* @param credentials The auth credentials to be used for the client creation
* @param regionId The region of the AWS client
* @param service The stateless service for which the operation is being performed.
* @param isMock Indicates if this a mock request
* @return
*/
public AmazonCloudWatchAsyncClient getOrCreateCloudWatchClient(AuthCredentialsServiceState credentials, String regionId, StatelessService service, boolean isMock, Consumer<Throwable> failConsumer) {
if (this.awsClientType != AwsClientType.CLOUD_WATCH) {
throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
}
if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
}
String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
if (isCloudWatchClientInvalid(cacheKey)) {
failConsumer.accept(new IllegalStateException("Invalid cloud watch client for key: " + cacheKey));
return null;
}
AmazonCloudWatchAsyncClient amazonCloudWatchClient = null;
try {
amazonCloudWatchClient = this.cloudWatchClientCache.computeIfAbsent(cacheKey, key -> {
AmazonCloudWatchAsyncClient client = AWSUtils.getStatsAsyncClient(credentials, regionId, getExecutor(), isMock);
client.describeAlarmsAsync(new AsyncHandler<DescribeAlarmsRequest, DescribeAlarmsResult>() {
@Override
public void onError(Exception exception) {
markCloudWatchClientInvalid(service, cacheKey);
}
@Override
public void onSuccess(DescribeAlarmsRequest request, DescribeAlarmsResult result) {
// noop
}
});
return client;
});
} catch (Throwable e) {
service.logSevere(e);
failConsumer.accept(e);
}
return amazonCloudWatchClient;
}
use of com.amazonaws.handlers.AsyncHandler in project spring-integration-aws by spring-projects.
the class KinesisMessageHandlerTests method testKinesisMessageHandler.
@Test
@SuppressWarnings("unchecked")
public void testKinesisMessageHandler() throws Exception {
Message<?> message = MessageBuilder.withPayload("message").build();
try {
this.kinesisSendChannel.send(message);
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).contains("'stream' must not be null for sending a Kinesis record");
}
this.kinesisMessageHandler.setStream("foo");
try {
this.kinesisSendChannel.send(message);
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).contains("'partitionKey' must not be null for sending a Kinesis record");
}
message = MessageBuilder.fromMessage(message).setHeader(AwsHeaders.PARTITION_KEY, "fooKey").setHeader(AwsHeaders.SEQUENCE_NUMBER, "10").setHeader("foo", "bar").build();
this.kinesisSendChannel.send(message);
ArgumentCaptor<PutRecordRequest> putRecordRequestArgumentCaptor = ArgumentCaptor.forClass(PutRecordRequest.class);
ArgumentCaptor<AsyncHandler<PutRecordRequest, PutRecordResult>> asyncHandlerArgumentCaptor = ArgumentCaptor.forClass((Class<AsyncHandler<PutRecordRequest, PutRecordResult>>) (Class<?>) AsyncHandler.class);
verify(this.amazonKinesis).putRecordAsync(putRecordRequestArgumentCaptor.capture(), asyncHandlerArgumentCaptor.capture());
PutRecordRequest putRecordRequest = putRecordRequestArgumentCaptor.getValue();
assertThat(putRecordRequest.getStreamName()).isEqualTo("foo");
assertThat(putRecordRequest.getPartitionKey()).isEqualTo("fooKey");
assertThat(putRecordRequest.getSequenceNumberForOrdering()).isEqualTo("10");
assertThat(putRecordRequest.getExplicitHashKey()).isNull();
Message<?> messageToCheck = new EmbeddedJsonHeadersMessageMapper().toMessage(putRecordRequest.getData().array());
assertThat(messageToCheck.getHeaders()).contains(entry("foo", "bar"));
assertThat(messageToCheck.getPayload()).isEqualTo("message".getBytes());
AsyncHandler<?, ?> asyncHandler = asyncHandlerArgumentCaptor.getValue();
RuntimeException testingException = new RuntimeException("testingException");
asyncHandler.onError(testingException);
verify(this.asyncHandler).onError(eq(testingException));
message = new GenericMessage<>(new PutRecordsRequest().withStreamName("myStream").withRecords(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey")));
this.kinesisSendChannel.send(message);
ArgumentCaptor<PutRecordsRequest> putRecordsRequestArgumentCaptor = ArgumentCaptor.forClass(PutRecordsRequest.class);
verify(this.amazonKinesis).putRecordsAsync(putRecordsRequestArgumentCaptor.capture(), any(AsyncHandler.class));
PutRecordsRequest putRecordsRequest = putRecordsRequestArgumentCaptor.getValue();
assertThat(putRecordsRequest.getStreamName()).isEqualTo("myStream");
assertThat(putRecordsRequest.getRecords()).containsExactlyInAnyOrder(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey"));
}
Aggregations