DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

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

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
  1. DZone
  2. Coding
  3. Frameworks
  4. Apache TomEE + Shrinkwrap == JavaEE Boot

Apache TomEE + Shrinkwrap == JavaEE Boot

Alex Soto user avatar by
Alex Soto
·
Oct. 01, 14 · Interview
Like (0)
Save
Tweet
Share
5.83K Views

Join the DZone community and get the full member experience.

Join For Free
WARNING: I am not an expert of Spring Boot. There are a lot of things that I find really interesting about it and of course that can really improve your day-to-day work. Moreover I don't have anything against Spring Boot nor people who develop it or use it. But I think that community are overestimating/overvenerating this product.
A year ago I started to receive a lot links about blogposts, tweets, information about Spring Boot. From his website you can read:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that can you can "just run". 
And it seems that this thing has just revolutionized the Java world.

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);
    }
}
As you can see the magic happens inside SpringApplication class which starts an embedded Tomcat or Jetty and using Spring thing it registers this controller. Pretty impressive I know with a few lines you can have an endpoint ready to be used.
But I wonder to myself if it is possible to use the same approach in JavaEE world and with the same low-level and light requirements. And the answer is absolutely. I have just created a really small protoype/proof-of-concept to prove that it is possible.
Also please don't misunderstand me, Spring boot offers a lot of more things apart from self-contained application like monitoring, actuators, or artifact dependency resolution. But these things are only integration with other technologies, my example has been developed from zero in 1 hour and a half so don't expect to have a Spring boot ready to be used.
The first thing to choose is the application server to be used, and in this case there is no doubt that the best one for this task is Apache TomEE. It is a certified web profile Java EE server which takes 1 second to start up and works with default Java memory parameters.

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.

In summary, of course Spring Boot is cool, but with really simple steps you can start having the same in JavaEE world. We (Apache TomEE contributors) are going to start to work on this and expand this idea. 
So don't underestimate Java EE because of Spring Boot.
Spring Framework Spring Boot Apache TomEE application

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: