@Async public Future<String> returnSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); if (i == 0) { throw new IllegalArgumentException(); } else if (i < 0) { return AsyncResult.forExecutionException(new IOException()); } return AsyncResult.forValue(Integer.toString(i)); }
@Test public void asyncResultWithSeparateCallbacksAndValue() throws Exception { String value = "val"; final Set<String> values = new HashSet<>(1); ListenableFuture<String> future = AsyncResult.forValue(value); future.addCallback(values::add, (ex) -> fail("Failure callback not expected: " + ex)); assertSame(value, values.iterator().next()); assertSame(value, future.get()); assertSame(value, future.completable().get()); future.completable().thenAccept(v -> assertSame(value, v)); }
@Test public void asyncResultWithCallbackAndValue() throws Exception { String value = "val"; final Set<String> values = new HashSet<>(1); ListenableFuture<String> future = AsyncResult.forValue(value); future.addCallback(new ListenableFutureCallback<String>() { @Override public void onSuccess(String result) { values.add(result); } @Override public void onFailure(Throwable ex) { fail("Failure callback not expected: " + ex); } }); assertSame(value, values.iterator().next()); assertSame(value, future.get()); assertSame(value, future.completable().get()); future.completable().thenAccept(v -> assertSame(value, v)); }
@Async public Future<String> testFuture(int index) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return AsyncResult.forValue("FutureTask:" + index); }
@Async public ListenableFuture<String> testLisenableAsync(int index) { if (index == 10) throw new RuntimeException("testLisenableAsync:" + index); return AsyncResult.forValue("testValue:" + index + ",threadId:" + Thread.currentThread().getId()); }
@SuppressWarnings("ConstantConditions") private int getEffectiveFetchSize(Statement statement) { if (statement.getFetchSize() > 0) { return statement.getFetchSize(); } if (getAsyncCqlOperations() instanceof CassandraAccessor) { CassandraAccessor accessor = (CassandraAccessor) getAsyncCqlOperations(); if (accessor.getFetchSize() != -1) { return accessor.getFetchSize(); } } return getAsyncCqlOperations() .execute((AsyncSessionCallback<Integer>) session -> AsyncResult.forValue(getConfiguredFetchSize(session))) .completable().join(); }