【发布时间】:2012-06-26 09:55:34
【问题描述】:
我在使用 MBean 作为参数时遇到问题。如果我尝试使用代理对象通过 JMX 执行它,我会得到一个异常:
Caused by: javax.management.ReflectionException
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:231)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
Caused by: java.lang.IllegalArgumentException: Unable to find operation updateProperties(java.util.HashMap)
它似乎试图使用实际的实现类而不是接口,并且不检查这是否是所需接口的子接口。扩展类也会发生同样的事情(例如声明HashMap,传入LinkedHashMap)。这是否意味着不可能为这些方法使用接口?目前我正在通过更改方法签名以接受HashMap 来解决它,但我无法在MBeans 中使用接口(或扩展类)似乎很奇怪。
编辑:代理对象由名为JmxInvocationHandler 的内部实用程序类创建。它的(希望)相关部分如下:
public class JmxInvocationHandler implements InvocationHandler
{
...
public static <T> T createMBean(final Class<T> iface, SFSTestProperties properties, String mbean, int shHostID)
{
T newProxyInstance = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[] { iface }, (InvocationHandler) new JmxInvocationHandler(properties, mbean, shHostID));
return newProxyInstance;
}
...
private JmxInvocationHandler(SFSTestProperties properties, String mbean, int shHostID)
{
this.mbeanName = mbean + MBEAN_SUFFIX + shHostID;
msConfig = new MsConfiguration(properties.getHost(0), properties.getMSAdminPort(), properties.getMSUser(), properties.getMSPassword());
}
...
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
if (management == null)
{
management = ManagementClientStore.getInstance().getManagementClient(msConfig.getHost(),
msConfig.getAdminPort(), msConfig.getUser(), msConfig.getPassword(), false);
}
final Object result = management.methodCall(mbeanName, method.getName(), args == null? new Object[] {} : args);
return result;
}
}
【问题讨论】:
-
你好雷神;你能提供创建你的代理对象的代码吗?
-
我正在使用内部实用程序类,坦率地说,我不确定我是否真的能弄清楚其中发生了什么(它正在做大量与安全相关的额外事情,而不是我见过的最好的书面代码),但我会尝试提取重要的部分并将它们添加到帖子中。