IC5Notice: We have upgraded developerWorks Community to the latest version of IBM Connections. For more information, read our upgrade FAQ.
Topic
  • 3 replies
  • Latest Post - ‏2013-04-05T22:33:20Z by SystemAdmin
IsaacGraf
IsaacGraf
5 Posts

Pinned topic Static variables across ear / war

‏2013-04-05T12:03:56Z |
For the application that I'm porting to Liberty, most of the jars are configured at the ear level.
There is a war under the ear that configures a JAX-WS servlet endpoint and maps the incoming soap calls to implementation classes that are in a jar inside the ear.
I'm noticing that when a static variable is set in the ear, this variable setting is not visible to the call inititated by jaxws servlet.
This structure worked fine in eWAS and the static variable in the ear was visible from the servlet in the war.
Is this something that can be corrected with configuration in Liberty profile?

Thanks very much,
Isaac
  • Jacek_Laskowski
    Jacek_Laskowski
    133 Posts

    Re: Static variables across ear / war

    ‏2013-04-05T12:26:26Z  
    > {quote:title=IsaacGraf wrote:}{quote}
    > I'm noticing that when a static variable is set in the ear, this variable setting is not visible to the call inititated by jaxws servlet.

    Hi,

    I hope it's only me who got struck by the sentence above as "a static variable" is a kind of oxymoron and should never be used in Java (and perhaps many other languages).

    With that said, I do understand there's a code that may not obey the rule and with a need to migrate to WLP, refactoring might not be an option.

    How does the static variable is set and accessed in JAXWS servlet? A bit of code might be of a great help.

    Jacek
    Japila :: verba docent, exempla trahunt
  • IsaacGraf
    IsaacGraf
    5 Posts

    Re: Static variables across ear / war

    ‏2013-04-05T12:51:09Z  
    > {quote:title=IsaacGraf wrote:}{quote}
    > I'm noticing that when a static variable is set in the ear, this variable setting is not visible to the call inititated by jaxws servlet.

    Hi,

    I hope it's only me who got struck by the sentence above as "a static variable" is a kind of oxymoron and should never be used in Java (and perhaps many other languages).

    With that said, I do understand there's a code that may not obey the rule and with a need to migrate to WLP, refactoring might not be an option.

    How does the static variable is set and accessed in JAXWS servlet? A bit of code might be of a great help.

    Jacek
    Japila :: verba docent, exempla trahunt
    Thanks for your reply.
    There's a class with a variable like:

    private static boolean primaryMode;

    and a static methods

    public static void setPrimaryMode(boolean b) {
    primaryMode = b;
    }

    public static boolean isPrimaryMode() {
    return primaryMode;
    }

    There is code in the ear that sets and gets this value;

    The jaxws servlet then invokes a class in the ear that calls the isPrimaryMode() method and it always returns false, even though it has been set to true by a thread running inside the ear.

    Thanks,
    Isaac
  • SystemAdmin
    SystemAdmin
    590 Posts

    Re: Static variables across ear / war

    ‏2013-04-05T22:33:20Z  
    • IsaacGraf
    • ‏2013-04-05T12:51:09Z
    Thanks for your reply.
    There's a class with a variable like:

    private static boolean primaryMode;

    and a static methods

    public static void setPrimaryMode(boolean b) {
    primaryMode = b;
    }

    public static boolean isPrimaryMode() {
    return primaryMode;
    }

    There is code in the ear that sets and gets this value;

    The jaxws servlet then invokes a class in the ear that calls the isPrimaryMode() method and it always returns false, even though it has been set to true by a thread running inside the ear.

    Thanks,
    Isaac
    Hi Isaac,

    Static variables are per classloader. (Look up gotchas with the singleton pattern for extensive discussion.)

    In an OSGI environment it's best to not make assumptions about classloaders in your server.

    I worked with Apache Sling some time ago and we would have created something like a "configuration service" defined by OSGI. I wonder if something close to that could work for you.

    In that case, your servlet would acquire a reference to the service and then call isPrimaryMode() on it. When your EJB wants to update the mode it would acquire the service and call setPrimaryMode() on it.

    Hope that helps. Maybe someone more familiar with WLP can fill in more specific detail.

    Regards,
    -james.