.handle((tags, e) -> { if (e != null) { service.getHost().log(Level.WARNING, "Error populating tags in "
/** * Perform the actual retrieval and returns to the client DeferredResult with actual QueryResult. */ public DeferredResult<QueryResult> query() { this.result = new QueryResult(); // start by retrieving the requested resource pools return retrieveResourcePools() .thenCompose(ignore -> executeRpQueries()) .thenCompose(ignore -> findComputesWithoutPool()) .thenCompose(this::handleMissingComputes) .handle((ignore, exc) -> exc != null ? QueryResult.forError(exc) : this.result); }
/** * Perform the actual retrieval and returns to the client DeferredResult with actual QueryResult. */ public DeferredResult<QueryResult> query() { this.result = new QueryResult(); // start by retrieving the requested resource pools return retrieveResourcePools() .thenCompose(ignore -> executeRpQueries()) .thenCompose(ignore -> findComputesWithoutPool()) .thenCompose(this::handleMissingComputes) .handle((ignore, exc) -> exc != null ? QueryResult.forError(exc) : this.result); }
@Test public void testHandleRethrow() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); AtomicInteger postExceptionInvocations = new AtomicInteger(); DeferredResult<Integer> original = new DeferredResult<>(); original .handle((x, ex) -> { Assert.assertNotNull(ex); Assert.assertEquals(RuntimeException.class, ex.getClass()); invocations.incrementAndGet(); throw new TestException(); }) .thenRun(() -> { postExceptionInvocations.incrementAndGet(); }) .whenComplete(getExpectedExceptionCompletion(ctx)); runAfter(10, () -> original.fail(new RuntimeException())); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(0, postExceptionInvocations.get()); }
@Test public void testHandleRethrow() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); AtomicInteger postExceptionInvocations = new AtomicInteger(); DeferredResult<Integer> original = new DeferredResult<>(); original .handle((x, ex) -> { Assert.assertNotNull(ex); Assert.assertEquals(RuntimeException.class, ex.getClass()); invocations.incrementAndGet(); throw new TestException(); }) .thenRun(() -> { postExceptionInvocations.incrementAndGet(); }) .whenComplete(getExpectedExceptionCompletion(ctx)); runAfter(10, () -> original.fail(new RuntimeException())); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(0, postExceptionInvocations.get()); }
@Test public void testHandle() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); DeferredResult<Integer> original = new DeferredResult<>(); DeferredResult<Integer> result = original .handle((x, ex) -> { Assert.assertNull(ex); invocations.incrementAndGet(); return x + 1; }); result.whenComplete(ctx.getCompletionDeferred()); runAfter(10, () -> original.complete(1)); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(2, result.getNow(exceptionSupplier()).intValue()); }
@Test public void testHandle() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); DeferredResult<Integer> original = new DeferredResult<>(); DeferredResult<Integer> result = original .handle((x, ex) -> { Assert.assertNull(ex); invocations.incrementAndGet(); return x + 1; }); result.whenComplete(ctx.getCompletionDeferred()); runAfter(10, () -> original.complete(1)); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(2, result.getNow(exceptionSupplier()).intValue()); }
private DeferredResult<Void> handleCloudAdminGroupUnassignment() { String roleLink = UriUtils.buildUriPath(RoleService.FACTORY_LINK, AuthRole.CLOUD_ADMIN .buildRoleWithSuffix(encode(principalId))); Operation getRole = Operation.createGet(service, roleLink); DeferredResult<Void> result = new DeferredResult<>(); service.sendWithDeferredResult(getRole, RoleState.class) .handle((ug, ex) -> { if (ex != null) { if (ex.getCause() instanceof ServiceNotFoundException) { // User group was never assigned, so we don't have to delete any role. DeferredResult.completed(ug); } return DeferredResult.failed(ex); } Operation deleteRoleOp = Operation.createDelete(service, roleLink); addReplicationFactor(deleteRoleOp); return service.sendWithDeferredResult(deleteRoleOp, RoleState.class); }) .thenAccept(ignore -> result.complete(null)) .exceptionally(ex -> { result.fail(ex); return null; }); return result; }
@Test public void testHandleException() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); int recoveredValue = 5; DeferredResult<Integer> original = new DeferredResult<>(); DeferredResult<Integer> result = original .handle((x, ex) -> { Assert.assertNotNull(ex); Assert.assertEquals(TestException.class, ex.getClass()); invocations.incrementAndGet(); return recoveredValue; }) .thenApply(x -> x + 1); result.whenComplete(ctx.getCompletionDeferred()); runAfter(10, () -> original.fail(new TestException())); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(recoveredValue + 1, result.getNow(exceptionSupplier()).intValue()); }
@Test public void testHandleException() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); AtomicInteger invocations = new AtomicInteger(); int recoveredValue = 5; DeferredResult<Integer> original = new DeferredResult<>(); DeferredResult<Integer> result = original .handle((x, ex) -> { Assert.assertNotNull(ex); Assert.assertEquals(TestException.class, ex.getClass()); invocations.incrementAndGet(); return recoveredValue; }) .thenApply(x -> x + 1); result.whenComplete(ctx.getCompletionDeferred()); runAfter(10, () -> original.fail(new TestException())); ctx.await(); Assert.assertEquals(1, invocations.get()); Assert.assertEquals(recoveredValue + 1, result.getNow(exceptionSupplier()).intValue()); }