取Jmx连接

本文介绍通过 JMXMP 和 IIOP 协议实现 JMX 连接器的具体方法。针对不同协议,代码展示了如何配置连接环境参数,并通过异常处理确保连接过程的健壮性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class JMXMPProtocolrovider implements IJMXProtocolProvider {


public JMXConnector getJMXConnector(Attribute attibute) throws Exception {
String userName = attibute.getUserName();
String userPwd  = attibute.getPassword();
String host     = attibute.getHost();
int port        = attibute.getPort();

JMXConnector connector = null;
JMXServiceURL url = null;
        try {
        url = new JMXServiceURL("service:jmx:jmxmp://" + host + ":" + port);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "com.sun.jmx.remote.protocol");
        env.put("jmx.remote.profiles", "SASL/PLAIN");
        env.put("jmx.remote.sasl.callback.handler", new SaslUserPasswordCallbackHandler(userName, userPwd));
        env.put(GenericConnector.MESSAGE_CONNECTION, new MuxSocketClientMessageConnection(url.getHost(), url.getPort()));


        // install SASL/PLAIN mechanism provider
        SaslClientSecurityProvider.install();
        if(url != null) {
        return JMXConnectorFactory.connect(url, env);
        }
        return null;
}
}

public class IIOPProtocolProvdier implements IJMXProtocolProvider {


    private static final ObjectName RUNTIME_OBJECTNAME;
    static{
        try{
            RUNTIME_OBJECTNAME = new ObjectName( "java.lang:type=Runtime" );
        }
        catch( final Exception ex ){
            throw new IllegalStateException( ex );
        }
    }


public JMXConnector getJMXConnector(Attribute attibute) throws Exception {
String userName = attibute.getUserName();
String userPwd  = attibute.getPassword();
String host     = attibute.getHost();
int port        = attibute.getPort();


JMXConnector connector = null;
Hashtable<String, Object> env = new Hashtable<String, Object>();
String prividerUrl = "iiop://" + host + ":" + port;
String[] credentials = new String[] { userName, userPwd };
env.clear();
env.put(Context.PROVIDER_URL, prividerUrl);
env.put(JMXConnector.CREDENTIALS, credentials);
env.put(Context.AUTHORITATIVE, userName);
env.put(Context.SECURITY_CREDENTIALS, userPwd);
try {
JMXServiceURL url = new JMXServiceURL("iiop", host, port, "/jndi/corbaname::1.2@" + host + ":" + port
+ "#jmx/rmi/RMIConnectorServer");
connector = JMXConnectorFactory.connect(url, env);
       connector.connect( );
       MBeanServerConnection c = connector.getMBeanServerConnection( );
       c.getMBeanInfo( RUNTIME_OBJECTNAME ).getAttributes( );
} catch (MalformedURLException e) {
throw new OperationNoSuccessException("connection to connector server fail,either no legal protocol could be found in a specification string or the string could not be parsed",e);
} catch (IOException e) {
throw new OperationNoSuccessException("connection to connector server fail", e);
} catch (SecurityException e) {
   throw new OperationNoSuccessException("user [" + userName + "] was not authenticated with this connection", e);
}
return connector;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值