public Future<TerminateInstancesResult> terminateInstancesAsync( AmazonEC2Client client, Instance... instances) { List<String> ids = Arrays.stream(instances).map(i -> i.getInstanceId()).collect(Collectors.toList()); TerminateInstancesRequest request = new TerminateInstancesRequest(ids); Future<TerminateInstancesResult> f = circuitBreaker.callWithCircuitBreaker( () -> Futures.future(() -> client.terminateInstances(request), executionContext)); PartialFunction<Throwable, Future<TerminateInstancesResult>> recovery = new PFBuilder<Throwable, Future<TerminateInstancesResult>>() .match( AmazonClientException.class, ex -> ex.isRetryable(), ex -> terminateInstancesAsync(client, instances)) .build(); return f.recoverWith(recovery, executionContext); }
public void terminateEC2(String... ec2id) { TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); amazonClient.terminateInstances(terminateInstancesRequest.withInstanceIds(ec2id)); }