随笔 - 127  文章 - 0  评论 - 0  阅读 - 74293

java连接jmx的mp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.test;
 
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.security.Security;
import java.util.HashMap;
 
/**
 * @author zhouyi
 * @description
 * @date 2021/8/26 9:08
 */
public class JmxClientTest {
    final static String RMI_URL = "service:jmx:jmxmp://127.0.0.1:7200";
 
 
    /**
     * 需要引入jar:
     * 在java环境中jre/lib/ext中加入jar: jmxremote_optional.jar
     * 在测试用例项目中引入jar: bean-validator-4.0.0.Alpha3.jar
     */
    public static void main(String[] args) throws Exception {
        HashMap<String, Object> env = new HashMap<>();
        Security.addProvider(new com.sun.jdmk.security.sasl.Provider());
        env.put("jmx.remote.profiles", "SASL/PLAIN");
        env.put("jmx.remote.sasl.callback.handler",new UserPasswordCallBackHandler("thanos","thanos123.com"));
        JMXConnector jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(RMI_URL), env);
        MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
        System.out.println("MBean count = " + mBeanServer.getMBeanCount());
        String[] domains = mBeanServer.getDomains();
        for (String doMain : domains) {
            System.out.println("JmxDomain:" + doMain);
        }
        System.out.println("====== Catalina Jmx =====");
    }
 
}

  




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.test;
 
import java.io.IOException;
 
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
 
/**
 * @author zhouyi
 * @description
 * @date 2021/8/25 17:00
 */
public class UserPasswordCallBackHandler implements CallbackHandler{
    /**
     * The username to be provided when prompted.
     */
    private String username;
 
    /**
     * The password to be provided when prompted.
     */
    private String password;
 
    /**
     * Create a new NamePasswordCallbackHandler (required Cams default
     * constructor that is dynamically called by the authentication server).
     */
    public UserPasswordCallBackHandler() {
        this.username = null;
        this.password = null;
    }
 
    /**
     * Create a new NamePasswordCallbackHandler (optional constructor used to
     * facilitate testing).
     *
     * @param username
     *            the username to provide when prompted.
     * @param password
     *            the password to provide when prompted.
     */
    public UserPasswordCallBackHandler(String username, String password) {
        this.username = username;
        this.password = password;
    }
 
    /**
     * Set the username.
     *
     * @param username
     *            the username to be provided to a NameCallback.
     */
    public void setUsername(String username) {
        if (username == null) {
            this.username = "";
            return;
        }
 
        this.username = username;
    }
 
    /**
     * Set the password.
     *
     * @param password
     *            the password to be provided to a PasswordCallback.
     */
    public void setPassword(String password) {
        if (password == null) {
            this.password = "";
            return;
        }
 
        this.password = password;
    }
 
    /**
     * Retrieve or display the information requested in the provided Callbacks.
     * The handle method implementation checks the instance(s) of the Callback
     * object(s) passed in to retrieve or display the requested information.
     *
     * @param callbacks
     *            an array of Callback objects provided by an underlying
     *            security service which contains the information requested to
     *            be retrieved or displayed.
     *
     * @exception IOException
     *                if an input or output error ocurrs
     * @exception UnsupportedCallbackException
     *                if the implementation of this method does not support one
     *                or more of the Callbacks specified in the callbacks
     *                parameter
     */
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        // Loop over all Callbacks
        for (int i = 0; i < callbacks.length; i++) {
            Callback cb = callbacks[i];
 
            if (cb instanceof NameCallback) {
                ((NameCallback) cb).setName(username);
            } else if (cb instanceof PasswordCallback) {
                // JAAS specifies that the password is a char[]
                ((PasswordCallback) cb).setPassword(password.toCharArray());
            } else {
                throw new UnsupportedCallbackException(cb, "Unrecognized Callback");
            }
        }
    }
}

  


posted on   groby  阅读(165)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示