在我的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;
           }
       }
   }

最佳答案

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)

关于java - 如何在Weblogic服务器上访问ehcache mbeans,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25523559/

10-10 07:12