Monday, April 15, 2013

JavaEE with Scala and TomEE

We know how to build applications with Java and Groovy (see previous posts). Now it is time to give some love to a language that has recently caught my attention. Scala runs on the jvm, so there is no trick to make it work with TomEE. Due to its seamless integration with java, one can easily use the well-known java annotations in order to build a standard JavaEE application.

See an example of stateless bean definition:

 package boxdata.ejb  

 import boxdata.data.dto.DeviceUsageDto  
 import scala.collection.JavaConverters._  
 import javax.ejb.EJB  
 import javax.ejb.Stateless  
 
 @Stateless  
 class DeviceUsageEjb {
  
   @EJB  
   var disk: DiskUsageEjb = _
  
   @EJB  
   var file: FileUsageEjb = _  

   def getUsage: DeviceUsageDto = {  
     val diskUsage = disk.getUsage  
     val fileUsage = file.getUsage  
     val dto = new DeviceUsageDto  
     dto.diskUsageList = diskUsage.asJava  
     dto.fileUsageList = fileUsage.asJava  
     dto  
   }  
 }  

Notice the usage of the JavaEE annotations ("@Stateless" and "@EJB").

How would we define a rest application?

 package boxdata.rest  

 import javax.ws.rs.ApplicationPath  
 import javax.ws.rs.core.Application  
 import java.util  
 import scala.collection.JavaConverters._  

 @ApplicationPath("/rest")  
 class ApplicationConfig extends Application {  

   override def getClasses: util.Set[Class[_]] = {  
     Set[Class[_]](  
       classOf[DeviceUsage],  
       classOf[SystemLoad],  
       classOf[SystemThreads]  
     ).asJava  
   }  
 }  

You don't need to download and install Scala before using it with TomEE. Maven takes care of everything. With Maven, the war generation is as simple as "mvn clean install". The resulting war will have only two jars: scala-library-2.9.2.jar and boxdata-web-1.0-SNAPSHOT.jar.

See what "boxdata-web-1.0-SNAPSHOT.jar" contains:
   128 Tue Apr 16 14:34:24 EDT 2013 META-INF/MANIFEST.MF  
   663 Tue Apr 16 14:34:12 EDT 2013 META-INF/ejb-jar.xml  
   827 Tue Apr 16 14:34:12 EDT 2013 META-INF/beans.xml  
  2727 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/DiskUsageDto.class  
  4145 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/ThreadDto.class  
  6451 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/SystemLoadDto.class  
  2518 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/DeviceUsageDto.class  
  1915 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/FileUsageDto.class  
  1977 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/SystemLoad.class  
  1464 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/DeviceUsage.class  
  2006 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/SystemThreads.class  
  1915 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/ApplicationConfig.class  
  3298 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemThreadsEjb.class  
  12153 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemThreadsEjb$$anonfun$getThreadsInfo$1.class  
  2308 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DeviceUsageEjb.class  
  1877 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/FileUsageEjb$$anonfun$buildUsage$1.class  
  5115 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemLoadEjb.class  
  2736 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SchedulerEjb.class  
  12043 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DiskUsageEjb$$anonfun$getUsage$1.class  
  4070 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/FileUsageEjb.class  
  2395 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DiskUsageEjb.class  
  3823 Tue Apr 16 14:34:24 EDT 2013 boxdata/cdi/util/DtoBuilder.class  
  2346 Sat Apr 13 09:17:58 EDT 2013 META-INF/maven/boxdata/boxdata-web/pom.xml  
   110 Tue Apr 16 14:34:26 EDT 2013 META-INF/maven/boxdata/boxdata-web/pom.properties  

For more details about how to build a JavaEE application with Scala, nothing better than checking a real code. Go ahead and clone the https://github.com/tveronezi/boxdata. This project is a starting point for your own applications with TomEE and Scala. For those who love Javascript, the example also shows how to integrate ExtJS and Highcharts.

Application output
Intellij with Scala code (plugin website)

If you are a windows user, you will need to download the bleeding-edge version of TomEE in order to run this application (See how to build and run it with Intellij in this blog post). If you are a Unix-like OS user, you can execute the "make start-tomee" makefile target (check the README file).

Have fun with JavaEE and TomEE!

Ps.: Check out the other JavaEE Scala application -> http://buildnplay.blogspot.ca/2013/06/ejb-mdb-rest-jpa-cdi-all-javaee-fun.html

[]s

No comments:

Post a Comment