如何在weblogic中获取DomainRuntimeService的实例?

时间:2013-11-07 18:37:29

标签: runtime weblogic jmx

这是我的代码:

domainService = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");

InitialContext ctx = new InitialContext();
mBeanServer = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");

ObjectName[] servers = (ObjectName[]) mBeanServer.getAttribute(domainService, "ServerRuntimes");

但是我收到了这个错误。我在这里做错了什么?

javax.management.InstanceNotFoundException: com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean^M
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)^M
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:662)^M
        at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:638)^M
        at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$12.run(WLSMBeanServerInterceptorBase.java:326)^M
        at java.security.AccessController.doPrivileged(Native Method)^M
        at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.getAttribute(WLSMBeanServerInterceptorBase.java:324)^M
        at weblogic.management.mbeanservers.internal.JMXContextInterceptor.getAttribute(JMXContextInterceptor.java:157)^M
        at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$12.run(WLSMBeanServerInterceptorBase.java:326)^M
        at java.security.AccessController.doPrivileged(Native Method)^M
        at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.getAttribute(WLSMBeanServerInterceptorBase.java:324)^M
        at weblogic.management.mbeanservers.internal.SecurityInterceptor.getAttribute(SecurityInterceptor.java:299)^M
        at weblogic.management.jmx.mbeanserver.WLSMBeanServer.getAttribute(WLSMBeanServer.java:279)^M
        at com.motive.smp.femto.deploy.wl.utils.WLUtilities.getListMServers(WLUtilities.java:105)^M
        at com.motive.smp.femto.deploy.utils.DeploymentUtilsServlet.doGet(DeploymentUtilsServlet.java:58)^M
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)^M
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)^M
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)^

2 个答案:

答案 0 :(得分:1)

试试这个:

Context context = new InitialContext();
MBeanServer domain = (MBeanServer) context.lookup("java:comp/env/jmx/domainRuntime");
DomainRuntimeServiceMBean domainRuntimeService = (DomainRuntimeServiceMBean) MBeanServerInvocationHandler.newProxyInstance(domain, 
    new ObjectName(DomainRuntimeServiceMBean.OBJECT_NAME));

答案 1 :(得分:0)

如果你连接到AdminServer,那么sonnykwe的答案是肯定的。在我的例子中,我连接到域中许多托管服务器之一。

这就是我所做的:

public static Map<String, String> getAdminServerInfo() {
    MBeanServer mBeanServer = null;
    InitialContext ctx = null;
    String administrationURL = null;
    String listenAddress = null;
    String domainName = null;
    String httpProtocol = null;
    Map<String, String> serverInfo = new HashMap<String, String>();
    int adminPort = 0;
    try {
        ctx = new InitialContext();
        mBeanServer = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");

        // Get Admin Server and Port
        String managedServerName = (String) mBeanServer.getAttribute(
                runtimeService, "ServerName");
        ObjectName msServerRuntime = new ObjectName("com.bea:Name="
                + managedServerName + ",Type=ServerRuntime");
        administrationURL = (String) mBeanServer.getAttribute(
                msServerRuntime, "AdminServerHost");
        listenAddress = (String) mBeanServer.getAttribute(msServerRuntime,
                "ListenAddress");
        adminPort = (Integer) mBeanServer.getAttribute(msServerRuntime,
                "AdminServerListenPort");
        Boolean isSecure = (Boolean) mBeanServer.getAttribute(
                msServerRuntime, "AdminServerListenPortSecure");
        httpProtocol = (isSecure) ? "https" : "http";
        ObjectName domainMBean = (ObjectName) mBeanServer.getAttribute(
                runtimeService, "DomainConfiguration");
        domainName = (String) mBeanServer.getAttribute(domainMBean, "Name");
        logger.debug("AdminURL= " + administrationURL + " ,Port= "
                + adminPort + " Domain= " + domainName + " ManagedServer= "
                + managedServerName + " ListenAddress= " + listenAddress
                + " AdminServerListenPortSecure?= " + isSecure);
        serverInfo.put("administrationURL", administrationURL);
        serverInfo.put("managedServerName", managedServerName);
        serverInfo.put("adminPort", Integer.toString(adminPort));
        serverInfo.put("domainName", domainName);
        serverInfo.put("listenAddress", listenAddress.substring(0, listenAddress.indexOf("/")));
        serverInfo.put("httpProtocol", httpProtocol);
    } catch (Exception ex) {
        logger.error(
                "Caught Exception while fetching Admin Server information : ",
                ex);
        throw new RuntimeException(
                "Caught Exception while fetching Admin Server information : ",
                ex);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    return serverInfo;
}