GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes EJB: where to store an instance that must be available to every EJB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB: where to store an instance that must be available to every EJB?" Watch "EJB: where to store an instance that must be available to every EJB?" New topic
Author

EJB: where to store an instance that must be available to every EJB?

Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Hello:

I'm developing an EJB application that needs to read a configuration file on its deployment. I want to read several parameters from that file and instantiate an immutable object that store or hold such values. Then I want to share this instance across all the existing EJBs. I should be able to use this instance on any method of that EJBs. I want to keep this instance restricted to the scope of the EJB module because it contains information that it's private, so I can't pass it as an argument to the methods.

I'm planning to use a shared single instance because conceptually there is "one configuration" only, so it makes sense in my opinion. Correct me if I'm wrong.

In some way I want something equivalent to the concept of javax.servlet.ServletContext in the "servlet world" which, among many other things, allows to store global attributes.

Is there something similar that I can use in EJB 3.0 and 3.1? For EJB 3.1 I've read that you can inject a javax.ejb.EJBContext object into your EJBs and get/set data through the java.util.Map returned by the method context.getContextData () . Is this right? What about EJB 3.0?

By the way, to achieve the global availability of this instance in the past, I used the singleton implementation explained in the book "Design Patterns". However I've been convinced that this is not the way to go and that's why I'm changing my strategy.

Thank you.
francesco umani
Greenhorn

Joined: Aug 07, 2013
Posts: 27
I usually create a

@Singleton
public class SingletonBean { ... }

Why not convinced?
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Francesco: first of all, thank you for answering.

It's not that I'm not convinced, he he he. I was just looking for a solution that worked in EJB 3.0 too. Because I've several projects running with that version and would like to use the same methodology.

Anyway, I believe that I'm going to keep the classic singleton implementation from "Design Patterns" in my EJB 3.0 projects, whereas I'm going to use the new Singleton bean in my EJB 3.1 projects.

Salutes ;) .
 
GeeCON Prague 2014
 
subject: EJB: where to store an instance that must be available to every EJB?