Adam Bien's Weblog
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]
Posted at 09:00AM Aug 18, 2009 by Adam Bien in Real World Java EE Patterns - Rethinking Best Practices | Kommentare[4]
[my tweets]
Rss My book: Real World Java EE - Rethinking Best Practices


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
Gesendet von Riccardo Casatta am September 07, 2009 at 05: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?
Gesendet von Tomasz Bartczak am October 16, 2009 at 05: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
Gesendet von Keith am November 11, 2009 at 11:20 AM CET #
Found it!
http://maven.glassfish.org/content/groups/glassfish/org/glassfish/javax.ejb/10.0-SNAPSHOT/
:-D
Gesendet von Keith am November 11, 2009 at 11:28 AM CET #