- Mastering JBoss Enterprise Application Platform 7
- Francesco Marchioni Luigi Fugaro
- 1556字
- 2021-07-14 10:07:46
Features of the CLI
The CLI offers you some neat features such as executing operations as an atomic operation - which is called batch mode - scripting all the commands into a file (commonly named .cli
files), and also the opportunity to do configuration tasks offline, which means without a running JBoss EAP instance... or better, there is one, but it's embedded into the CLI and it's not invisible outside.
Let's start gradually.
Batch mode
Batch mode allows you to run multiple CLI commands and operations in sequence, and commit the whole set of statements as one. The all-or-nothing commit pattern is applied, which means that all tasks must run successfully in order to complete correctly, otherwise if just one task goes in error, the whole batch sequence rolls back.
To enable batch mode, first you need to invoke the batch
directive, since all future commands and operations will not be executed until you invoke the end of the batch sequence by invoking the run-batch
command.
Suppose you have an application that needs to connect to a MySQL database via JNDI. You will first need to add the JDBC driver, then add datasource configuration, and then deploy the application. All these tasks can be done in batch mode, as follows:
[standalone@localhost:9990 /] batch [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-module-name=jdbc.mysql, driver-name=mysql) [standalone@localhost:9990 /] /subsystem=datasources/data-source=MySQL-DS:add(jndi-name="java:jboss/datasources/MySQL-DS", connection-url="jdbc:mysql://localhost:3306/MJB7", driver-name="mysql", user-name="mjb7", password="mjb7", prepared-statements-cache-size=128, share-prepared-statements=true, blocking-timeout-wait-millis=60000, idle-timeout-minutes=20) [standalone@localhost:9990 /] deploy mjb7.war [standalone@localhost:9990 /] run-batch
The preceding CLI script actually means the following:
- Start a new batch atomic transaction
- Add the JDCB driver
- Create a data-source
- Deploy the application
- Execute all the statements
There is also a nice feature to use while running batch mode, which is to label a set of batch statements and recall them when needed. The command to achieve such a feature is holdback-batch
. Improving the preceding example, we could remove the default H2
driver, the default ExampleDS
datasource, and split the rest of the example in two, as follows:
[standalone@localhost:9990 /] batch [standalone@localhost:9990 /] /subsystem=datasources/data- source=ExampleDS:remove [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=h2:remove [standalone@localhost:9990 /] holdback-batch remove-default-data-source [standalone@localhost:9990 /] batch [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-module-name=jdbc.mysql, driver-name=mysql) [standalone@localhost:9990 /] holdback-batch add-mysql-jdbc-driver [standalone@localhost:9990 /] batch [standalone@localhost:9990 /] /subsystem=datasources/data-source=MySQL-DS:add(jndi-name="java:jboss/datasources/MySQL-DS", connection-url="jdbc:mysql://localhost:3306/MJB7", driver-name="mysql", user-name="mjb7", password="mjb7", prepared-statements-cache-size=128, share-prepared-statements=true, blocking-timeout-wait-millis=60000, idle-timeout-minutes=20) [standalone@localhost:9990 /] holdback-batch add-mysql-data-source [standalone@localhost:9990 /] batch [standalone@localhost:9990 /] deploy mjb7.war [standalone@localhost:9990 /] holdback-batch deploy-mjb7
We can now run each batch, as follows:
[standalone@localhost:9990 /] batch remove-default-data-source [standalone@localhost:9990 /] run-batch [standalone@localhost:9990 /] batch add-mysql-jdbc-driver [standalone@localhost:9990 /] run-batch [standalone@localhost:9990 /] batch add-mysql-data-source [standalone@localhost:9990 /] run-batch [standalone@localhost:9990 /] batch deploy-mjb7 [standalone@localhost:9990 /] run-batch
This feature is quite useful when you have a repeatable task to execute on demand.
Commands in batch mode
There are also other commands available in batch mode. The following table describes all available command when running batch mode:

Scripting in the CLI
Scripting the CLI is intended to have interaction with the CLI itself, thus making more sophisticated tasks, read and elaborate the output of a command and make decisions on it. Typically, this is done in Unix-like environments, where there is a massive use of scripts. The first thing you need to know is that you can invoke commands outside of the CLI, as follows:
$JBOSS_HOME/bin/jboss-cli.sh --connect --command=":read-attribute(name=server-state)" { "outcome" => "success", "result" => "running" }
As mentioned, you can elaborate the output of the command for further use as you prefer. For example, from the previous output we just want to know if the server is in a running state or not, and we can achieve this as follows (only in Linux environments):
$JBOSS_HOME/bin/jboss-cli.sh --connect --command=":read-attribute(name=server-state)" | awk 'NR==3 { print $3 }' "running"
Another feature is executing a list of commands (including batch as well) stored in a file, typically a .cli
file. Suppose that the previous example is stored in a file named server-state.cli
:
connect :read-attribute(name=server-state)"
You can invoke the commands within the file, as follows:
$JBOSS_HOME/bin/jboss-cli.sh --file="ch02/server-state.cli" { "outcome" => "success", "result" => "running" }
As shown before, you can elaborate the output as you prefer. Eventually, you can redirect the output to a file, if you don't need it printed on the screen. Both Windows and Unix-like environments can redirect an output using the >
operator, as follows:
$JBOSS_HOME/bin/jboss-cli.sh --file="ch02/server-state.cli" > output.log
Using the CLI in offline mode
In previous versions, such as JBoss EAP 6, if you wanted to configure your server using the CLI, you needed a running JBoss instance. Now, with the new JBoss EAP 7, you have the feature to set up your entire environment directly from within the CLI without a running server, or better, running a server directly from the CLI as an embedded server.
The embedded server is a fully running EAP instance, except that it does not deploy applications, it does not open network ports, and it has to be considered for internal use only.
To run an embedded server, just do as follows:
[root@foogaro eap7]# ./bin/jboss-cli.sh You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands. [disconnected /] embed-server --std-out=echo 12:09:11,779 INFO [org.jboss.modules] (AeshProcess: 1) JBoss Modules version 1.5.1.Final-redhat-1 12:09:11,992 INFO [org.jboss.msc] (AeshProcess: 1) JBoss MSC version 1.2.6.Final-redhat-1 12:09:12,112 INFO [org.jboss.as] (MSC service thread 1-7) WFLYSRV0049: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) starting 12:09:13,827 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) started in 2025ms - Started 35 of 47 services (18 services are lazy, passive or on-demand) [standalone@embedded /]
This kind of feature is very useful when you want to automate a configuration process to spread to different servers.
Once the configuration is done, you can reload the server and run it as fully operative with the following command:
[standalone@embedded /] reload --admin-only=false
The output should look like the following:
[standalone@embedded /] reload --admin-only=false 10:13:57,873 INFO [org.jboss.as] (MSC service thread 1-8) WFLYSRV0050: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) stopped in 30ms 10:13:57,876 INFO [org.jboss.as] (MSC service thread 1-5) WFLYSRV0049: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) starting 10:13:58,158 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http) 10:13:58,184 INFO [org.xnio] (MSC service thread 1-2) XNIO version 3.3.6.Final-redhat-1 10:13:58,194 INFO [org.xnio.nio] (MSC service thread 1-2) XNIO NIO Implementation Version 3.3.6.Final-redhat-1 10:13:58,272 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem. 10:13:58,279 WARN [org.jboss.as.txn] (ServerService Thread Pool -- 54) WFLYTX0013: Node identifier property is set to the default value. Please make sure it is unique. 10:13:58,302 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem 10:13:58,315 INFO [org.jboss.as.security] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem 10:13:58,334 INFO [org.wildfly.extension.io] (ServerService Thread Pool -- 37) WFLYIO001: Worker 'default' has auto-configured to 8 core threads with 64 task threads based on your 4 available processors 10:13:58,350 INFO [org.jboss.remoting] (MSC service thread 1-2) JBoss Remoting version 4.0.18.Final-redhat-1 10:13:58,351 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension 10:13:58,350 INFO [org.jboss.as.jsf] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main] 10:13:58,394 INFO [org.jboss.as.security] (MSC service thread 1-4) WFLYSEC0001: Current PicketBox version=4.9.6.Final-redhat-1 10:13:58,430 INFO [org.wildfly.extension.undertow] (MSC service thread 1-7) WFLYUT0003: Undertow 1.3.21.Final-redhat-1 starting 10:13:58,436 INFO [org.jboss.as.mail.extension] (MSC service thread 1-3) WFLYMAIL0001: Bound mail session [java:jboss/mail/Default] 10:13:58,437 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0003: Undertow 1.3.21.Final-redhat-1 starting 10:13:58,434 INFO [org.jboss.as.naming] (MSC service thread 1-6) WFLYNAM0003: Starting Naming Service 10:13:58,472 INFO [org.jboss.as.connector] (MSC service thread 1-6) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.3.3.Final-redhat-1) 10:13:58,481 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3) 10:13:58,502 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0018: Started Driver service with driver-name = h2 10:13:58,810 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0014: Creating file handler for path '/opt/rh/eap7/welcome-content' with options [directory-listing: 'false', follow-symlink: 'false', case-sensitive: 'true', safe-symlink-paths: '[]'] 10:13:58,834 INFO [org.jboss.as.ejb3] (MSC service thread 1-6) WFLYEJB0482: Strict pool mdb-strict-max-pool is using a max instance size of 16 (per class), which is derived from the number of CPUs on this host. 10:13:58,834 INFO [org.jboss.as.ejb3] (MSC service thread 1-5) WFLYEJB0481: Strict pool slsb-strict-max-pool is using a max instance size of 64 (per class), which is derived from thread worker pool sizing. 10:13:58,860 INFO [org.wildfly.extension.undertow] (MSC service thread 1-3) WFLYUT0012: Started server default-server. 10:13:58,861 INFO [org.wildfly.extension.undertow] (MSC service thread 1-3) WFLYUT0018: Host default-host starting 10:13:58,950 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080 10:13:59,135 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-5) WFLYDS0013: Started FileSystemDeploymentService for directory /opt/rh/eap7/standalone/deployments 10:13:59,441 INFO [org.jboss.ws.common.management] (MSC service thread 1-4) JBWS022052: Starting JBossWS 5.1.3.SP1-redhat-1 (Apache CXF 3.1.4.redhat-1) 10:13:59,443 INFO [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-5) ISPN000128: Infinispan version: Infinispan 'Mahou' 8.1.2.Final-redhat-1 10:13:59,754 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS] 10:13:59,816 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management 10:13:59,816 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990 10:13:59,817 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: JBoss EAP 7.0.0.GA (WildFly Core 2.1.2.Final-redhat-1) started in 1938ms - Started 267 of 553 services (371 services are lazy, passive or on-demand) [standalone@embedded /]
Using the CLI in graphical mode
If you really don't feel comfortable with the CLI shell, but still don't want to use the Web console, Red Hat engineers thought that you may like thick clients instead. There is a GUI client for the CLI, and it's available by issuing the following command:
$JBOSS_HOME/bin/jboss-cli.sh --gui
And it shows the CLI within a GUI, as depicted here:

At the bottom of the interface there is a useful textbox, which lets you easily find resources, as shown here using data
as the filter:

- Web程序設計及應用
- Mastering Ember.js
- Java入門很輕松(微課超值版)
- Getting Started with Gulp
- Visual Basic程序設計實踐教程
- C#實踐教程(第2版)
- Arduino計算機視覺編程
- 區塊鏈國產化實踐指南:基于Fabric 2.0
- Maker基地嘉年華:玩轉樂動魔盒學Scratch
- Visual C++開發寶典
- HikariCP數據庫連接池實戰
- Learning Redux
- JavaScript程序設計基礎教程(慕課版)
- Learning C# by Developing Games with Unity 3D Beginner's Guide
- Instant MongoDB