上一節已經學習了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
文章列表