如何在运行时更改Java属性?
问题: 有没有某种方法可以“连接”到正在运行的 JVM 并更改系统属性(由 -Dproperty=value 给出),而无需停止 JVM 且无需编写相应的方法?
现在,上下文: 我有一个在远程计算机上运行的 JBoss 服务器,它托管我的应用程序,但也托管其他应用程序。这些其他应用程序可能不会停止。使用仅与我的应用程序相关的特定 -D 属性调用服务器。服务器启动时该属性被分配了错误的值。我需要更改该属性。最简单的方法是重新启动 JBoss,但这会导致所有应用程序停机一段时间。有没有办法在不停止除我自己的应用程序之外的任何应用程序的情况下更改该属性?
谢谢你!
The question:
Is there some way to "connect" to a running JVM and change system properties (given by -Dproperty=value) without stopping the JVM and without having programmed a way of doing it?
Now, the context:
I have a JBoss server running on a remote machine, which is hosting my application, but also other applications. Those other apps may not be stopped. The server is invoked with a specific -D property that is relevant to my application only. This property was assigned the wrong value at server startup. I need to change that property. The easiest way would be to restart JBoss, but that would cause all apps to go down for some time. Is there a way of changing that property without stopping any applications but my own?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我的代码之一中找到的示例:
您可以运行它来响应“重新配置”查询(将
reconfigure()
添加到您的服务器代码)。Example found in one of my code:
You can run it in response for "reconfigure" query (add
reconfigure()
to your server code).许多系统属性仅在启动时进行检查,因此更改它们并不总是有帮助。
我建议您在应用程序中添加支持以通过请求执行更改,以便您的应用程序知道它已经发生并进行相应的处理。
Many system properties are only examined on start up so changing them doesn't always help.
I suggest you add support within your application to perform the change via a request so your application will know that it has happened and handle it accordingly.
正如其他人已经建议的那样,可能无法更改应用程序使用的系统属性值。一种选择可能是重新启动您的应用程序。看来 Jboss 为 Web 应用程序提供了启用 JMX 的停止/启动功能,
As others have already suggested, probably can't change the system property value used by your application. One option might be restarting your application. It seems that Jboss offers JMX enabled stop/start ability for web applications which you can read here though I haven't actually tried it out.
这不是长期解决方案,但您可以连接调试器并热交换一些返回所需属性值的代码,而不是查找属性。但这要求您在启动 JBoss 时启用远程调试。
Not a long-term solution, but you could connect a debugger and hot-swap some code that returns the property value you want instead of looking up the property. This requires that you enabled remote debugging when you started JBoss though.
您可以使用 System.setProperty() 方法来完成此操作。
例如:System.setProperty(key, value);
you can do it using System.setProperty() method.
for example: System.setProperty(key, value);