@Override public F.Promise<Void> store(Path path, String key) { Promise<Void> promise = Futures.promise(); TransferManager transferManager = new TransferManager(credentials); Upload upload = transferManager.upload(bucketName, key, path.toFile()); upload.addProgressListener( (ProgressListener) progressEvent -> { if (progressEvent.getEventType().isTransferEvent()) { if (progressEvent .getEventType() .equals(ProgressEventType.TRANSFER_COMPLETED_EVENT)) { transferManager.shutdownNow(); promise.success(null); } else if (progressEvent .getEventType() .equals(ProgressEventType.TRANSFER_FAILED_EVENT)) { transferManager.shutdownNow(); promise.failure(new Exception("Upload failed")); } } }); return F.Promise.wrap(promise.future()); }
private F.Promise<WSResponse> execute(Request request) { final scala.concurrent.Promise<WSResponse> scalaPromise = scala.concurrent.Promise$.MODULE$.<WSResponse>apply(); try { AsyncHttpClient asyncHttpClient = (AsyncHttpClient) client.getUnderlying(); asyncHttpClient.executeRequest( request, new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(Response response) { final Response ahcResponse = response; scalaPromise.success(new AhcWSResponse(ahcResponse)); return response; } @Override public void onThrowable(Throwable t) { scalaPromise.failure(t); } }); } catch (RuntimeException exception) { scalaPromise.failure(exception); } return F.Promise.wrap(scalaPromise.future()); }
@Test public void mustBeAbleToExecuteAnOnExceptionCallback() throws Throwable { final CountDownLatch latch = new CountDownLatch(1); Promise<String> cf = Futures.promise(); Future<String> f = cf.future(); f.onFailure( new OnFailure() { public void onFailure(Throwable t) { if (t instanceof NullPointerException) latch.countDown(); } }, system.dispatcher()); Throwable exception = new NullPointerException(); cf.failure(exception); assertTrue(latch.await(5000, TimeUnit.MILLISECONDS)); assertEquals(f.value().get().failed().get(), exception); }
@Test public void recoverWithToMustBeCallable() throws Exception { final IllegalStateException fail = new IllegalStateException("OHNOES"); Promise<Object> p = Futures.promise(); Future<Object> f = p.future() .recoverWith( new Recover<Future<Object>>() { public Future<Object> recover(Throwable t) throws Throwable { if (t == fail) return Futures.<Object>successful("foo"); else throw t; } }, system.dispatcher()); Duration d = Duration.create(1, TimeUnit.SECONDS); p.failure(fail); assertEquals(Await.result(f, d), "foo"); }
@Override public F.Promise<Void> delete(String key) { Promise<Void> promise = Futures.promise(); AmazonS3 amazonS3 = new AmazonS3Client(credentials); DeleteObjectRequest request = new DeleteObjectRequest(bucketName, key); request.withGeneralProgressListener( progressEvent -> { if (progressEvent.getEventType().isTransferEvent()) { if (progressEvent.getEventType().equals(ProgressEventType.TRANSFER_COMPLETED_EVENT)) { promise.success(null); } else if (progressEvent .getEventType() .equals(ProgressEventType.TRANSFER_FAILED_EVENT)) { promise.failure(new Exception("Delete failed")); } } }); amazonS3.deleteObject(request); return F.Promise.wrap(promise.future()); }