我正在尝试使用Java进行OSB(ESB)自动构建过程。为此,我需要做以下事情:
我使用以下代码制作ALSBConfigurationMBean对象:
DomainRuntimeServiceMBean domainService = ConnectionUtil
.createDomainService(mHost, mPort, mUsername, mPassword,
connector);
ALSBConfigurationMBean alsbSession = (ALSBConfigurationMBean) domainService
.findService(ALSBConfigurationMBean.NAME,
ALSBConfigurationMBean.TYPE, null);
我得到的错误是:
Exception in thread "main" java.lang.ClassCastException: $Proxy1 cannot be cast to weblogic.management.mbeanservers.Service
at $Proxy0.findService(Unknown Source)
答案 0 :(得分:4)
我正在做类似的事情,@ Rich给我的解决方案。但是,我最终采用了稍微不同的方法,不需要使用weblogic.management.jmx.MBeanServerInvocationHandler(至少直接)。
以下是一些代码,可以满足您的需求:
package scratch;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import com.bea.wli.sb.management.configuration.ALSBConfigurationMBean;
import com.bea.wli.sb.management.configuration.SessionManagementMBean;
public class TestOSB_JMX {
public static void main(String[] args) throws Throwable {
// Provide connection information for OSB WebLogic AdminServer on command line
String host = args[0];
int port = Integer.parseInt(args[1]);
String user = args[2];
String password = args[3];
JMXConnector conn = initJmxConnection(host, port, user, password);
try {
System.out.println("Opened JMX connection to " + host + ":" + port + " as " + user);
// get mbean connection
MBeanServerConnection mbconn = conn.getMBeanServerConnection();
// Get SessionmanagementMBean
SessionManagementMBean sm =
JMX.newMBeanProxy(mbconn, ObjectName.getInstance(SessionManagementMBean.OBJECT_NAME), SessionManagementMBean.class);
// Create a session
String sessionName = "MySession";
sm.createSession(sessionName);
// Get the configuration MBean for the session, do stuff, and then discard the session.
try
{
System.out.println("Session exists? : " + sm.sessionExists(sessionName));
ALSBConfigurationMBean configMBean =
JMX.newMBeanProxy(
mbconn,
ObjectName.getInstance("com.bea:Name=" +ALSBConfigurationMBean.NAME + "." + sessionName +",Type=" + ALSBConfigurationMBean.TYPE),
ALSBConfigurationMBean.class
);
System.out.println("Got the config MBean for session: " + configMBean.getSession());
// Do stuff in the session here....
}
finally
{
// use activateSession to commit session changes instead
sm.discardSession(sessionName);
}
} finally {
conn.close();
System.out.println("Closed JMX connection");
}
}
public static JMXConnector initJmxConnection(String hostname, int port,String username, String password)
throws IOException, MalformedURLException
{
JMXServiceURL serviceURL = new JMXServiceURL("t3", hostname, port, "/jndi/" + DomainRuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
Hashtable<String, String> h = new Hashtable<String, String>();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
return JMXConnectorFactory.connect(serviceURL, h);
}
}
以下是我的解决方案的一些注意事项:
答案 1 :(得分:1)
如果有人得到相同的错误,请使用 weblogic.management.jmx.MBeanServerInvocationHandler而不是javax.management.MBeanServerInvocationHandler:
https://forums.oracle.com/forums/thread.jspa?threadID=817973&tstart=420