[英]Java: Can we reuse MBeanServerConnection without any issue?
If yes, which moment we need to call JMXConnector.close() method? 如果是,我们什么时候需要调用JMXConnector.close()方法?
public class MyClass { public static JMXConnector jmxc = . . . public static MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ......... public logSomeJmxStats() { mbsc.invoke(.....) } public updateSomeJmxStats() { mbsc.invoke(.....) } }
This Java class is used by Java application running as a service. 作为服务运行的Java应用程序使用此Java类。
This is from the documentation of JMXConnector.getMBeanServerConnection : 这来自JMXConnector.getMBeanServerConnection的文档:
For a given JMXConnector and Subject, two successful calls to this method will usually return the same MBeanServerConnection object, though this is not required.
对于给定的JMXConnector和Subject,对该方法的两次成功调用通常将返回相同的MBeanServerConnection对象,尽管这不是必需的。
So I would think that you safely may reuse it. 因此,我认为您可以安全地重用它。 Though you may be safer by not reusing it.
尽管不重用它可能会更安全。
You should call close if noone is going tho use the connection again. 如果没有人要再次使用该连接,则应致电close。 This may be easier to achieve if you don't put the connection in a static field but an instance field.
如果您不将连接放在静态字段中,而是在实例字段中,则可能更容易实现。 So when you shutdown your application you can hook onto that an close the connection.
因此,当您关闭应用程序时,可以挂钩关闭连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.