Simplest Possible EJB 3.1 Singleton - Injected Into Servlet 3.0, WAR Deployment


@Singleton
public class MasterDataCache {

    private Map cache;

    @PostConstruct
    public void initCache(){
        this.cache = new HashMap();
    }

    public Object get(String key){
        return this.cache.get(key);
    }
    
    public void store(String key,Object value){
        this.cache.put(key, value);
    }
}

How to compile:

You will need the the EJB 3.1 API in the classpath, or at least the @Singleton annotation.

How to deploy:

Just JAR the class and put the JAR into e.g: [glassfishv3]\glassfish\domains\domain1\autodeploy

How to use:


@WebServlet(name="SingletonTester", urlPatterns={"/SingletonTester"})
public class SingletonTester extends HttpServlet {

    @EJB
    MasterDataCache masterDataCache;

    @Override
    public void init(){
     masterDataCache.store("startup", new Date());
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            out.println("Startup time: " + masterDataCache.get("startup") );
        } finally { 
            out.close();
        }
    } 
}

And: there is no XML, strange configuration, libraries, additional frameworks or jars needed...  

The whole WAR-project (Singleton) was checked in into: http://kenai.com/projects/javaee-patterns/. It was developed with Netbeans 6.8 and tested with Glassfish v3. The deployment took: INFO: Deployment of Singleton done is 272 ms.

See also: EJB 3 series and EJB 3.1 From Legacy To Secret Weapon

[See also "Real World Java EE Patterns - Rethinking Best Practices", ServiceStarter page 207 and especially Singleton 211] 


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

hello,

the annotation @WebServlet isn't available for me (running like you glassfish v3 and i have downloaded the ejb module right now...)

what about accessing the cache from a jsp with EL?

The name of the variable "masterDataCache" isn't specified anywhere, is this because is a Singleton and any names with class MasterDataCache is good?

Thank you

Posted by Riccardo Casatta on September 07, 2009 at 07:54 PM CEST #

Singleton looks great at first sight. But diving into spec: "In cases where the container is distributed over many virtual machines, each application will have one bean instance of the Singleton for each JVM."

So it isn't actually a 'application singleton' but a one node singleton.
This is a major missing functionality, because it would stop from using it in the clustered environment. There would still be a need of sth. like Terracota.

What are the reasons for such a Singleton?

Posted by Tomasz Bartczak on October 16, 2009 at 07:33 PM CEST #

Wondering if you could help, where do I get the latest J2EE jar with the EJB 3.1 API?
I have a JAr and Glassfish v3, but it dose not contain the @Singleton class, so I guess it's not the latest.

Cheers
Keith

Posted by Keith on November 11, 2009 at 12:20 PM CET #

Found it!

http://maven.glassfish.org/content/groups/glassfish/org/glassfish/javax.ejb/10.0-SNAPSHOT/

:-D

Posted by Keith on November 11, 2009 at 12:28 PM CET #

Sonarqube 5.6.6 reports issues when using EJB in WebServlet. According to sonar the ejb variable should be final (not possible with injection) or static (not easy testable using EasyMock without adding just another testing framework).

Someone has tips?

Posted by Rene on June 28, 2017 at 04:15 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License