Repository: deltaspike Updated Branches: refs/heads/master 40d24ab50 -> 17b3513f6
DELTASPIKE-1176 Support void methods in Futureable Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/17b3513f Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/17b3513f Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/17b3513f Branch: refs/heads/master Commit: 17b3513f69f3546f4b60f8a5ca744644983944a9 Parents: 40d24ab Author: Xavier Dury <[email protected]> Authored: Fri Jul 22 12:56:59 2016 +0200 Committer: John D. Ament <[email protected]> Committed: Sat Jul 30 07:53:25 2016 -0400 ---------------------------------------------------------------------- .../impl/future/DefaultFutureableStrategy.java | 10 +++++++++- .../test/core/impl/future/FutureableTest.java | 20 ++++++++++++++++++++ .../test/core/impl/future/Service.java | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/17b3513f/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/future/DefaultFutureableStrategy.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/future/DefaultFutureableStrategy.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/future/DefaultFutureableStrategy.java index 70ee7b7..facecdc 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/future/DefaultFutureableStrategy.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/future/DefaultFutureableStrategy.java @@ -138,9 +138,10 @@ public class DefaultFutureableStrategy implements FutureableStrategy // validate usage final Class<?> returnType = ic.getMethod().getReturnType(); if (!Future.class.isAssignableFrom(returnType) && + !void.class.isAssignableFrom(returnType) && (COMPLETION_STAGE == null || !COMPLETION_STAGE.isAssignableFrom(returnType))) { - throw new IllegalArgumentException("Return type should be a CompletableStage or Future"); + throw new IllegalArgumentException("Return type should be a CompletableStage, Future or Void"); } if (configByMethod == null) @@ -202,6 +203,13 @@ public class DefaultFutureableStrategy implements FutureableStrategy }; final ExecutorService pool = getOrCreatePool(ic); + + if (void.class.isAssignableFrom(returnType)) + { + pool.submit(invocation); + return null; + } + if (COMPLETABLE_FUTURE == null) // not on java 8 can only be a future { return pool.submit(invocation); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/17b3513f/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/FutureableTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/FutureableTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/FutureableTest.java index 5dec0a7..b3a006a 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/FutureableTest.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/FutureableTest.java @@ -30,8 +30,10 @@ import org.junit.runner.RunWith; import javax.inject.Inject; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -61,6 +63,24 @@ public class FutureableTest { @Inject private Service service; + + @Test + public void voidTest() + { + CountDownLatch latch = new CountDownLatch(1); + service.thatSLong(1000, latch); + try + { + if (!latch.await(2000, TimeUnit.MILLISECONDS)) { + fail("Asynchronous call should have terminated"); + } + } + catch (final InterruptedException e) + { + Thread.interrupted(); + fail(); + } + } @Test public void future() http://git-wip-us.apache.org/repos/asf/deltaspike/blob/17b3513f/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/Service.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/Service.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/Service.java index beede68..4b729c0 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/Service.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/future/Service.java @@ -21,6 +21,8 @@ package org.apache.deltaspike.test.core.impl.future; import org.apache.deltaspike.core.api.future.Futureable; import javax.enterprise.context.ApplicationScoped; + +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -29,6 +31,21 @@ import java.util.concurrent.TimeoutException; @ApplicationScoped public class Service { + + @Futureable + public void thatSLong(final long sleep, CountDownLatch latch) { + try + { + Thread.sleep(sleep); + latch.countDown(); + } + catch (final InterruptedException e) + { + Thread.interrupted(); + throw new IllegalStateException(e); + } + } + @Futureable // or CompletableFuture<String> public Future<String> thatSLong(final long sleep) {
