如何从ModelNode结果中获取特定对象

时间:2019-02-20 14:44:48

标签: java jboss wildfly application-server

我与 ModelControllerClient 一起获得有关Logger的一些信息,这些信息在 Wildfly服务器中使用。 我想要一个所有记录器名称的列表。

这是我获取有关记录器的所有信息的方式:

ModelNode op = new ModelNode();
op.get("operation").set("read-resource");

ModelNode address = op.get("address");
address.add("subsystem", "logging");
address.add("logger", "*");

ModelNode result = client.execute(op);

这是我得到的 ModelNode 结果的一个小例子。

{
        "address" => [
            ("subsystem" => "logging"),
            ("logger" => "jacorb")
        ],
        "outcome" => "success",
        "result" => {
            "category" => "jacorb",
            "filter" => undefined,
            "filter-spec" => undefined,
            "handlers" => undefined,
            "level" => "WARN",
            "use-parent-handlers" => true
        }
    },
    {
        "address" => [
            ("subsystem" => "logging"),
            ("logger" => "jacorb.config")
        ],
        "outcome" => "success",
        "result" => {
            "category" => "jacorb.config",
            "filter" => undefined,
            "filter-spec" => undefined,
            "handlers" => undefined,
            "level" => "ERROR",
            "use-parent-handlers" => true
        }
    },

我只想获取“ logger” /“ category”的值。

我试图通过以下方式获取记录器的名称:

result.get("logger");

,但返回“未定义”。我想问题是我有多个对象称为“记录器”。当我尝试使用“类别”时,也会发生相同的情况。

1 个答案:

答案 0 :(得分:1)

您需要先阅读结果。在简单的情况下,它将是result.get("result", "logger")。您还可以使用Operations API并执行类似Operations.readResult(result).get("logger")的操作。

如果仅需要记录器名称,请参见以下示例:

try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990)) {
    final ModelNode address = Operations.createAddress("subsystem", "logging");
    final ModelNode op = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION, address);
    op.get(ClientConstants.CHILD_TYPE).set("logger");
    final ModelNode result = client.execute(op);
    if (Operations.isSuccessfulOutcome(result)) {
        final List<ModelNode> loggerNames = Operations.readResult(result).asList();
        for (ModelNode loggerName : loggerNames) {
            System.out.printf("logger=%s%n", loggerName.asString());
        }
    } else {
        System.err.printf("Failed to get the logger names: %s%n", Operations.getFailureDescription(result).asString());
    }
}

如果您需要完整的说明,则可以改用read-children-resource操作。

WildFly文档还包含using the ModelControllerClientModelNode and ModelType API的一部分。