
本文介绍了在java应用程序中执行wildfly服务器重载操作后,如何准确判断服务器是否已完全启动并运行。针对`reload`命令本身不阻塞直到服务器完全就绪的问题,文章提出并演示了利用wildfly `modelcontrollerclient`结合辅助api轮询服务器状态的解决方案,确保后续操作如部署能安全执行。
在自动化部署或管理WildFly服务器的场景中,我们经常需要在执行reload命令后等待服务器完全重启并就绪,才能进行后续的操作,例如部署新的应用内容。然而,直接通过Java的Process API执行WildFly CLI的reload命令并调用process.waitFor()方法,并不能达到预期的效果。
当我们通过CliCommandBuilder和Launcher执行reload命令时:
CliCommandBuilder cliCommandBuilder = ...;
cliCommandBuilder.setCommand("reload");
Process process = Launcher.of(cliCommandBuilder)
.inherit()
.setRedirectErrorStream(true)
.launch();reload命令会指示WildFly服务器执行一个优雅的关机和重启过程。但需要注意的是,执行reload命令的CLI进程本身通常会相对较快地退出,它仅仅是向WildFly服务器发出了重载的指令,而不是等待服务器完成整个重启周期。因此,如果紧接着调用process.waitFor():
Launcher.of(cliCommandBuilder)
.inherit()
.setRedirectErrorStream(true)
.launch().waitFor();waitFor()方法只会等待CLI进程的终止,而不是等待WildFly服务器完全启动并进入运行状态。这意味着,在CLI进程退出后,Java应用程序的线程会继续执行,但此时WildFly服务器可能仍在启动过程中,尚未准备好接受新的部署或请求。如果此时尝试进行部署,可能会因为服务器未完全就绪而失败。
立即学习“Java免费学习笔记(深入)”;
为了准确判断WildFly服务器是否已完成重载并处于运行状态,我们需要在CLI进程退出后,通过WildFly的管理接口主动查询服务器的运行状态。这可以通过ModelControllerClient结合WildFly管理API辅助类来实现。
以下是实现这一机制的详细步骤和代码示例:
首先,我们像往常一样执行reload命令。重要的是,我们仍然需要等待这个CLI进程的终止,以确保reload指令已经被服务器接收。
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.dmr.ModelNode;
import org.wildfly.core.cli.CliCommandBuilder;
import org.wildfly.core.cli.Launcher;
import org.wildfly.plugins.core.ServerHelper; // 需要引入 wildfly-maven-plugin-core 依赖
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class WildFlyReloadWaiter {
private static final String WILDFLY_HOME = "/opt/wildfly-27.0.0.Final"; // 替换为你的WildFly安装路径
private static final String MANAGEMENT_HOST = "localhost";
private static final int MANAGEMENT_PORT = 9990;
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Initiating WildFly server reload...");
// 1. 构建并执行reload命令
final CliCommandBuilder commandBuilder = CliCommandBuilder.of(WILDFLY_HOME)
.setConnection(MANAGEMENT_HOST + ":" + MANAGEMENT_PORT)
.setCommand("reload");
final Process process = Launcher.of(commandBuilder)
.inherit() // 继承父进程的IO流,便于观察CLI输出
.setRedirectErrorStream(true)
.launch();
// 2. 等待CLI进程终止
// 设定一个合理的超时时间,防止CLI命令卡住
if (!process.waitFor(60, TimeUnit.SECONDS)) { // 例如,等待60秒
throw new RuntimeException("WildFly CLI reload command process failed to terminate within the timeout.");
}
System.out.println("WildFly CLI reload command process terminated.");
// 3. 轮询服务器状态直到其运行
waitForWildFlyServerToStart(MANAGEMENT_HOST, MANAGEMENT_PORT, 180, TimeUnit.SECONDS); // 设定服务器启动超时时间
System.out.println("WildFly server has successfully reloaded and is running.");
// 服务器已就绪,现在可以执行后续操作,例如部署
// deployNewContent();
}
private static void waitForWildFlyServerToStart(String host, int port, long timeout, TimeUnit unit) throws IOException, InterruptedException {
System.out.printf("Waiting for WildFly server to start at %s:%d...%n", host, port);
long startTime = System.currentTimeMillis();
long timeoutMillis = unit.toMillis(timeout);
try (ModelControllerClient client = ModelControllerClient.Factory.create(host, port)) {
while (!ServerHelper.isStandaloneRunning(client)) {
if (System.currentTimeMillis() - startTime > timeoutMillis) {
throw new RuntimeException("WildFly server did not restart within the specified timeout (" + timeout + " " + unit.name() + ").");
}
TimeUnit.MILLISECONDS.sleep(500L); // 每隔500毫秒检查一次
}
// 服务器已运行,可选地读取其运行模式进行确认
ModelNode op = Operations.createReadResourceOperation();
op.get(ClientConstants.INCLUDE_RUNTIME).set(true);
ModelNode result = client.execute(op);
if (Operations.isSuccessfulOutcome(result)) {
String runningMode = Operations.readResult(result).get("running-mode").asString();
System.out.printf("WildFly Server is now confirmed running. Mode: %s%n", runningMode);
} else {
System.err.printf("Failed to read server running mode after restart: %s%n", Operations.getFailureDescription(result).asString());
}
}
}
}在CLI进程退出后,我们使用ModelControllerClient连接到WildFly的管理接口,并通过ServerHelper.isStandaloneRunning()方法持续检查服务器是否处于运行状态。
要运行上述代码,你需要在项目的pom.xml中添加以下Maven依赖:
<dependencies>
<!-- WildFly CLI Launcher -->
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-cli-launcher</artifactId>
<version>20.0.0.Final</version> <!-- 根据你的WildFly版本选择合适的版本 -->
</dependency>
<!-- WildFly Controller Client (for ModelControllerClient) -->
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-controller-client</artifactId>
<version>20.0.0.Final</version> <!-- 与wildfly-cli-launcher版本匹配 -->
</dependency>
<!-- WildFly Maven Plugin Core (for ServerHelper) -->
<dependency>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin-core</artifactId>
<version>4.2.0.Final</version> <!-- 选择最新稳定版本 -->
</dependency>
<!-- JBoss DMR (for ModelNode operations) -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-dmr</artifactId>
<version>1.6.1.Final</version> <!-- 选择最新稳定版本 -->
</dependency>
</dependencies>请根据你使用的WildFly版本和项目需求,调整上述依赖的版本号。
通过上述方法,你可以在Java应用程序中可靠地等待WildFly服务器完成重载并启动,从而确保后续的自动化管理和部署任务能够顺利执行。
以上就是使用Java API监控WildFly服务器重载完成状态的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号