开发者

Load external properties files into EJB 3 app running on WebLogic 11

开发者 https://www.devze.com 2023-03-17 00:58 出处:网络
Am researching the best way to load external properties files from and EJB 3 app whose EAR file is deployed to WebLogic.

Am researching the best way to load external properties files from and EJB 3 app whose EAR file is deployed to WebLogic.

Was thinking about using an init servlet but I read somewhere that it would be too slow (e.g. my message handler might receive a message from my JMS queue before the init servlet runs).

Suppose I have multiple property files or one file here:

~/opt/conf/

So far, I feel that the best possible solution is by using a Web Logic application lifecycle event where the code to read the properties files during pre-st开发者_如何学JAVAart:

import weblogic.application.ApplicationLifecycleListener;
import weblogic.application.ApplicationLifecycleEvent;

public class MyListener extends ApplicationLifecycleListener {
   public void preStart(ApplicationLifecycleEvent evt) {
      // Load properties files
   } 
}

See: http://download.oracle.com/docs/cd/E13222_01/wls/docs90/programming/lifecycle.html

What would happen if the server is already running, would post start be a viable solution?

Can anyone think of any alternative ways that are better?


It really depends on how often you want the properties to be reloaded. One approach I have taken is to have a properties file wrapper (singleton) that has a configurable parameter that defines how often the files should be reloaded. I would then always read properties through that wrapper and it would reload the properties ever 15 minutes (similar to Log4J's ConfigureAndWatch). That way, if I wanted to, I can change properties without changing the state of a deployed application.

This also allows you to load properties from a database, instead of a file. That way you can have a level of confidence that properties are consistent across the nodes in a cluster and it reduces complexity associated with managing a config file for each node.

I prefer that over tying it to a lifecycle event. If you weren't ever going to change them, then make them static constants somewhere :)

Here is an example implementation to give you an idea:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

/**
 * User: jeffrey.a.west
 * Date: Jul 1, 2011
 * Time: 8:43:55 AM
 */
public class ReloadingProperties
{
  private final String lockObject = "LockMe";
  private long lastLoadTime = 0;
  private long reloadInterval;
  private String filePath;
  private Properties properties;

  private static final Map<String, ReloadingProperties> instanceMap;
  private static final long DEFAULT_RELOAD_INTERVAL = 1000 * 60 * 5;

  public static void main(String[] args)
  {
    ReloadingProperties props = ReloadingProperties.getInstance("myProperties.properties");
    System.out.println(props.getProperty("example"));

    try
    {
      Thread.sleep(6000);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }

    System.out.println(props.getProperty("example"));
  }

  static
  {
    instanceMap = new HashMap(31);
  }

  public static ReloadingProperties getInstance(String filePath)
  {
    ReloadingProperties instance = instanceMap.get(filePath);

    if (instance == null)
    {
      instance = new ReloadingProperties(filePath, DEFAULT_RELOAD_INTERVAL);

      synchronized (instanceMap)
      {
        instanceMap.put(filePath, instance);
      }
    }

    return instance;
  }

  private ReloadingProperties(String filePath, long reloadInterval)
  {
    this.reloadInterval = reloadInterval;
    this.filePath = filePath;
  }

  private void checkRefresh()
  {
    long currentTime = System.currentTimeMillis();
    long sinceLastLoad = currentTime - lastLoadTime;

    if (properties == null || sinceLastLoad > reloadInterval)
    {
      System.out.println("Reloading!");
      lastLoadTime = System.currentTimeMillis();
      Properties newProperties = new Properties();
      FileInputStream fileIn = null;

      synchronized (lockObject)
      {
        try
        {
          fileIn = new FileInputStream(filePath);
          newProperties.load(fileIn);
        }
        catch (FileNotFoundException e)
        {
          e.printStackTrace();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
        finally
        {
          if (fileIn != null)
          {
            try
            {
              fileIn.close();
            }
            catch (IOException e)
            {
              e.printStackTrace();
            }
          }
        }

        properties = newProperties;
      }
    }
  }

  public String getProperty(String key, String defaultValue)
  {
    checkRefresh();
    return properties.getProperty(key, defaultValue);
  }


  public String getProperty(String key)
  {
    checkRefresh();
    return properties.getProperty(key);
  }
}


Figured it out...

See the corresponding / related post on Stack Overflow.

0

精彩评论

暂无评论...
验证码 换一张
取 消