我正在研究类似的东西,@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);
}
}
以下是关于我的解决方案的一些注意事项:
- 我的类路径直接引用以下 WebLogic 和 OSB JAR 文件:alsb.jar、osb-server-modules-ref.jar、wlfullclient.jar
- 这些 JAR 将通过清单文件中的 Class-Path 条目引用(许多)其他 JAR。
- 您可能需要按照此处所述生成 wlfullclient.jar 文件:http: //docs.oracle.com/cd/E17904_01/web.1111/e13717/jarbuilder.htm
- 对于这种类型的客户端应用程序,最好使用 wlfullclient.jar 而不是 weblogic.jar。我首先使用的是 weblogic.jar,它给我带来了很多类加载问题。
- 我正在使用 Oracle Service Bus 版本:11.1.1.4 WebLogic 版本:10.3.4 和 Java 1.6.0_29。