DELTASPIKE-1176 Support void methods in Futureable
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 @@
// 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 @@
};
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);
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 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 @@
@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()
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 @@
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 @@
@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)
{