仅在更改时加载属性文件

发布于 2024-10-02 05:54:16 字数 247 浏览 2 评论 0原文

伙计们,我在 jboss 类路径之外创建了一个属性文件

(我保留在外面,因为我可以在运行时更改并且值将被反映)。

我每次都会加载这个属性文件(例如System.getProperty("jboss.base.dir.home"))来进行rmi查找。我认为每次加载相同的文件,即使它没有改变是痛苦。

我想要一些想法,如何检测属性文件中的更改并仅在发生更改时加载。我想到了最后修改的时间戳。请让我知道您的建议。

Guys i have created a properties file outside the jboss classpath

(I kept outside because i can change during runtime and the values will be reflected).

I am loading this property file (like System.getProperty("jboss.base.dir.home")) everytime to do rmi lookup .I think that everytime loading the same file, eventhough it is not changed is pain.

I want ideas , how to detect the change in properties file and load only when there is a change.i thought of having the timestamp of lastmodified .kindly let me know your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-10-09 05:54:16

您可以重用 Apache commons 来读取属性文件:
http://commons.apache.org/configuration/userguide/howto_filebased.html#Automatic_Reloading

或者

您可以自己编程。这将重新实现 apache commons 代码 - 可能需要更少的测试并且更容易出错。如果您坚持这种方法,我认为您可以这样做:

  1. Properties 实例上使用包装类。
  2. 这个包装器类应该(初始化时)从预定义位置(可能是可配置的)加载属性文件
  3. 包装器在初始化后应该启动一个永远运行的线程并执行以下操作:
    • 记录最近读取的属性文件的修改时间戳(参见File类中的方法)
    • 睡眠可配置的时间段
    • 唤醒并检查属性文件的修改时间戳
    • 如果时间戳存在差异 - 将属性文件重新加载到 Properties 实例中。请记住,这必须单独完成(即在没有人访问对象时更改对象)。

华泰

You could reuse Apache commons to read the property file :
http://commons.apache.org/configuration/userguide/howto_filebased.html#Automatic_Reloading

OR

You could do it programatillcay yourself. This would reimplementing the apache commons code - probably with far less testing and more error prone. If you insist on this approach here is that I think you can do :

  1. Use a wrapper class on a Properties instance.
  2. This wrapper class should (when initialized) load the properties file from the predefined location (probably configurable)
  3. The wrapper, post init, should start a thread that would run forever and do this :
    • record the modified timestamp of the properties file recently read (see methods in File class)
    • sleep for a configurable time period
    • wake up and check the modified timestamp of the properties file
    • if there is a difference in the timestamps - reload the properties file into the Properties instance. Remember this must be done in isolation (i.e change the object when no ones accessing it).

HTH

追我者格杀勿论 2024-10-09 05:54:16

也许您可以通过命名约定来做到这一点。例如,如果没有更改,文件将被命名(无论您现在命名它),例如:

foo.properties

但是,另一方面,如果您希望更改它,您可以将其命名为:

foo.properties-changed

然后您可以以编程方式查找'-changed' 文件,加载它,然后去掉后缀。

Perhaps you could do this with a naming convention. For example, if there are no changes the file would be named (whatever you name it now) for example:

foo.properties

But, on the other hand, if you wish to change it, you could name it something like:

foo.properties-changed

You could then programmatically look for the '-changed' file, load it, and then strip off the suffix.

壹場煙雨 2024-10-09 05:54:16

该解决方案的灵感来自于 javamonkey79 的答案以及 JBoss EAP 6.1.0 中处理热部署的方式。我没有在每次更改 foo.properties 文件时对其进行重命名,而是在同一目录中创建了一个额外的 foo.updated 文件。每当我更改 foo.properties 文件中的设置时,我都会将 foo.updated 重命名为 foo.doUpdate。

在启动时从 foo.properties 文件加载设置的单例 bean 中,在尝试从 bean 获取特定值之前,我首先检查配置目录中是否有 foo.doUpdate 文件。如果是这样,我从 foo.properties 文件中读取设置,然后删除 foo.doUpdate 并创建一个名为 foo.updated 的新文件。

This solution was inspired by javamonkey79's answer and by the way hot deployment is handled in JBoss EAP 6.1.0. Instead of renaming the foo.properties file whenever I change it, I created an additional foo.updated file in the same directory. Whenever I change the settings in the foo.properties file, I rename foo.updated to foo.doUpdate.

In the singleton bean that loads the settings from the foo.properties file on startup, before I try to get a certain value from the bean, I first check if there is a foo.doUpdate file in the configuration directory. If so, I read the settings from the foo.properties file, and then delete foo.doUpdate and create a new file named foo.updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文