The previous section has learned the CLI command line to control JBoss, if you want to programmatically control JBoss in the program, you can refer to the following code, in fact, in the previous article, using code to control JBoss DataSource, has been contacted, the API and the CLI is completely equivalent, One is the manual knocking instruction, one is the code control, the final effect is consistent.
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 command line: Ls/host=master/server-config list<string> Server s = getservers ("master"); for (String s:servers) {System.out.println (s); }} @Test public void Getserverstatus () {///equals CLI command line:/host=master/server=server-one:read-attribute (name=s erver-state) System.out.println (GetserverstatuS ("Master", "Server-one")); Equivalent to CLI command line:/host=master/server-config=server-one:read-attribute (name=status) System.out.println ( GetServerStatus2 ("Master", "Server-one")); } @Test public void Teststartserver () {//= CLI command line:/host=master/server-config=server-one:start System . OUT.PRINTLN (StartServer ("Master", "Server-one")); } @Test public void Teststopserver () {//= CLI command line:/host=master/server-config=server-one:stop SYSTEM.O Ut.println (Stopserver ("Master", "Server-one")); /** * Gets the specified server run status * @param hostName * @param serverName * @return */public String Getserverst ATUs (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; }/** * Another way to get the server running state * @param hostName * @param serverName * @return */public String Getserv ErStatus2 (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; /** * Start the specified server * * @param hostName * @param serverName * * Public modelnode startserver (String H Ostname, 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; }/** * Stops the specified server * * @param hostName * @param serverName * * Public modelnode stopserver (String ho Stname, 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; /** * Gets all servers under the specified host * * @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 { Back 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); }}
In addition to the native Managent API, JBoss also provides a set of HTTP-based rest-style APIs, the 9990-port corresponding API, which is interesting to refer to the following article
Https://docs.jboss.org/author/display/AS71/The+HTTP+management+API
Https://docs.jboss.org/author/display/AS71/The+native+management+API
GitHub has an open source project that manages JBoss from the mobile phone, which is based on the HTTP API, and technically, using these two sets of APIs, you can customize a JBoss management console (either C/S or b/s).
Jboss eap:native Management API Learning