Fun with Scala, JEE 6, JBossAS 7 and Async EJB
In this post i will show a simple Asynchronous service using enterprise Java Beans. Instead of using Java i will use Scala (to increase the fun :-) ). We gonna deploy the code in JBoss Aplication Server 7 (BTW this new version is ultra fast compared to JBOSSAS 6 and previous versions) this version is compliant with JEE6 and we will use one of the new features called Async support for EJB.
I will use maven 3 as a build and dependency management system. This code also works on JBossAS 8 akka Wildfly. If you are impatient you can get the full code on my github. JEE6 don`t demand you to create an web.xml so we gonna create an servlet and an ejb using annotations only(that`s pretty cool).
WheaterServlet
Note that i used the Annotations @WebServlet and @Inject. The first annotation is used in order to define a servlet in JEE6. You need set asyncsupported to true in order to have async support for EJB as well. Actually the async started at the servlet level and them an AsyncContext is propagated to the EJB context.
The second annotation is an CDI annotation, i used it because i want to inject an EJB instance on the servlet. As you can see i just delegate to work to an ejb on the doGet method of this servlet. Now lets take a look on the EJB code.
WeatherService
This is a Stateless EJB, note i'm using the @Asynchronous annotation in order to get the Async support for EJB, you also need receive the Servlet AsyncContext object by parameter. By deisgn i'm making this EJB wait 5 seconds before return the result this is just to simulate somethign that takes some time and don`t give an immediate response. Once i finished i need call the method complete on the AsyncContext.
Okay, almost there, now we just need create a simple HTML file with JavaScript in order to call our async servlet/ejb, so lets see.
Now i just need build the solution, you can do that doing: $ mvn scala:compile -DdisplayCmd=true install
Deploy your WAR file at JBossAS 7 or 8 and them just hit: http://localhost:8080/jbossas7-jee6-playground-1.0-SNAPSHOT/ and have fun :-)
I will use maven 3 as a build and dependency management system. This code also works on JBossAS 8 akka Wildfly. If you are impatient you can get the full code on my github. JEE6 don`t demand you to create an web.xml so we gonna create an servlet and an ejb using annotations only(that`s pretty cool).
WheaterServlet
Note that i used the Annotations @WebServlet and @Inject. The first annotation is used in order to define a servlet in JEE6. You need set asyncsupported to true in order to have async support for EJB as well. Actually the async started at the servlet level and them an AsyncContext is propagated to the EJB context.
The second annotation is an CDI annotation, i used it because i want to inject an EJB instance on the servlet. As you can see i just delegate to work to an ejb on the doGet method of this servlet. Now lets take a look on the EJB code.
WeatherService
This is a Stateless EJB, note i'm using the @Asynchronous annotation in order to get the Async support for EJB, you also need receive the Servlet AsyncContext object by parameter. By deisgn i'm making this EJB wait 5 seconds before return the result this is just to simulate somethign that takes some time and don`t give an immediate response. Once i finished i need call the method complete on the AsyncContext.
Okay, almost there, now we just need create a simple HTML file with JavaScript in order to call our async servlet/ejb, so lets see.
Now i just need build the solution, you can do that doing: $ mvn scala:compile -DdisplayCmd=true install
Deploy your WAR file at JBossAS 7 or 8 and them just hit: http://localhost:8080/jbossas7-jee6-playground-1.0-SNAPSHOT/ and have fun :-)