0
我正在開發一個java web應用程序,它將從weblogic服務器中提取一些信息(cpu使用情況,java堆大小等)並將其顯示在網頁上。現在服務器即時連接到本地,但我會在以後擴展。我的問題是IM連接似乎訪問服務器的配置端,我不能得到我想要的數據。我對java和weblogic很新。這裏是我的代碼(你會看到一些冗餘,我在測試不同的技術了。)通過webapp連接到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 Server拔出某些屬性和設置變量等於它們的值,所以我可以稍後使用它們。並再次即時非常新,所以我可能會完全關閉。
我會看看。我被推入了一個需要知道如何使用它的位置,所以任何事情都會有所幫助。 – rebullet00 2014-09-29 15:52:18
我也應該得到我檢查的服務器上的CPU使用率,但被告知我可能無法通過這種方法做到這一點。我被告知,獲得這種信息的好方法是通過unix。你還有更多的教程可以閱讀嗎? – rebullet00 2014-09-29 15:58:07