Jboss EAP:native management API学习

简介: 上一节已经学习了CLI命令行来控制JBOSS,如果想在程序中以编码方式来控制JBOSS,可以参考下面的代码,实际上在前面的文章,用代码控制Jboss上的DataSource,已经有所接触了,API与CLI是完全等价的,一个是人工敲指令,一个是代码控制,二者最终的效果一致。

上一节已经学习了CLI命令行来控制JBOSS,如果想在程序中以编码方式来控制JBOSS,可以参考下面的代码,实际上在前面的文章,用代码控制Jboss上的DataSource,已经有所接触了,API与CLI是完全等价的,一个是人工敲指令,一个是代码控制,二者最终的效果一致。

import com.sun.javafx.sg.PGShape;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.dmr.ModelNode;
import org.junit.Test;

import javax.security.auth.callback.*;
import javax.security.sasl.RealmCallback;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class JBossClient {

    private String host = "172.16.38.***";
    private int port = 9999;
    private String userid = "jimmy";
    private String password = "*****";

    @Test
    public void testGetServers() {
        //相当于CLI命令行: ls /host=master/server-config
        List<String> servers = getServers("master");
        for (String s : servers) {
            System.out.println(s);
        }
    }

    @Test
    public void getServerStatus() {
        //相当于CLI命令行:/host=master/server=server-one:read-attribute(name=server-state)
        System.out.println(getServerStatus("master", "server-one"));
        //相当于CLI命令行:/host=master/server-config=server-one:read-attribute(name=status)
        System.out.println(getServerStatus2("master", "server-one"));
    }

    @Test
    public void testStartServer() {
        //相当于CLI命令行:/host=master/server-config=server-one:start
        System.out.println(startServer("master", "server-one"));
    }

    @Test
    public void testStopServer() {
        //相当于CLI命令行:/host=master/server-config=server-one:stop
        System.out.println(stopServer("master", "server-one"));
    }

    /**
     * 获取指定服务器运行状态
     * @param hostName
     * @param serverName
     * @return
     */
    public String getServerStatus(String hostName, String serverName) {
        String status = "unknown";
        ModelControllerClient client = null;
        try {
            client = createClient(InetAddress.getByName(host), port, userid, password.toCharArray(), "ManagementRealm");
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.out.println("UHE: " + uhe.getMessage());
        }
        try {
            ModelNode op = new ModelNode();
            op.get(ClientConstants.OP).set(ClientConstants.READ_ATTRIBUTE_OPERATION);
            op.get(ClientConstants.OP_ADDR).add("host", hostName);
            op.get(ClientConstants.OP_ADDR).add("server", serverName);
            op.get("name").set("server-state");

            status = client.execute(op).get(ClientConstants.RESULT).asString();

            if (client != null) client.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
        }

        return status;
    }


    /**
     * 另一种获取服务器运行状态的方法
     * @param hostName
     * @param serverName
     * @return
     */
    public String getServerStatus2(String hostName, String serverName) {
        String status = "unknown";
        ModelControllerClient client = null;
        try {
            client = createClient(InetAddress.getByName(host), port, userid, password.toCharArray(), "ManagementRealm");
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.out.println("UHE: " + uhe.getMessage());
        }
        try {
            ModelNode op = new ModelNode();
            op.get(ClientConstants.OP).set(ClientConstants.READ_ATTRIBUTE_OPERATION);
            op.get(ClientConstants.OP_ADDR).add("host", hostName);
            op.get(ClientConstants.OP_ADDR).add("server-config", serverName);
            op.get("name").set("status");

            status = client.execute(op).get(ClientConstants.RESULT).asString();

            if (client != null) client.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
        }

        return status;
    }

    /**
     * 启动指定服务器
     *
     * @param hostName
     * @param serverName
     */
    public ModelNode startServer(String hostName, String serverName) {

        ModelControllerClient client = null;
        ModelNode returnVal = null;
        try {
            client = createClient(InetAddress.getByName(host), port, userid, password.toCharArray(), "ManagementRealm");
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.out.println("UHE: " + uhe.getMessage());
        }
        try {
            ModelNode op = new ModelNode();
            op.get(ClientConstants.OP).set("start");
            op.get(ClientConstants.OP_ADDR).add("host", hostName);
            op.get(ClientConstants.OP_ADDR).add("server-config", serverName);

            returnVal = client.execute(op).get(ClientConstants.RESULT);

            if (client != null) client.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
        }

        return returnVal;

    }


    /**
     * 停止指定服务器
     *
     * @param hostName
     * @param serverName
     */
    public ModelNode stopServer(String hostName, String serverName) {

        ModelControllerClient client = null;
        ModelNode returnVal = null;
        try {
            client = createClient(InetAddress.getByName(host), port, userid, password.toCharArray(), "ManagementRealm");
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.out.println("UHE: " + uhe.getMessage());
        }
        try {
            ModelNode op = new ModelNode();
            op.get(ClientConstants.OP).set("stop");
            op.get(ClientConstants.OP_ADDR).add("host", hostName);
            op.get(ClientConstants.OP_ADDR).add("server-config", serverName);
            returnVal = client.execute(op).get(ClientConstants.RESULT);
            if (client != null) client.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
        }

        return returnVal;

    }


    /**
     * 获取指定host下的所有server
     *
     * @param hostName
     * @return
     */
    public List<String> getServers(String hostName) {
        List<String> servers = new ArrayList<String>();
        ModelControllerClient client = null;
        try {
            client = createClient(InetAddress.getByName(host), 9999, userid, password.toCharArray(), "ManagementRealm");
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.out.println("UHE: " + uhe.getMessage());
        }
        try {
            ModelNode op = new ModelNode();
            op.get(ClientConstants.OP).set(ClientConstants.READ_RESOURCE_OPERATION);
            op.get(ClientConstants.OP_ADDR).add("host", hostName);
            List<ModelNode> returnVal = client.execute(op).get(ClientConstants.RESULT).get("server-config").asList();

            for (ModelNode _ : returnVal) {
                servers.add(_.asProperty().getName());
            }

            if (client != null) client.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
        }
        return servers;
    }


    private ModelControllerClient createClient(final InetAddress host, final int port, final String username, final char[] password, final String securityRealmName) {
        final CallbackHandler callbackHandler = new CallbackHandler() {
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (Callback current : callbacks) {
                    if (current instanceof NameCallback) {
                        NameCallback ncb = (NameCallback) current;
                        ncb.setName(username);
                    } else if (current instanceof PasswordCallback) {
                        PasswordCallback pcb = (PasswordCallback) current;
                        //pcb.setPassword("admin123".toCharArray());
                        pcb.setPassword(password);
                    } else if (current instanceof RealmCallback) {
                        RealmCallback rcb = (RealmCallback) current;
                        rcb.setText(rcb.getDefaultText());
                    } else {
                        throw new UnsupportedCallbackException(current);
                    }
                }
            }
        };
        return ModelControllerClient.Factory.create(host, port, callbackHandler);
    }
}

