简体   繁体   English

Java:我们可以重用MBeanServerConnection吗?

[英]Java: Can we reuse MBeanServerConnection without any issue?

  1. Can we assign MBeanServerConnection object to a static variable and reuse? 我们可以将MBeanServerConnection对象分配给静态变量并重用吗?
  2. 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.

相关问题 在Java中如何在不从字符串中删除任何字符的情况下拆分字符串? - How can we split a string without removing any characters from the string in Java? 我们可以在Java页面中调用任何视图而不调用其ID吗? - Can we call any view in java page without calling its id? 我们如何在不使用任何内置函数的情况下找到Java中String的长度? (甚至不包括charAt()或toCharArray()或length()) - How can we find the length of a String in Java without using any inbuilt functions? (not even charAt() or toCharArray() or length()) 我们可以重用Gson对象吗? - Can we reuse a Gson object? 我们可以将Java EE war文件部署到Google App Engine而不进行任何更改吗? - Can we deploy a Java EE war file to Google App Engine without any changes? TestNG/RestAssured:有什么方法可以在不同的类中重用 @Test 方法? - TestNG/RestAssured : Is there any way we can reuse @Test methods in different classes? 如何在Java中使用TripleDESCryptoServiceProvider而没有任何问题 - how to use TripleDESCryptoServiceProvider in java without any issue 没有继承的Java代码重用 - Java code reuse without inheritance 我们如何重用在JSP Page中定义的JSP块; 没有创建另一个JSP文件? - How can we reuse JSP chunk defined within JSP Page; without creating another JSP file? 我们可以重用hbase Java客户端多线程之间的连接吗? - Can we reuse the hbase Java client Connection between multi-threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM