Reputation: 2824
I'm sure I'm going about this in completely the wrong way, but can someone point out the error in the code below...
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 ();
The problem is occurring when I try to instantiate the Connection object by invoking the getConnection method on my MBean. I receive the following error...
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)
What am I doing wrong?
Upvotes: 0
Views: 2917
Reputation: 116888
I see you are doing:
Connection myConnection = (Connection) server.invoke (mBean, "getConnection",
params, null);
You are passing in null
for the param signature array which I don't think is allowed. To quote the javadocs from MbeanServer.invoke(...)
:
@param signature An array containing the signature of the operation. The class objects will be loaded using the same class loader as the one used for loading the MBean on which the operation was invoked.
This array should hold the class names of the method parameters you are trying to invoke and they must match precisely. Primitive types should be passed in as the string "int"
, "long"
, ... while class types as "java.util.Date"
, "java.lang.String"
, ...
So I think you need to pass in something like:
String [] params = {"<username>", "<password>"};
String [] signatures = {"java.lang.String", "java.lang.String"};
Connection myConnection = (Connection) server.invoke (mBean, "getConnection",
params, signatures);
Upvotes: 2