除了native managent API外,jboss还提供了一套基于http的REST风格API,即9990端口对应的API,有兴趣的可以参考下面的文章

https://docs.jboss.org/author/display/AS71/The+HTTP+management+API

https://docs.jboss.org/author/display/AS71/The+native+management+API

GitHub有一个开源项目,从手机上管理jboss,就是基于http的这一套API实现的,技术上讲 ,利用这二套API,完全可以自己定制一套Jboss管理控制台(不管是c/s还是b/s)

 

最后送点福利,GitHub上的开源项目jboss-controller-operation-executor,我在原来的基础上,增加了几个domain模式下的控制方法,包括 停止/启用某一台服务器、获取服务器状态、停止/启用某个ServerGroup下所有Server,并增加了单元测试的示例代码,并将pom依赖项,升级到7.5,以兼容JBOSS EAP 6.4

项目地址:https://github.com/yjmyzz/jboss-controller-operation-executor

示例代码:https://github.com/yjmyzz/jboss-controller-operation-executor/blob/master/src/test/java/uk/co/techblue/jboss/test/UnitTest.java

 

目录
相关文章
|
JavaScript
Vue基础学习——Vue3的Options-API
Vue基础学习——Vue3的Options-API
298 0
|
存储 缓存 网络协议
dpdk课程学习之练习笔记二(arp, udp协议api测试)
dpdk课程学习之练习笔记二(arp, udp协议api测试)
380 0
|
11月前
|
API 数据安全/隐私保护 UED
探索鸿蒙的蓝牙A2DP与访问API:从学习到实现的开发之旅
在掌握了鸿蒙系统的开发基础后,我挑战了蓝牙功能的开发。通过Bluetooth A2DP和Access API,实现了蓝牙音频流传输、设备连接和权限管理。具体步骤包括:理解API作用、配置环境与权限、扫描并连接设备、实现音频流控制及动态切换设备。最终,我构建了一个简单的蓝牙音频播放器,具备设备扫描、连接、音频播放与停止、切换输出设备等功能。这次开发让我对蓝牙技术有了更深的理解,也为未来的复杂项目打下了坚实的基础。
454 58
探索鸿蒙的蓝牙A2DP与访问API:从学习到实现的开发之旅
|
11月前
|
人工智能 数据可视化 API
自学记录鸿蒙API 13:Calendar Kit日历功能从学习到实践
本文介绍了使用HarmonyOS的Calendar Kit开发日程管理应用的过程。通过API 13版本,不仅实现了创建、查询、更新和删除日程等基础功能,还深入探索了权限请求、日历配置、事件添加及查询筛选等功能。实战项目中,开发了一个智能日程管理工具,具备可视化管理、模糊查询和智能提醒等特性。最终,作者总结了模块化开发的优势,并展望了未来加入语音助手和AI推荐功能的计划。
786 1
|
11月前
|
存储 监控 算法
|
Kubernetes 安全 API
Kubernetes学习-集群搭建篇(三) Node配置完善和API概述
Kubernetes学习-集群搭建篇(三) Node配置完善和API概述
Kubernetes学习-集群搭建篇(三) Node配置完善和API概述
|
存储 Java API
【JAVA学习之路 | 提高篇】[内部类与常见API]String类
【JAVA学习之路 | 提高篇】[内部类与常见API]String类
|
存储 安全 机器人
【LLM】智能学生顾问构建技术学习(Lyrz SDK + OpenAI API )
【5月更文挑战第13天】智能学生顾问构建技术学习(Lyrz SDK + OpenAI API )
193 1
|
存储 API Go
学习gin-vue-admin之创建api和swagger
学习gin-vue-admin之创建api和swagger
|
JavaScript 前端开发 Java
下一篇
oss云网关配置