Tomcat embed with anotated Servlet
Recently I have playing in with the Tomcat embed version. Currently Spring Boot gives a fancy, easy and rapid creation of the Main class invoking a instanting a Tomcat server. However there is a lack of official information that it makes me doubt about of readiness for production. I belive that the classic war and deployment it is going to be safer in the future and it of course much more portable . But an embeded version can have a futture for testin. I will chose war solution for cloud PaaS envirotments or container solutions such us Cloud Foundry unless Apache will decide to publich formal and clear documentation. Spring give us a OSGI version which wrap tomcat, this seeems be valid for production but Spring is Spring and I personally hate it because they reinvent the heel constantly, I lke JEE a littel bit more but my feeling is that you will never be safe either Spring nor JEE.
It is my intention to do the same test but in a JEE cointainer like Payara or Payara-micro which give us much better documentation.
Having said that. Here is the issues or poblems that I found. My goal was instanciate a Tomcat with a anotated Servlet with no web.xml and no adding explicti Servlet at all. Remember Servlet 3.0 support is a must and tomcat +7 is necessary.
I built the solution using gradle but only with java plugin, the first issue was find out the exacly dependences.
The dependeces list are in the build.gradle but is intersted notice that not all the tomcat versions worked. Also be carefull in that you maven or gradle include the anotations solution for Tomcat otherwise you can face issues.
compile "org.apache.tomcat:tomcat-catalina:$tomcatVersion"
compile "org.apache.tomcat:tomcat-util:$tomcatVersion"
compile "org.apache.tomcat:tomcat-jasper:$tomcatVersion"
compile "org.apache.tomcat:tomcat-jasper-el:$tomcatVersion"
compile "org.apache.tomcat:tomcat-jsp-api:$tomcatVersion"
compile "org.apache.tomcat.embed:tomcat-embed-core:$tomcatVersion"
compile "org.apache.tomcat.embed:tomcat-embed-logging-juli:$tomcatVersion"
compile "org.apache.tomcat.embed:tomcat-embed-jasper:$tomcatVersion"
To be the Servlet reconzied your tomcat classpath must have the class but also the context must have the access to the servlet.class file. The easy and standart way to achieve this is include the class inside of jar file and execute it using java -jar my.jar along with all the dependences uncompress in the same jar file.
To achieve this in gradle I used the shadow gradle plugin, in Maven you can achieve the same with the maven-assembly-plugin
to run the sample the sources are in https://github.com/jordiesc/java/tree/master/embedtomcat/JavaTomcatEmbed
$ gradle build shadowJar
$ cd build/libs
$ java -jar GroovyTomcatEmbed-all.jar
$ cd build/libs
$ java -jar GroovyTomcatEmbed-all.jar
Moreover you will see two versions of the project. they are almost identical apart form some details. The most important is the groovy incompatibility with Java annotations. where you must to turn curly bracktest {} into [] otherwishe the compiler will interpret a Groovy Clousre
The Java way
@WebServlet(
name = "dataServlet",
urlPatterns = {"/datajava"}
)
name = "dataServlet",
urlPatterns = {"/datajava"}
)
The Groovy way
@WebServlet(
name = "dataServlet",
urlPatterns = ["/datagroovy"]
)
name = "dataServlet",
urlPatterns = ["/datagroovy"]
)
just run http://localhost:8080/datajava or http://localhost:8080/datagroovy
Cap comentari:
Publica un comentari a l'entrada