Created
March 8, 2016 01:54
-
-
Save johnament/74913f04e97065971c80 to your computer and use it in GitHub Desktop.
A really simple Async CDI extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.interceptor.InterceptorBinding; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Inherited; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Inherited | |
@InterceptorBinding | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
public @interface Async { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApplicationScoped | |
@Async | |
public class AsyncBean { | |
public Future<Object> doLongCall() { | |
try { | |
Thread.sleep(1000); | |
System.out.println("Current thread "+Thread.currentThread().getName()); | |
Thread.sleep(1000); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return CompletableFuture.completedFuture("I am the returned value"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.annotation.Priority; | |
import javax.enterprise.context.Dependent; | |
import javax.interceptor.AroundInvoke; | |
import javax.interceptor.Interceptor; | |
import javax.interceptor.InvocationContext; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
@Async | |
@Interceptor | |
@Priority(1001) | |
@Dependent | |
public class AsyncInterceptor { | |
@AroundInvoke | |
public Object runInThread(InvocationContext ctx) throws Exception { | |
ExecutorService executorService = Executors.newFixedThreadPool(10); | |
return executorService.submit(ctx::proceed); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.junit.Arquillian; | |
import org.jboss.shrinkwrap.api.ShrinkWrap; | |
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | |
import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import javax.inject.Inject; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.TimeUnit; | |
@RunWith(Arquillian.class) | |
public class AsyncTest { | |
@Deployment | |
public static JavaArchive create() { | |
return ShrinkWrap.create(JavaArchive.class).addClasses(Async.class, AsyncBean.class, AsyncInterceptor.class).addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | |
} | |
@Inject | |
private AsyncBean asyncBean; | |
@Test | |
public void shouldRunAsync() throws Exception { | |
Future<Object> objectFuture = asyncBean.doLongCall(); | |
System.out.println("Object future called"); | |
Object o = objectFuture.get(3, TimeUnit.SECONDS); | |
CompletableFuture<Object> cf = (CompletableFuture<Object>)o; | |
System.out.println("The output: "+ cf.get()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment