0

我正在研究从其 EAR 文件部署到 WebLogic 的 EJB 3 应用程序加载外部属性文件的最佳方法。

正在考虑使用 init servlet,但我在某处读到它会太慢(例如,我的消息处理程序可能在 init servlet 运行之前从我的 JMS 队列接收到一条消息)。

假设我这里有多个属性文件或一个文件:

~/选择/配置/

到目前为止,我认为最好的解决方案是使用 Web Logic 应用程序生命周期事件,其中代码在预启动期间读取属性文件:

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

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

请参阅:http: //download.oracle.com/docs/cd/E13222_01/wls/docs90/programming/lifecycle.html

如果服务器已经在运行会发生什么,启动后会是一个可行的解决方案吗?

谁能想到任何更好的替代方法?

4

2 回答 2

2

这实际上取决于您希望重新加载属性的频率。我采用的一种方法是拥有一个属性文件包装器(单例),它具有一个可配置的参数,该参数定义了应该多久重新加载文件。然后我总是通过该包装器读取属性,它会每 15 分钟重新加载一次属性(类似于 Log4J 的 ConfigureAndWatch)。这样,如果我愿意,我可以在不更改已部署应用程序状态的情况下更改属性。

这也允许您从数据库而不是文件加载属性。这样,您就可以确信集群中的节点之间的属性是一致的,并且降低了与管理每个节点的配置文件相关的复杂性。

我更喜欢将它与生命周期事件联系起来。如果您不打算更改它们,请在某处将它们设为静态常量:)

这是一个示例实现,可以为您提供一个想法:

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);
  }
}
于 2011-06-30T22:01:15.800 回答
0

弄清楚了...

请参阅Stack Overflow 上的相应/相关帖子。

于 2011-07-05T18:12:17.807 回答