使用 org.jboss.dmr.ModelNode 更改数据源 属性
Change datasource property using org.jboss.dmr.ModelNode
我在 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"}
}
您的 DMR 不太正确。对于 write-attribute
操作,您需要定义 name
和 value
属性。它应该看起来更像:
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);
我在 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"}
}
您的 DMR 不太正确。对于 write-attribute
操作,您需要定义 name
和 value
属性。它应该看起来更像:
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);