在DataSource MBean上调用方法时出错

时间:2012-08-06 19:52:51

标签: connection datasource jmx invoke mbeans

我确信我会以完全错误的方式解决这个问题,但有人可以指出下面代码中的错误......

MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer (null).get (0);
ObjectName mBean = new ObjectName ("Catalina:type=DataSource,path=/<context>,host=localhost,class=javax.sql.DataSource,name=\"<name>\"");
String [] params = {"<username>", "<password>"};
Connection myConnection = (Connection) server.invoke (mBean, "getConnection", params, null);
Statement myStatement = myConnection.createStatement ();
String myResult = myStatement.executeQuery ("SELECT 1 FROM DUAL;").toString ();
myConnection.close ();

当我尝试通过在MBean上调用getConnection方法来实例化Connection对象时,会出现问题。我收到以下错误...

Aug 6, 2012 8:46:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: Inconsistent arguments and signature
        at org.apache.tomcat.util.modeler.ManagedBean.getInvoke(ManagedBean.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我看到你在做:

Connection myConnection = (Connection) server.invoke (mBean, "getConnection",
     params, null);

您正在传递null以获取我认为不允许的param签名数组。引用javadocs from MbeanServer.invoke(...)

  

@param signature包含操作签名的数组。使用与用于加载调用操作的MBean相同的类加载器来加载类对象。

此数组应包含您尝试调用的方法参数的类名,并且它们必须精确匹配。原始类型应作为字符串"int""long",...传递,而类类型为"java.util.Date""java.lang.String",...

所以我认为你需要传递类似的内容:

String [] params = {"<username>", "<password>"};
String [] signatures = {"java.lang.String", "java.lang.String"};
Connection myConnection = (Connection) server.invoke (mBean, "getConnection",
     params, signatures);