I like your solution, but I am always uneasy about using static initalizers
when my component sits on top of a more or less complex system. As far as I
know, there is not much in the EJB spec (1.1) on when the bean classes are
loaded. So you are probably limited to doing tasks that do not need more
than the base java classes.
-----Original Message-----
From: Kevin Mukhar [mailto:[EMAIL PROTECTED]]
Sent: Friday, June 08, 2001 6:20 PM
To: [EMAIL PROTECTED]
Subject: Re: A question about singleton
Glyn Normington wrote:
>
> Kevin Mukhar wrote:
>
> >The classic pattern for the Singleton class uses a read-only static field
to
> >hold the Singleton reference, ...
>
> I suppose the initial expression for the static field would be like this:
>
> BeanX singleton = ((BeanXHome)javax.rmi.PortableRemoteObject.narrow(
> new InitialContext().lookup(path), BeanXHome.class)).create(parms)
>
> but where would this code would be placed. It's no good inside ejbCreate
> as ejbCreate is already running on a new instance rather than the
singleton.
> So how could you implement the singleton portably?
This is how I currently load a static member in a bean I developed:
public class MyBean implements SessionBean {
static Hashtable keyValuePairs = createHashtable();
static Hashtable createHashtable() {
Hashtable h = new Hashtable();
//... code to fill hashtable
return h;
}
//... rest of bean methods
}
This code will execute when the class is loaded by the JVM, initializing the
static reference. It will execute before the constructor is called and
before
the call to ejbCreate(). If you wanted to use a singleton to hold a
reference
to a bean, the method would need to include a try-catch block, or put the
initialization into a static block with a try-catch inside the static block:
public class MyBean implements SessionBean {
static BeanX singleton = null;
static {
try {
BeanX singleton = ((BeanXHome)javax.rmi.PortableRemoteObject.narrow(
new InitialContext().lookup(path), BeanXHome.class)).create(parms)
} catch {
}
}
//... rest of bean methods
}
You would also need to implement the bean to deal with an uninitialized
reference if creating the bean fails in some way.
I'm not sure there would be a good use for a static reference to a bean,
though. I use a static data member to hold data used by all the bean
instances. What I do is in line with what the question that started this
thread wanted. I have some data in an LDAP directory. The data does not
change, but is used in the system. Rather than performing an LDAP search
everytime the bean needs the data (hundreds or thousands of times per day),
I
do the search when the bean class is loaded, and save the data into a static
member. This greatly reduces the number of LDAP searches.
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".