By Murat Yener

Love it or hate it, Java EE has been the most adapted platform since mainframe and Cobol. Although Java is best known, there has been a huge debate about Java EE and other frameworks which offer similar functionality. Each camp has its own fanatics and usually they are pretty religious about their idea.

If you used to watch ‘How I met your mother’, you may remember the ‘Ewok Theory’ from Barney Stinson. Star Wars fans usually have mixed feelings on teddy bear like creatures called Ewoks. According to Barney, appreciation for Ewoks can be explained with the Ewok Line, which basically suggests that anyone born May 25, 1973 and was old enough to think Ewoks are childish by the time they appeared on the Return of the Jedi, simply hates them. However, those who were born after, thus being young enough to find them cute, loved them because Ewoks reminded them of teddy bears.

Screen Shot 2014-11-11 at 12.35.18

Using Spring on Tomcat instead of EJBs and JavaEE  on an application server would greatly increase the development productivity but still JavaEE 5 was big step forward designing, leveraging and architecting the Enterprise Java platform from scratch. Java EE  6 aimed at the weakest remaining points. Maturing the platform while offering a fast, lightweight runtime and development platform. Those who suggest using Tomcat because it starts up in few seconds where a Java EE Application Server would take minutes lost their ground with the new Java EE servers such as GlassFish or JBoss, which would start just a little slower than Tomcat.

Still, if you are a big fan of Tomcat, it already has a Java EE certified version called TomEE which uses OpenEJB / OpenJPA and boots in just a few seconds more than a regular Tomcat. If you are looking for professional support it is not hard to find a vendor like Payara who deliver a commercial version of GlassFish. Java EE 6 not only offers the standards, ease of use, and a proven design, but also something which has been neglected until now: the developer productivity with the Web Profile which support all you need other than the MDBs. You do not need full blown heavy servers or Frankestein IDEs, nor do you require need coffee breaks on builds and server restarts. Downloading the Java E E6 SDK which is bundled with GlassFish and your favorite IDE is all you need. You can also use JBoss or TomEE as your server.

After a similar blog post of mine became popular years ago, I decided to write a series focusing on Design Patterns in Java EE to show how is easy to use patterns in Java EE without writing much code which turn out to be pretty popular and leading a way to publish a book from Wiley. (Find the book here). Here is a brief intro to some of the patterns we wrote about in the book.

Singleton

Singleton Pattern relies on a very simple idea of having just one single instance of an object throughout all system. However, the implementation might be tricky. Due to race condition, implementing simple singleton might become troublesome. There are several tricks like double checking, using enums or static references.

Java EE has a very simple builtin implementation for singletons. If you want to turn an EJB into a Singleton, a simple annotation would do the work.

package com.devchronicles.singleton;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import java.util.logging.Logger;

@Singleton
public class MySingleton {
    public String doSomething(){
        return “DONE”;
    }
}

Singletons in Java EE are very flexible, you can configure the order of the startup and even ask them to do stuff at initialization.

Dependency Injection

Dependency Injection or Inversion of control (IoC) relies on the idea of loosely coupling by injecting required resources and eliminating the keyword “new”. Without DI, J2EE developers needed to perform JNDI lookups to access JNDI resources such as beans or create the needed resource with the “new” keyword. Initial DI frameworks still relied on heavy XML configuration but Java EE does not.

Injecting a resource is as simple as placing an annotation.

package com.devchronicale.di;
import javax.inject.Inject;

@Singleton
public class MySingleton {
    @Inject
    private SomeOtherBean sob;

    public String doSomething(){
        return sob.doSomethingElse();
    }
}

Dependency Injection can work using types or named beans. Java EE also introduces CDI which introduces the magic of EJB container to simple POJOs. CDI can work on any POJO (by instantiating and injecting objects into each other.

Factory

Factory Patterns are used widely to encapsulate object creation, thus, the code is very prone to change. Java EE offers an elegant an easy way to create factories. Once again this pattern can be implemented with annotations without any XML configuration. Let’s create a factory to create the SomeOtherBean in the previous example.

package com.devchronicles.producer;
import javax.enterprise.inject.Produces;

public class SomeOtherBeanProducer {
    @Produces
    public SomeOtherBean getMessage(){
        return new SomeOtherBean(“with”, “some”, “options”);
    }
}

Any Inject annotation with the return type of the method annotated with Produces will be automatically produced via our factory and be injected. The type of the Produces can be tweaked by using object types or annotations.

Observer Pattern

Observer Pattern might be one of widely used and useful pattern in frameworks, UI frameworks and even other patterns such as MVC. Observers work effectively on listening resources which subject to change. Once again, Observers are very easy to implement with Java EE without any XML configuration. Let’s start with a resource which will publish updates.

package com.devchronicles.observer;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventPublisher {
    @Inject
    private String message;

    @Inject
    Event<String> event;

    public void publishEvent(){
        event.fire(“Something has changed!!!”);
    }
}

Since the publisher is ready to publish updates now it is time to listen them. A simple Observes annotation will be enough for registering the listener bean.

package com.devchronicles.observer;

import javax.ejb.Stateless;
import javax.enterprise.event.Observes;

@Stateless
public class MyObserver {

    public void someDataChanged(@Observes String message){
        System.out.println(“Change: " + message);
    }
}

Those are only a few that I quickly selected from the book. There is a long list of patterns which can be used with zero boilerplate code and minimal configuration in Java EE.

No matter which camp you are in, JavaEE is definitely worth to try. It might have taken some time (and lots of grey hairs) but by following the path which frameworks and the community has pointed, Java EE became a rock solid, cool and productive environment. Some may argue about the vendor support, but as the community shifts to new versions of JavaEE, all vendors would only left with one choice to follow – it is for sure that, whatever happens, Java will be the future of development in the enterprise world.

Java EE and the Ewoks

| Server Side Java| 1,287 views | 0 Comments
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>