如何在weblogic服务器上访问ehcache mbeans

时间:2014-08-27 09:30:18

标签: java weblogic jmx ehcache

在我的weblogic服务器中部署了ehcache,我需要通过java编程从这个程序中获取ehcahe mbeans,通过JMX我无法连接。我可以获得那些自定义mbeans吗?

我试图通过weblogic t3协议获得mbeans

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;
           }
       }
   }

1 个答案:

答案 0 :(得分: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查看如何以编程方式获取远程缓存。从本质上讲,您需要创建CacheManager可以访问的configuration (or configuration file)