使用org.jboss.dmr.ModelNode更改数据源属性

时间:2018-08-07 08:48:46

标签: java wildfly datasource jboss-cli

standalone.xml中有相当常规的数据源定义

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/MYDS" pool-name="MYDS" enabled="true" use-java-context="true">
            <connection-url>jdbc:hsqldb:file:MYDB</connection-url>
            <security>
                <user-name>superman</user-name>
            </security>
        </datasource>
   </datasources>
</subsystem>

尝试从命令行更改某些属性,这可以正常工作:

/subsystem=datasources/data-source=MYDS:write-attribute(name=connection-url,value=achooo)

结果:

{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}

但是使用ModelNode API会让我发疯。

ModelNode request = new ModelNode();
request.get(ClientConstants.OP_ADDR).add("subsystem", "datasources");
request.get(ClientConstants.OP_ADDR).add("data-source", "MYDS");
request.get(ClientConstants.OP).set(ClientConstants.WRITE_ATTRIBUTE_OPERATION);
request.get("connection-url").set("achooo");
myCliExecutor.executeCliCommandWithResult(request);

结果:

{
    "outcome" => "failed",
    "failure-description" => "JBAS014746: name may not be null",
    "rolled-back" => true,
    "response-headers" => {"process-state" => "reload-required"}
}

1 个答案:

答案 0 :(得分:1)

您的DMR不太正确。对于write-attribute操作,您需要定义namevalue属性。它看起来应该更像:

final ModelNode address = new ModelNode().setEmptyList();
address.add("subsystem", "datasources");
address.add("data-source", "ExampleDS");

ModelNode request = new ModelNode();
request.get(ClientConstants.OP_ADDR).set(address);
request.get(ClientConstants.OP).set(ClientConstants.WRITE_ATTRIBUTE_OPERATION);
request.get("name").set("connection-url");
request.get("value").set("achooo");
myCliExecutor.executeCliCommandWithResult(request);

org.wildfly.core:controller-client中还有一个Operations助手应该提供帮助。

final ModelNode address = Operations.createAddress("subsystem", "datasources", "data-source", "MYDS");
final ModelNod op = Operations.createWriteAttributeOperation(address, "connection-url", "achooo");
myCliExecutor.executeCliCommandWithResult(op);