I think i found a way.
See below.
Please comment on it.


J. Hondius schreef:
Hi,

Please help.
I have a need for a variable that is globally accessible from my service implementation.
The normal way to do this in java is using statics AFAIK.

I am however painfully aware that i cannot use static variables in axis2.
They will be shared across all service instances, and persist after the service call is done.
I believe this has to do with the axis2 classloader.

I really need to use some globally accessible variable in my service implementation though! How do i go about?

My service implementation is a pojo BTW.

Greetings from Holland, Joek

Im registring the "global variable" as a property in the messagecontext.
Access to this "global variable/messagecontect property" is via a getter and setter in a obeject that is a static itself.
Because its a static its acessible from anywhere in the code.
Testing reveils that the values do not get messed up.

PS a am aware that using globals is not a nice thing.

Example code:

the wrapper class for the get/set access
---------------------------------------
public class GlobalHolder{
   // staticly instantiate itself
   public static GlobalHolder globalholder = new GlobalHolder();

   public int getGlobalVar() {
MessageContext messageContext = MessageContext.getCurrentMessageContext();
        return (Integer) messageContext.getProperty("globalvar");
   }

   public int setGlobalVar(int pValue) {
MessageContext messageContext = MessageContext.getCurrentMessageContext();
        messageContext.setProperty("globalvar", pValue);
   }
}


in the Main class
-----------------
public class Mainclass {
   public Mainclass(){
        //constructor: make sure the property exists to avoid null pointers
        GlobalHolder.globalholder.setGlobalVar(0);
   }

   public int myMethod(){
       // go about getting and setting it everywhere in the code.
       GlobalHolder.globalholder.setGlobalVar(700);
       return GlobalHolder.globalholder.getGlobalVar();
   }

}






Reply via email to