Jboss 如何卸载loadAgent()加载的代理
我正在使用jdk1.6。我已经在运行时成功加载了一个jar。代码如下:Jboss 如何卸载loadAgent()加载的代理,jboss,aop,agent,cglib,javaagents,Jboss,Aop,Agent,Cglib,Javaagents,我正在使用jdk1.6。我已经在运行时成功加载了一个jar。代码如下: vm = VirtualMachine.attach(vid); vm.loadAgent(agentPath); 现在我不想在运行时卸载此代理。没有API文档可以这样做。谁能给我一些建议吗?谢谢 编辑 更多代码 GlobalVariables.vm = VirtualMachine.attach(vid); // Check to see if transformer agent is installed if(!Gl
vm = VirtualMachine.attach(vid);
vm.loadAgent(agentPath);
现在我不想在运行时卸载此代理。没有API文档可以这样做。谁能给我一些建议吗?谢谢
编辑
更多代码
GlobalVariables.vm = VirtualMachine.attach(vid);
// Check to see if transformer agent is installed
if(!GlobalVariables.vm.getSystemProperties().contains("demo.agent.installed")) {
System.out.println("Load agent");
GlobalVariables.vm.loadAgent(agentPath);
}
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
if(null == GlobalVariables.connectorAddress) {
// It's not, so install the management agent
String javaHome = GlobalVariables.vm.getSystemProperties().getProperty("java.home");
File managementAgentJarFile = new File(javaHome + File.separator + "lib" + File.separator + "management-agent.jar");
GlobalVariables.vm.loadAgent(managementAgentJarFile.getAbsolutePath());
System.out.println("Load Management agent");
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
// Now it's installed
}
// Now connect and transform the classnames provided in the remaining args.
//JMXConnector connector = null;
try {
// This is the ObjectName of the MBean registered when loaded.jar was installed.
//ObjectName on = new ObjectName("transformer:service=DemoTransformer");
GlobalVariables.on = new ObjectName("transformer:service=DemoTransformer");
// Here we're connecting to the target JVM through the management agent
GlobalVariables.connector = JMXConnectorFactory.connect(new JMXServiceURL(GlobalVariables.connectorAddress));
GlobalVariables.server = GlobalVariables.connector.getMBeanServerConnection();
System.out.println("MBean Server connection...");
// Call transformClass on the transformer MBean
GlobalVariables.server.invoke(GlobalVariables.on, "transformClass", new Object[]{className, args}, new String[]{String.class.getName(), String.class.getName()});
} catch (Exception ex) {
ex.printStackTrace(System.err);
} finally {
if(GlobalVariables.connector!=null) try { GlobalVariables.connector.close(); } catch (Exception e) {}
GlobalVariables.vm.detach();
System.out.println("Has disconnected");
}
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import com.sun.tools.attach.VirtualMachine;
public class GlobalVariables{
public static MBeanServerConnection server;
public static ObjectName on;
public static JMXConnector connector = null;
public static String connectorAddress = null;
public static VirtualMachine vm;
}
我当时做了什么
我再次运行上述代码以再次加载这两个代理。但我有错误
错误
GlobalVariables.vm = VirtualMachine.attach(vid);
// Check to see if transformer agent is installed
if(!GlobalVariables.vm.getSystemProperties().contains("demo.agent.installed")) {
System.out.println("Load agent");
GlobalVariables.vm.loadAgent(agentPath);
}
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
if(null == GlobalVariables.connectorAddress) {
// It's not, so install the management agent
String javaHome = GlobalVariables.vm.getSystemProperties().getProperty("java.home");
File managementAgentJarFile = new File(javaHome + File.separator + "lib" + File.separator + "management-agent.jar");
GlobalVariables.vm.loadAgent(managementAgentJarFile.getAbsolutePath());
System.out.println("Load Management agent");
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
// Now it's installed
}
// Now connect and transform the classnames provided in the remaining args.
//JMXConnector connector = null;
try {
// This is the ObjectName of the MBean registered when loaded.jar was installed.
//ObjectName on = new ObjectName("transformer:service=DemoTransformer");
GlobalVariables.on = new ObjectName("transformer:service=DemoTransformer");
// Here we're connecting to the target JVM through the management agent
GlobalVariables.connector = JMXConnectorFactory.connect(new JMXServiceURL(GlobalVariables.connectorAddress));
GlobalVariables.server = GlobalVariables.connector.getMBeanServerConnection();
System.out.println("MBean Server connection...");
// Call transformClass on the transformer MBean
GlobalVariables.server.invoke(GlobalVariables.on, "transformClass", new Object[]{className, args}, new String[]{String.class.getName(), String.class.getName()});
} catch (Exception ex) {
ex.printStackTrace(System.err);
} finally {
if(GlobalVariables.connector!=null) try { GlobalVariables.connector.close(); } catch (Exception e) {}
GlobalVariables.vm.detach();
System.out.println("Has disconnected");
}
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import com.sun.tools.attach.VirtualMachine;
public class GlobalVariables{
public static MBeanServerConnection server;
public static ObjectName on;
public static JMXConnector connector = null;
public static String connectorAddress = null;
public static VirtualMachine vm;
}
当前虚拟机中出现错误
目标vm中出现错误
目标
做同样的工作没有错误。我想我每次完成工作后都得把探员解雇。但我没能甩掉那些特工
编辑全球变量
GlobalVariables.vm = VirtualMachine.attach(vid);
// Check to see if transformer agent is installed
if(!GlobalVariables.vm.getSystemProperties().contains("demo.agent.installed")) {
System.out.println("Load agent");
GlobalVariables.vm.loadAgent(agentPath);
}
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
if(null == GlobalVariables.connectorAddress) {
// It's not, so install the management agent
String javaHome = GlobalVariables.vm.getSystemProperties().getProperty("java.home");
File managementAgentJarFile = new File(javaHome + File.separator + "lib" + File.separator + "management-agent.jar");
GlobalVariables.vm.loadAgent(managementAgentJarFile.getAbsolutePath());
System.out.println("Load Management agent");
GlobalVariables.connectorAddress = GlobalVariables.vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
// Now it's installed
}
// Now connect and transform the classnames provided in the remaining args.
//JMXConnector connector = null;
try {
// This is the ObjectName of the MBean registered when loaded.jar was installed.
//ObjectName on = new ObjectName("transformer:service=DemoTransformer");
GlobalVariables.on = new ObjectName("transformer:service=DemoTransformer");
// Here we're connecting to the target JVM through the management agent
GlobalVariables.connector = JMXConnectorFactory.connect(new JMXServiceURL(GlobalVariables.connectorAddress));
GlobalVariables.server = GlobalVariables.connector.getMBeanServerConnection();
System.out.println("MBean Server connection...");
// Call transformClass on the transformer MBean
GlobalVariables.server.invoke(GlobalVariables.on, "transformClass", new Object[]{className, args}, new String[]{String.class.getName(), String.class.getName()});
} catch (Exception ex) {
ex.printStackTrace(System.err);
} finally {
if(GlobalVariables.connector!=null) try { GlobalVariables.connector.close(); } catch (Exception e) {}
GlobalVariables.vm.detach();
System.out.println("Has disconnected");
}
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import com.sun.tools.attach.VirtualMachine;
public class GlobalVariables{
public static MBeanServerConnection server;
public static ObjectName on;
public static JMXConnector connector = null;
public static String connectorAddress = null;
public static VirtualMachine vm;
}
你说卸下代理是什么意思?您不能卸载文档中隐含的类:
指定的JAR文件将添加到
目标虚拟机)
解决这个问题的唯一方法可能是一些自定义类装入器魔法,但我不建议这样做
更新:在看了你的扩展问题后,我认为你的问题实际上是其他问题。在某个时刻,您正在调用
JMXConnectorFactory.connect(new JMXServiceURL(GlobalVariables.connectorAddress))
为了创建一个。我猜在您的GlobalVariables.server.invoke
调用中,您正在通过newobjectname(“transformer:service=DemoTransformer”)
注册MBean。此名称必须是唯一的,并且当您第二次运行代码时,此名称已按照javax.management.InstancealReadyExistException:transformer:service=DemoTransformer
的建议使用。您需要做的是:
- 或者在第二次注册MBean时选择另一个名称
- 在从远程JVM分离之前,调用
,以使名称再次可用MBeanServerConnection.close(新对象名(“transformer:service=DemoTransformer”)
JMXConnector.close
附言:你可能会觉得有趣
更新2:在聊天中进行讨论并排除MBean命名冲突后,我认为这就是问题的原因:
第二次加载Java代理时,代理附带的类(在managementAgentJarFile
中表示)已加载到目标JVM中。这意味着,不会再次运行任何类初始值设定项,并且仍然会表示由静态
变量表示的状态更改。此外,不可能加载具有相同名称但更改了实现的类。这将导致LinkageError
s,并且代理加载将失败。解决方案是避免静态
状态,使代理无法对自身造成影响,并为不同的代理创建单独的名称空间。否则,可以使用自定义类装入器卸载代理类。有关这一问题的更多信息,可在许多地方和此处找到:
移除仪器
意味着卸载代理
?我已经在raphw编辑了我的问题。你能帮我提些建议吗?感谢GlobalVariables
虚拟机的超级类型是什么?这甚至不应该编译。一个包含一些静态变量的类。我已将代码添加到问题描述中@我想我知道你的问题是什么了。您注册了两个同名的MBean。请看我上面编辑的答案。我想你代码的第一行漏掉了一个点GlobalVariables.vm=VirtualMachine.attach(vid)代码>在此处复制我的代码时,它将丢失。这是我计划的一部分。