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 Mutual Object for all users 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 "Mutual Object for all users" Watch "Mutual Object for all users" New topic
Author

Mutual Object for all users

Alex Tosh
Greenhorn

Joined: Jul 17, 2002
Posts: 13
Hi there,
we need to provide an object within our J2EE-Architecture that stores a value (this is indeed not a very difficult task ). This Object should be accessible & the relevant attribute should be read/writable for all users of the application. The object should not be an entity bean - no persistent storage is needed - and it has to be thread-save. What would you recommend to use? Something like the ServletContext-Object in a web-app?
Thank you
Alex
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
I would just use a static property of an object. Like this:
public class AppProperties {
private static String myProp = "default";
public static String getMyProp() {
return myProp;
}
public static void setMyProp(String newVal) {
myProp = newVal;
}
}
Then you can get it from anywhere by using:
AppProperties.getMyProp();
and set it using:
AppProperties.setMyProp(newVal);
This really isn't a J2EE question, just a beginner Java question.
Alex Tosh
Greenhorn

Joined: Jul 17, 2002
Posts: 13
I knew about the usage of static variables; that's not new to me.
But I was wondering if there is a possibility to use an application-context variable in the EJB-container - as is exists is the web-container (ServletContext-object).
Maybe the question was not exact enough.
Thank you anyway
Alex
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
There's a reason that neither the application context (which you can't access from the EJB container, btw) or static variables will work for this -- neither one will work in a clustered environment.
You say there's no need for the value to be persistent, but in fact making this an entity bean is probably your best, most scaleable option.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Alex Tosh
Greenhorn

Joined: Jul 17, 2002
Posts: 13
Hi Kyle,
thank you for this comment - that's an interesting one. What about performance using an entity bean - what exactly happens when you update a record in an entity-bean. I read that the entity-bean instance gets a flag beeing not valid anymore. The next request initiates a new instance of the entity-bean which makes the hole thing quite slow. Is this correct?
cheers
Alex
Nitzan Levi
Greenhorn

Joined: Feb 19, 2003
Posts: 22
hi,
it's very importent to know what's the future use of the application. if it's a small application that will always run on a single server then you can use the static approach that will be the simplest and with less cost.
if your application needs to run in a clusterd environment than you need a more copmlex solution and the entity bean will be a good one. the cost of an entity bean (if you dont use persistence) is minimal for that complex environment !!
like every solution there's a pro's end con's the best solution depends on the requierments


nitzan levi :-)
Alex Tosh
Greenhorn

Joined: Jul 17, 2002
Posts: 13
Hi Nitzan,
thank you for the tip. I don't know yet if the app will run in a clustered environment, but to assure that it will run everywhere, I'm going to use an entity bean.
regards
Alex
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mutual Object for all users