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

Jboss EAP:native management API学习的更多相关文章

  1. 动态配置 JBOSS ( eap 6.2 ) 数据源

    操作环境 windows + jboss eap 6.2 + MyEclipse 10.0 项目用的是jboss eap 6.2,作为Red公司升级后的eap稳定版. 相比之前的 AS 系列,不管是安 ...

  2. node-webkit学习(4)Native UI API 之window

    node-webkit学习(4)Native UI API 之window 文/玄魂 目录 node-webkit学习(4)Native UI API 之window 前言 4.1  window a ...

  3. node-webkit学习(3)Native UI API概览

    node-webkit学习(3)Native UI API概览 文/玄魂 目录 node-webkit学习(3)Native UI API概览 前言 3.1  Native UI api概览 Exte ...

  4. CAS (8) —— Mac下配置CAS到JBoss EAP 6.4(6.x)的Standalone模式(服务端)

    CAS (8) -- Mac下配置CAS到JBoss EAP 6.4(6.x)的Standalone模式(服务端) jboss版本: jboss-eap-6.4-CVE-2015-7501 jdk版本 ...

  5. Oracle Coherence应用部署到Jboss EAP 6.x 时 NoClassDefFoundError: sun/rmi/server/MarshalOutputStream 的解决办法

    今天将一个web应用从weblogic 10.3迁移到jboss EAP 6.3上,该应用使用oracle coherence做为缓存,部署上去后,启动时一直报如下错误:     at java.ut ...

  6. jboss eap 6.3 域(Domain)模式配置

    jboss提供了二种运行模式:standalone(独立运行模式).domain(域模式),日常开发中,使用standalone模式足已:但生产部署时,一个app,往往是部署在jboss集群环境中的, ...

  7. JBOSS EAP实战(1)

    JBOSS的诞生 1998年,在硅谷SUN公司的SAP实验室,一个年轻人正坐在电脑前面思考,然后写着什么东西.不,他没有在写程序,他在写辞呈.他正在做出人生的一个重大决定:他要辞掉在SUN的这份工作, ...

  8. JBOSS EAP 6 系列四 EJB实现——调用(贯穿始终的模块)

    本文主要介绍在JBOSS EAP 6.2(或者JBOSS AS7)中模块是如何贯穿EJB实现的始终.延续上一博文<认识模块的使用>的话题继续聊JBOSS做为模块申明式容器的这一特性在EJB ...

  9. JVM Management API

    JVM本身提供了一组管理的API,通过该API,我们可以获取得到JVM内部主要运行信息,包括内存各代的数据.JVM当前所有线程及其栈相关信 息等等.各种JDK自带的剖析工具,包括jps.jstack. ...

随机推荐

  1. Jexus-5.6.3使用详解、Jexus Web Server配置

    一.Jexus Web Server配置   在 jexus 的工作文件夹中(一般是“/usr/jexus”)有一个基本的配置文件,文件名是“jws.conf”. jws.conf 中至少有 Site ...

  2. WPF学习之路(十)实例:用户注册

    通过一个注册用户的实例了解页面间数据的传递 首先构建一个User类  User.cs public class User { private string name; public string Na ...

  3. JSON 数据使用方法

    当同一个模板需要替换不同的数据显示的时候,如果数据量大点,用json很方便. json对象: var JSONObject= { "name":"Bill Gates&q ...

  4. Git从零教你入门(4):Git服务之 gogs部署安装

    Git从零入门系列4: 先看上一篇文章: http://www.51testing.com/index.php?uid-497177-action-viewspace-itemid-3706817 今 ...

  5. SQL Server(九)——事务

    事务: 保障流程的完整执行,就像银行取钱,先在你账上扣钱,然后存入别人的账上:但是从你账上扣完钱了,突然网断了,对方没有收到钱,那么此时你的钱也没了,别人的钱也没加上,事务为了防止此类情况的出现. 事 ...

  6. SQL Server 2012中Task是如何调度的?

    SQL Server 2012中Task是如何调度的?[原文来自:How It Works: SQL Server 2012 Database Engine Task Scheduling]     ...

  7. mvn archetype:create和mvn archetype:generate

    create is deprecated in maven 3.0.5 and beyond,在maven3.0.5以上版本舍弃了create,使用generate生成项目 before:mvn ar ...

  8. mysql-6 数据检索(4)

    汇总数据 函数 说明 AVG() 返回某列的平均数 COUNT() 返回某列的行数 MAX() 返回某列的最大值 MIN() 返回某列的最小值 SUM() 返回某列值的和 1.AVG函数 SELECT ...

  9. [.net程序员必看]微软新动向之Android和IOS应用 visual studio 2015 Cordova[原创]

    自萨蒂亚·纳德拉(Satya Nadella)上任微软CEO以来,可谓是惊喜不断,仿佛让世界尤其是我们.net程序员心中又燃起了希望.先是免费提供 iOS 版和安卓版 Office:然后在 xbox ...

  10. Win7下硬盘安装Ubuntu 12.04.3双系统

    一. 准备工作 1. 下载ubuntu镜像文件:Ubuntu-12.04.3-desktop-amd64.iso(4G及以上内存建议64位),注意这个amd并不是指amd芯片. 2. 下载硬盘分区工具 ...