public CircuitBreaker getCircuitBreaker( String actorName, ExecutionContext executionContext, Scheduler scheduler, int maxFailures, FiniteDuration callTimeout, FiniteDuration resetTimeout) { CircuitBreaker circuitBreaker = new CircuitBreaker(executionContext, scheduler, maxFailures, callTimeout, resetTimeout); circuitBreaker.onOpen( new Runnable() { public void run() { onOpen(actorName); } }); circuitBreaker.onClose( new Runnable() { @Override public void run() { onClose(actorName); } }); circuitBreaker.onHalfOpen( new Runnable() { @Override public void run() { onHalfOpen(actorName); } }); return circuitBreaker; }
public Future<Instance> startInstanceAsync(AWSCredentials credentials) { Future<Instance> f = circuitBreaker.callWithCircuitBreaker( () -> Futures.future(() -> startInstance(credentials), executionContext)); PartialFunction<Throwable, Future<Instance>> recovery = new PFBuilder<Throwable, Future<Instance>>() .match( AmazonClientException.class, ex -> ex.isRetryable(), ex -> startInstanceAsync(credentials)) .build(); return f.recoverWith(recovery, executionContext); }
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); }