Jboss EAP:native management API学习

上一节已经学习了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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏IT可乐

mybatis源码解读(二)——构建Configuration对象

1962
来自专栏清晨我上码

异步任务执行的设计模式

2403
来自专栏大内老A

采用一个自创的"验证框架"实现对数据实体的验证[编程篇]

昨天晚上突发奇想,弄了一个简易版的验证框架,用于进行数据实体的验证。目前仅仅实现基于属性的声明式的验证,即通过自定义特性(Custom Attribute)的方...

2606
来自专栏码匠的流水账

聊聊sentinel的DegradeSlot

com/alibaba/csp/sentinel/slots/block/degrade/DegradeSlot.java

1371
来自专栏我的小碗汤

golang 设置 http response 响应头与坑

之前遇到个问题,在一段代码中这样设置WriteHeader,最后在header中取Name时怎么也取不到。

862
来自专栏数据结构与算法

BZOJ1269: [AHOI2006]文本编辑器editor

Descriptio 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗? 为了明确任务目标,可可对“文本编辑器...

2857
来自专栏老码农专栏

原 荐 OSGL 工具库 - Java 字串

1954
来自专栏码匠的流水账

聊聊sentinel的SimpleHttpCommandCenter

sentinel-transport-simple-http-0.1.1-sources.jar!/com/alibaba/csp/sentinel/trans...

841
来自专栏我杨某人的青春满是悔恨

封装一个 Swift-Style 的网络模块

Swift 跟 OC 有着完全不同的设计哲学,它鼓励你使用 protocol 而不是 super class,使用 enum 和 struct 而不是 clas...

913
来自专栏Spark生态圈

[spark] DAGScheduler 提交stage源码解析

DAGScheduler在划分完Stage后([spark] DAGScheduler划分stage源码解析 ),将会通过submitStage(finalSt...

1023

扫码关注云+社区

领取腾讯云代金券