通过webapp连接到weblogic服务器并提取服务器属性

时间:2014-09-25 19:03:13

标签: java web-applications weblogic jndi

我正在开发一个java web应用程序,它将从weblogic服务器中提取一些信息(cpu使用,java堆大小等)并将其显示在网页上。现在连接到的服务器是本地的,但我稍后会扩展。我的问题是即时连接似乎访问服务器的配置端的方式,我无法获得我想要的数据。我是java和weblogic的新手。这是我的代码(你会看到一些冗余,我正在测试不同的技术。)

package Servlets;

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.naming.Context;
import weblogic.jndi.*;
import weblogic.management.configuration.ServerMBean;

/**
 * Servlet implementation class getStatus
 */
public class getStatus extends HttpServlet {
private static final long serialVersionUID = 1L;
ServerMBean beanOne = null;
String beanTwo = null;
Context ctx = null;
InitialContext ictx = null;
/**
 * @see HttpServlet#HttpServlet()
 */
public getStatus() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see Servlet#init(ServletConfig)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void init(ServletConfig config) throws ServletException {


      Hashtable ht = new Hashtable();
      ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
      ht.put(Context.SECURITY_PRINCIPAL, "weblogic");
      ht.put(Context.SECURITY_CREDENTIALS, "welcome1");

      try {
        ctx = new InitialContext(ht);
        try { 
               beanOne = (ServerMBean) ctx.lookup("ServerMBean");
               beanOne.getName();


            }catch (NameNotFoundException e) {
              // binding does not exist
                System.out.println("bean not found :(");
            }catch (NamingException e) {
              // a failure occurred
                System.out.println("bean not found D:");
            }
        // Use the context in your program
      }
      catch (NamingException e) {
          System.out.println("no context :(");
        // a failure occurred
      }
      finally {
        try {(ctx).close();}
        catch (Exception e) {
          // a failure occurred
            System.out.println("no context D:");
        }
      }
      weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
      environment.setInitialContextFactory(weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
      environment.setProviderUrl("t3://localhost:7001");
      environment.setSecurityPrincipal("weblogic");
      environment.setSecurityCredentials("welcome1");

      try{
      ictx = (InitialContext) environment.getInitialContext();
      }
      catch(Exception e){
          System.out.println("no initial context :(");
      }

      try { 
           beanTwo = (String) ictx.lookup("ejb.serviceBean");
        }catch (NameNotFoundException e) {
          // binding does not exist
            System.out.println("bean not found :(");
        }catch (NamingException e) {
          // a failure occurred
            System.out.println("bean not found D:");
        }
      try {
            ctx.close();
        } catch (Exception e) {
           //a failure occurred
        }
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

我需要一种连接到weblogic服务器的方法来拉出某些属性并设置等于它们的值的变量,以便我以后可以使用它们。再次对我非常新,所以我可能会完全离开。

1 个答案:

答案 0 :(得分:0)

您尝试做的事情看起来像基本的JMX bean轮询,您是否在线阅读了教程?如果您搜索" JMX教程"有很多,从official和其他许多其他内容开始。在你最喜欢的搜索引擎中。