【问题标题】:How to perform system property operations in WildFly via REST?如何通过 REST 在 WildFly 中执行系统属性操作?
【发布时间】:2021-12-09 06:16:53
【问题描述】:

本文档指出可以通过 REST 对 WildFly 服务器执行某些操作:https://docs.jboss.org/author/display/WFLY10/The%20HTTP%20management%20API.html 但是,没有示例如何添加/删除/读取系统属性。我不知道 HTTP 正文必须如何查找这些调用。

以下 StackOverflow 问题的答案表明示例中使用的类 SimpleOperation 并不真正存在:Wildfly 10 management Rest API

我想做以下操作:

/system-property=BLA:remove
/system-property=BLA:add(value="1,2,3,4")

并阅读它。

如何使用 WildFly HTTP 管理 API 通过 REST 执行这些操作?理想情况下,如果有 Java API,我会使用它。

【问题讨论】:

  • 您对非 REST API 持开放态度吗?在 Java 中有一种简单的方法。
  • REST 会好很多,但您可以发布一个带有 Java API 的示例。你说的是 EJB 吗?

标签: java rest jboss wildfly system-properties


【解决方案1】:

使用org.wildfly.core:wildfly-controller-client API,您可以执行以下操作:

try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990)) {
    final ModelNode address = Operations.createAddress("system-property", "test.property");
    ModelNode op = Operations.createRemoveOperation(address);
    ModelNode result = client.execute(op);
    if (!Operations.isSuccessfulOutcome(result)) {
        throw new RuntimeException("Failed to remove property: " + Operations.getFailureDescription(result).asString());
    }
    op = Operations.createAddOperation(address);
    op.get("value").set("test-value");
    result = client.execute(op);
    if (!Operations.isSuccessfulOutcome(result)) {
        throw new RuntimeException("Failed to add property: " + Operations.getFailureDescription(result).asString());
    }
}

您也可以使用 REST API,但是您需要有一种方法来进行摘要式身份验证。

Client client = null;
try {
    final JsonObject json = Json.createObjectBuilder()
            .add("address", Json.createArrayBuilder()
                    .add("system-property")
                    .add("test.property.2"))
            .add("operation", "add")
            .add("value", "test-value")
            .build();
    client = ClientBuilder.newClient();
    final Response response = client.target("http://localhost:9990/management/")
            .request()
            .header(HttpHeaders.AUTHORIZATION, "Digest <settings>")
            .post(Entity.json(json));
    System.out.println(response.getStatusInfo());
} finally {
    if (client != null) client.close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2016-06-08
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多