2014-08-27 78 views
0

在我的weblogic服務器中部署了ehcache,我需要通過java編程從此程序中獲取ehcahe mbeans,通過JMX我無法連接。我可以獲取這些自定義mbeans嗎?如何在weblogic服務器上訪問ehcache mbeans

我試圖打通的WebLogic T3協議的MBean

public class Test 
{ 

    private String hostName = ""; 
    private String port = ""; 
    private String userName = ""; 
    private String password = ""; 
    private String connectorURL = "service:jmx:rmi:///jndi/rmi://{0}:{1}/jmxrmi"; 
    private JMXConnector jmxc = null; 
    public static void main(String []args) throws Exception 
    { 
     Test t = new Test(); 
     t.hostName = args[0]; 
     System.out.println(args[1]); 
     t.port = args[1]; 
     t.userName = args[2]; 
     t.password = args[3]; 
     t.jmxc = t.initConnection(); 




     MBeanServerConnection mbsc = t.jmxc.getMBeanServerConnection(); 
     System.out.println(mbsc); 
     Set<ObjectInstance> st =mbsc.queryMBeans(new ObjectName("net.*:*"), null); 
     System.out.println(st.toString()); 
     Iterator<ObjectInstance> it = st.iterator(); 

     while(it.hasNext()) 
     { 
      System.out.println(it.next()); 
     } 


     t.closeConnection(); 
    } 


    private JMXConnector initConnection() 
    { 
     System.out.println("initiate connection"); 


     JMXServiceURL serviceURL = null; 

     try 
     { 
      String jndiroot = "/jndi/"; 
      String mserver = "weblogic.management.mbeanservers.domainruntime"; 

      int port1 = Integer.parseInt(port); 
      serviceURL = new JMXServiceURL("t3", hostName, port1, jndiroot + mserver); 

      Hashtable h = new Hashtable(); 
      h.put(Context.SECURITY_PRINCIPAL, userName); 
      h.put(Context.SECURITY_CREDENTIALS, password); 
      h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); 

      long lngJmxClientWTO = 10000; 

      h.put("jmx.remote.x.request.waiting.timeout", lngJmxClientWTO); 



      return JMXConnectorFactory.connect(serviceURL, h); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      return null; 
     } 

    } 


    /** 
    * This method closes client connection with server 
    * @throws IOException 
    */ 
    public void closeConnection() 
    { 

     if(jmxc != null) 
     { 
      try 
      { 
       jmxc.close(); 
      } 
      catch (IOException e) { 
       jmxc = null; 
      } 
     } 
    } 
+0

Ehcache mbeans是什麼意思? – 2014-08-27 09:45:03

+0

在我們的weblogic服務器中,ehcache被安裝爲二級緩存提供程序服務器,此ehcache包含以下mbean net.sf.ehcache:它提供有關緩存數量條目的統計信息..我需要通過java編程獲取這些統計信息。 – user2572969 2014-08-27 09:46:36

回答

2
import net.sf.ehcache.CacheManager; 
import net.sf.ehcache.Ehcache; 

CacheManager manager = CacheManager.newInstance(); 
Ehcache cache = manager.getEhcache("Some cache name here..."); //<-- PLEASE EDIT THE CACHE NAME... 

我不知道這是不是你問什麼....

一旦你獲得你的cache,你可以使用它,非常像一個java Map


您可以按照Ehcache documentation瞭解如何以編程方式獲取遠程緩存。實質上,您需要創建一個configuration (or configuration file),其中CacheManager可以訪問。

+0

我需要遠程連接到ehcache服務器...... – user2572969 2014-08-27 10:03:22

+0

配置您的'ehcache.xml'以遠程指向您的'Terracotta Big Memory Server'。 – 2014-08-27 10:04:31

+0

通過Java編程我需要遠程獲取ehcache mbeans – user2572969 2014-08-27 10:26:28