Sharing Environment Variables


EJB design: Sharing Environment Variables

  1. Sharing Environment Variables (4 messages)

    I have a simple question that I imagine has a simple answer, but I have not been able to find it. Is it possible for two or more beans to share the same environment variable? And if so, how? If, for example, I want BeanA and BeanB to both have access to a variable called "taxRate" that will be utilized as a java.lang.Float, how would I make that happen?

    Thanks for any insight you can provide.

    Threaded Messages (4)

  2. Join A&B by C[ Go to top ]

    More simple solution is to create additional EJB, that will supply others with *named* variables. So, it is only one place where You can/must manage values.
    There are lot of possible implementations, lets try to use StatelessEJB:

    class SharedValues ... {
      public Float getTaxRate() {...}

    and in BeanA & BeanB just ask "SharedValues" about desired variable:

      SharedValues shared = ...;

      Float f = shared.getTaxRate();

    That's all. Other in Your hand.

    But TaxRate can be changed with time -- economics has float nature. Hence, environment of container isn't good place.
  3. Join A&B by C[ Go to top ]

    OK, I follow your solution. But 2 questions:

    1) What did you mean that the float nature of a tax rate makes the container a poor place for the value?

    2) By suggesting this solution, are you implying that there is no way to share variables inherently?

    It seems though that your solution would demand code changes every time you add environment variables, for each new variable would require a new method for retrieving it. Is this not so?

    Thanks for the insights.
  4. Version 2[ Go to top ]

    1) I mean that after deploy, if TaxRate must be changed, someone must do big amount of brute work -- examine all EJBs and change every "taxRate" entry. Even if TaxRate exsists in one container only, Your must replace one with new value (much easy).
    But not all Application Servers allow execute such operations on the fly. In common case Your must redeploy whole application or some its containers (depend on AppServer).

    In any case check all in practice(I also can be mistaken).

    2) Not exactly. You can use JNDI to store all shared variables (global environment).

    class ... {
      final static String SHARED = "java:comp/env/shared_values/";

      void DoSomethingUseful() {
        try {
         Context ctx = new InitialContext(SHARED);

         Float taxRate = (Float) ctx.lookup("taxRate");
        } catch ...

    But in such case You must take care about this global environment. You even can make special framework to reflect some DB table into this JNDI branch.
    More complicated problem here -- How to store this shared variables in JNDI?

    3) About code changes... Are all clear?

    class Shared ... {
      Object getObject(String name) throws NoSuchVariable {
        Object obj = ... retrieve/fetch *named* falue from Hash/JNDI/DB...

        if (obj == nil) then throw NoSuchVariable("No variable associated with " + name);

        return obj;
      Float getFloat(String name) throws NoSuchVariable {
        return (Float)getObject(name)
      String getString(String name) throws NoSuchVariable {
        return (String)getObject(name);
  5. Sharing Environment Variables[ Go to top ]

    The most simple way to do so is to have a Class with Static variable "taxRate" which can be updated/read from any of the two EJB's. Here you can share the same variable within the same JVM without any hassle.