Apache TomEE + Shrinkwrap == JavaEE Boot
Join the DZone community and get the full member experience.
Join For Free
For example a Spring MVC (RESTful as well) application in Spring Boot looks like:
@Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
So I added tomee dependencies in my pom.xml file.
<dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>tomee-embedded</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>openejb-cxf-rs</artifactId> <version>4.7.1</version> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>tomee-jaxrs</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>org.jboss.shrinkwrap</groupId> <artifactId>shrinkwrap-depchain</artifactId> <version>1.2.2</version> <type>pom</type> </dependency> </dependencies>
In used tomee embedded version (1.7.1) you can only deploy applications contained inside a file, you cannot add for example a Servlet programmatically like it is done in Tomcat. This may change in near future of embedded tomee API, but for now we are going to use ShrinkWrap to create these deployment files in a programmatic way.
This is what we want to do:
import javax.ejb.Stateless; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Stateless @Path("/sample") public class SampleController { @GET @Produces("text/plain") public String sample() { return "Hello World"; } public static void main(String args[]) { TomEEApplication.run(HelloWorldServlet.class, SampleController.class); } }
Notice that we are only importing JavaEE classes and it is as reduced as Spring Boot one. In only 2 seconds the application is ready to be used. Keep in mind that you can use any feature provided by web profile spec as well as JAX-RS or JMS. So for example you can use JPA, Bean Validation, EJBs, CDI, ...
And what's inside TomEEApplication? You will be surprised a class with only 70 lines:
public class TomEEApplication { private static void startAndDeploy(Archive<?> archive) { Container container; try { Configuration configuration = new Configuration(); String tomeeDir = Files.createTempDirectory("apache-tomee").toFile().getAbsolutePath(); configuration.setDir(tomeeDir); configuration.setHttpPort(8080); container = new Container(); container.setup(configuration); final File app = new File(Files.createTempDirectory("app").toFile().getAbsolutePath()); app.deleteOnExit(); File target = new File(app, "app.war"); archive.as(ZipExporter.class).exportTo(target, true); container.start(); container.deploy("app", target); container.await(); } catch (Exception e) { throw new IllegalArgumentException(e); } registerShutdownHook(container); } private static void registerShutdownHook(final Container container) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { if(container != null) { container.stop(); } } catch (final Exception e) { throw new IllegalArgumentException(e); } } }); } public static void run(Class<?> ... clazzes) { run(ShrinkWrap.create(WebArchive.class).addClasses(clazzes)); } public static void run(WebArchive archive) { startAndDeploy(archive); } }
As you may see it is really simple piece of code and for example configuration or name of the application is hardcoded, but see that with several small easy changes you may start configuring server, application and so on.
Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
WireMock: The Ridiculously Easy Way (For Spring Microservices)
-
Fun Is the Glue That Makes Everything Stick, Also the OCP
-
Integration Architecture Guiding Principles, A Reference
-
Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
Comments