Weblogic ALSBConfigurationMBean初始化

时间:2011-09-30 08:38:45

标签: weblogic jmx osb

我正在尝试使用Java进行OSB(ESB)自动构建过程。为此,我需要做以下事情:

  1. 连接到weblogic - 这是有效的// weblogic版本是10.3.0.0
  2. 使用连接创建一个DomainRuntimeServiceMBean对象 - 这也正常工作
  3. 使用以前创建的DomainRuntimeServiceMBean对象创建一个ALSBConfigurationMBean对象 - 这是我的问题。
  4. 我使用以下代码制作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)
    

2 个答案:

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

答案 1 :(得分:1)

如果有人得到相同的错误,请使用 weblogic.management.jmx.MBeanServerInvocationHandler而不是javax.management.MBeanServerInvocationHandler:

https://forums.oracle.com/forums/thread.jspa?threadID=817973&tstart=420