File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes where can i initialize the global parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "where can i initialize the global parameter?" Watch "where can i initialize the global parameter?" New topic
Author

where can i initialize the global parameter?

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
consider my scinario,when i develop project in web tier,i can write a singleton to encaptualte the global parameter,such as jndi look up......,and write a servlet (config it as "load on startup")to instantiate the singleton as soon as the web container is started up,then other classes in the web tier can get the global parameter by using the static get method of the singleton,i think maybe it is so-called "service locator pattern",it works well.
but now,i want to extend such mechanism to ejb tier,that is i also write a singleton to encaptulate the global patameter within the ejb tier,this singleton can be started up automatically by something(in web tier,i can use serlvet to take the responsibility,but i can't such a thing in the ejb tier) as soon as the ejb container is started up,it is only my guess,i don't know if it is realizable,and the following question confuse me:
1 if the ejb container support singleton object,that is if the session bean can visit the singleton in ejb container just as java bean visit the singleton in servlet container?
2 if my suggestion is available,who can take the role to start up the singleton in ejb container just as the servlet in web container takes?

thanks for any instruction!
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Certain application servers have the concept of a "startup class" that may be of use to you. Which application are you using?
Simon
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks for your answer
i use weblogic6.1,how can i set up start up class within the weblogic ejb container?
thanks
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Hi
Here is the bea documentation for a startup classhere
To write a startup class here is an example

import java.util.Hashtable;
import weblogic.common.T3StartupDef;
import weblogic.common.T3ServicesDef;
import weblogic.common.T3Services;
import weblogic.common.T3User;
import weblogic.common.T3Client;
import weblogic.common.AdminServicesDef;
/** Class which is invoked when WLC starts up */
public class StarterUp implements T3StartupDef
{
/** T3ServicesDef type data member */
private T3ServicesDef services = null;
/** Member containing this classes name in string */
private String _className = getClass().getName();
/** Default Constructor -- Required for any class that implements weblogic.common.T3StartupDef */
public StarterUp()
{
}
/** Method called by WLS */
public void setServices(T3ServicesDef services)
{
this.services = services;
}

/**
*/
public String startup(String name, Hashtable args) throws Exception
{
// write your code here
return name;
}
}
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Basically since you want to initialize a singleton this class must be there in your server classpath.
Your server classpath is set in your setenv.bat file.
Since this is there in your server classpath and since the ejb classloader is a sibling of the server classpath the class will be found.
However another strategy is to initialize the singleton when it is called the first time from an ejb. In this case the singleton can be packaged in the ejb too. Note that since the web application is a sibling class loader of the ejb classloader this will be found in the web application too.
Thus getInstance() of the singleton needs to be written like this
class mysingleton{
boolean firstime=true;
static void getInstance(){
if (firstTime){
firstTime=false;
//do initialisation of singleton;
}else{

}
}
}
I would recommend the second way as this does not affect the Server classpath and the ejb can be deployed on any application server without a problem.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Please note my quotes above with respect to classloaders is specific to weblogic implementation in 6.1 and 7.0 and may not be relevant to other application servers.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks for all of your helps
according to the requirement of our project,it require platform independency,the second option of Mahindrakar is more reasonable i think.
but in my ejb tier,there are tens of session bean,and there is not startup order between them,that is i can't decide which one is loaded first and which one is second,so that,i must write the code of initializing the singleton into all of my session beans's start up method,just as your suggestion,in the "getInstance()" method of the singleton,i can decide if execute the initializing action by the "firstTime" switch,is that right?
but i still have following question:
1 do you mean that there is not the same mechanism of config the session bean to load on startup just as the servlet in web container by modifying the deploying descriptor?
2 in which method of the session bean to initialize the singleton? "ejbLoad()" or "ejbActivate()"?i think it should be "ejbLoad()" method,because the ejbLoad method act preceding to the ejbActivate method.
thanks!
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
cong,
I do not get you. Initialization code for the Singleton lies in the singleton and is decided by the swith "firstTime".
So there is no need to initialize the singleton in the EJB.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
The first time you use the singleton by trying to use the getInstance() the singleton will initialize itself.
The architecture is not platform indepependent as the classloader hierarchy is Weblogic based if you are trying to use the singleton in the Web application.
However the architecture is platform independent if you are trying to use the singleton only in the EJB layer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where can i initialize the global parameter?