如何在运行时更改Java属性?

发布于 2024-10-07 18:10:47 字数 305 浏览 8 评论 0原文

问题: 有没有某种方法可以“连接”到正在运行的 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 技术交流群。

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

发布评论

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

评论(5

放手` 2024-10-14 18:10:47

在我的代码之一中找到的示例:

System.setProperty("javax.net.ssl.trustStore", keystore_file); 

您可以运行它来响应“重新配置”查询(将 reconfigure() 添加到您的服务器代码)。

Example found in one of my code:

System.setProperty("javax.net.ssl.trustStore", keystore_file); 

You can run it in response for "reconfigure" query (add reconfigure() to your server code).

无远思近则忧 2024-10-14 18:10:47

许多系统属性仅在启动时进行检查,因此更改它们并不总是有帮助。

我建议您在应用程序中添加支持以通过请求执行更改,以便您的应用程序知道它已经发生并进行相应的处理。

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.

顾铮苏瑾 2024-10-14 18:10:47

正如其他人已经建议的那样,可能无法更改应用程序使用的系统属性值。一种选择可能是重新启动您的应用程序。看来 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.

音栖息无 2024-10-14 18:10:47

这不是长期解决方案,但您可以连接调试器并热交换一些返回所需属性值的代码,而不是查找属性。但这要求您在启动 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.

北陌 2024-10-14 18:10:47

您可以使用 System.setProperty() 方法来完成此操作。
例如:System.setProperty(key, value);

you can do it using System.setProperty() method.
for example: System.setProperty(key, value);

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