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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4469774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:32:19  来源:igfitidea点击:

How to change Java properties at runtime?

javajbosspropertiesruntime

提问by Bruno Teixeira

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?

问题:是否有某种方法可以“连接”到正在运行的 JVM 并更改系统属性(由 -Dproperty=value 给出),而无需停止 JVM 并且没有编程方法来执行此操作?

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?

现在,上下文:我有一个在远程机器上运行的 JBoss 服务器,它托管我的应用程序,还有其他应用程序。那些其他应用程序可能不会停止。使用仅与我的应用程序相关的特定 -D 属性调用服务器。在服务器启动时为该属性分配了错误的值。我需要更改该属性。最简单的方法是重新启动 JBoss,但这会导致所有应用程序停机一段时间。有没有办法在不停止除我自己的应用程序之外的任何应用程序的情况下更改该属性?

Thank you!

谢谢!

回答by Micha? Niklas

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).

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

回答by Peter Lawrey

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.

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

回答by Sanjay T. Sharma

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 herethough I haven't actually tried it out.

正如其他人已经建议的那样,可能无法更改您的应用程序使用的系统属性值。一种选择可能是重新启动您的应用程序。Jboss 似乎为 Web 应用程序提供了启用 JMX 的停止/启动功能,您可以在此处阅读,尽管我还没有真正尝试过。

回答by OrangeDog

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.

这不是一个长期的解决方案,但您可以连接调试器并热交换一些返回您想要的属性值的代码,而不是查找属性。这需要您在启动 JBoss 时启用远程调试。

回答by GuruKulki

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

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