You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ModelControllerClient在Wildfly中编程添加模块?

用ModelControllerClient编程实现WildFly模块添加

在WildFly-CLI中,可通过jboss-cli.sh执行以下命令添加模块:

module add --name=org.postgres
           --resources=postgresql-42.2.5.jar
           --dependencies=javax.api,javax.transaction.api

该操作会将postgresql-42.2.5.jar文件添加到指定路径,同时在/modules/org/postgres/main目录下生成如下结构的module.xml文件:

<?xml version='1.0' encoding='UTF-8'?>
<module xmlns="urn:jboss:module:1.1" name="org.postgres">
    <resources>
        <resource-root path="postgresql-42.5.1.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

接下来通过ModelControllerClient(依赖坐标org.wildfly.core:wildfly-controller-client:19.0.1.Final)编程实现上述操作,先初始化客户端:

ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990);

实现步骤

要完成模块添加,需构建对应的操作请求并处理资源文件上传,核心逻辑如下:

  1. 构建模块添加操作
    使用Operation.Builder创建对应module add的操作,指定模块名称、依赖等参数:

    // 构建模块添加操作
    Operation op = Operation.create("module", "add")
            .addParameter("name", "org.postgres")
            .addParameter("dependencies", Arrays.asList("javax.api", "javax.transaction.api"))
            .build();
    
  2. 上传资源文件并执行操作
    由于需要将本地JAR文件上传到服务器模块目录,需结合Attachment传递文件,再执行操作:

    try {
        // 指定本地Postgres驱动JAR路径
        File jarFile = new File("/你的本地路径/postgresql-42.2.5.jar");
        // 创建附件,对应CLI中的--resources参数
        Attachment attachment = Attachment.create("resources", jarFile);
        // 执行操作并获取结果
        ModelNode result = client.executeOperation(op, Collections.singletonList(attachment));
        
        // 校验操作结果
        if (result.get("outcome").asString().equals("success")) {
            System.out.println("模块添加成功");
        } else {
            System.err.println("模块添加失败: " + result.get("failure-description").asString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 关闭客户端连接
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

完整代码示例

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.Operation;
import org.jboss.as.controller.client.Attachment;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;

public class WildFlyPostgresModuleAdd {
    public static void main(String[] args) {
        // 使用try-with-resources自动关闭客户端
        try (ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990)) {
            // 构建模块添加操作
            Operation op = Operation.create("module", "add")
                    .addParameter("name", "org.postgres")
                    .addParameter("dependencies", Arrays.asList("javax.api", "javax.transaction.api"))
                    .build();

            // 本地Postgres驱动文件
            File jarFile = new File("postgresql-42.2.5.jar");
            Attachment attachment = Attachment.create("resources", jarFile);

            // 执行操作
            ModelNode result = client.executeOperation(op, Collections.singletonList(attachment));

            if ("success".equals(result.get("outcome").asString())) {
                System.out.println("Postgres模块添加成功");
            } else {
                System.err.println("模块添加失败: " + result.get("failure-description").asString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  • 确保WildFly服务器处于运行状态,管理端口(默认9990)可正常访问
  • 运行代码的用户需具备WildFly服务器的管理权限
  • wildfly-controller-client版本需与WildFly服务器版本匹配(示例用19.0.1.Final,对应WildFly 26.x系列)

内容的提问来源于stack exchange,提问作者Thor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 21:11:29