仅在更改时加载属性文件
伙计们,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以重用 Apache commons 来读取属性文件:
http://commons.apache.org/configuration/userguide/howto_filebased.html#Automatic_Reloading
或者
您可以自己编程。这将重新实现 apache commons 代码 - 可能需要更少的测试并且更容易出错。如果您坚持这种方法,我认为您可以这样做:
Properties
实例上使用包装类。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 :
Properties
instance.File
class)Properties
instance. Remember this must be done in isolation (i.e change the object when no ones accessing it).HTH
也许您可以通过命名约定来做到这一点。例如,如果没有更改,文件将被命名(无论您现在命名它),例如:
但是,另一方面,如果您希望更改它,您可以将其命名为:
然后您可以以编程方式查找'-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:
But, on the other hand, if you wish to change it, you could name it something like:
You could then programmatically look for the '-changed' file, load it, and then strip off the suffix.
该解决方案的灵感来自于 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.