英文:
How to add a cache programmatically in WildFly/JBoss using the Detyped Java API
问题
有许多示例可用,说明如何使用Native Management API以编程方式添加数据源,详见 https://docs.jboss.org/author/display/AS71/Management%20API%20reference.html 以及其在JBoss DMR(Dynamic Model Representation)中的Java实现,链接在 https://github.com/jbossas/jboss-dmr
例如,MasterTheBoss 提供了一个使用Java库的教程,其中包括数据源添加的示例:
https://www.mastertheboss.com/jbossas/jboss-as-7/using-jboss-management-api-programmatically/
因此,我找不到有关如何添加具有子元素的更复杂结构的有用文档或示例。对于数据源,数据源下的所有(XML-)元素都应被视为数据源的属性,然后由服务器自己渲染,但对于Infinispan缓存的情况,我找不到通过JBoss DMR添加配置的匹配方法。
以下是从配置的XML表示中摘录的片段,需要使用API添加到infinispan子系统中:
<cache-container name="cache1">
<transport lock-timeout="60000"/>
<replicated-cache name="replicated1">
<transaction mode="BATCH"/>
</replicated-cache>
<replicated-cache name="replicated2">
<transaction mode="BATCH"/>
</replicated-cache>
</cache-container>
希望这能提供有帮助的线索。
英文:
There are many examples available how to add a datasource programatically using the Native Management API https://docs.jboss.org/author/display/AS71/Management%20API%20reference.html and its Java implementation in the JBoss DMR (Dynamic Model Representation) https://github.com/jbossas/jboss-dmr
A tutorial using the Java library with a datasource addition as an example is given at MasterTheBoss for instance:
https://www.mastertheboss.com/jbossas/jboss-as-7/using-jboss-management-api-programmatically/
Thus, I could not find helpful documentation or an example where a more complex strucure with sub-elements was added to the configuration. For the datasource, all (XML-)elements under datasource are to be addressed as properties of the datasource and then rendered by the server itself, whereas for the following case of an Infinispan cache, I could not find the matching approach for adding the configuration through JBoss DMR.
The following snippet from the XML representation of the configuration needs to be added in the infinispan subsystem using the API:
<cache-container name="cache1">
<transport lock-timeout="60000"/>
<replicated-cache name="replicated1">
<transaction mode="BATCH"/>
</replicated-cache>
<replicated-cache name="replicated2">
<transaction mode="BATCH"/>
</replicated-cache>
</cache-container>
A helpful hint would be great
答案1
得分: 2
jboss-cli可以使用echo-dmr命令显示您的命令的dmr结构:
echo-dmr /core-service=module-loading:read-attribute(name=module-roots, include-defaults)
{
"address" => [("core-service" => "module-loading")],
"operation" => "read-attribute",
"include-defaults" => true,
"name" => "module-roots"
}
英文:
jboss-cli can show you the dmr structure of your command using the echo-dmr command:
echo-dmr /core-service=module-loading:read-attribute(name=module-roots, include-defaults)
{
"address" => [("core-service" => "module-loading")],
"operation" => "read-attribute",
"include-defaults" => true,
"name" => "module-roots"
}
答案2
得分: 1
明白了。一个好的方法似乎是如下:
- 搜索API文档,以查找您想要处理的方面,例如缓存的传输配置 https://docs.wildfly.org/26.1/wildscribe/subsystem/infinispan/cache-container/transport/jgroups/index.html,该文档指出了需要在此处使用jgroups类型。
- 使用jboss-cli 来测试您构建的内容(此处的标准配置示例可能会有所帮助:https://gist.github.com/cheinema/a68ae81f1bbdc695c755)。
- 当您找到了正确的命令时,将其转换为Java DMR API。
以下示例展示了这个过程可能是如何进行的:
/subsystem=infinispan/cache-container=cache1/transport=jgroups:add(lock-timeout=20)
这可以在Java中使用如下方式:
ModelNode cacheContainer = new ModelNode();
cacheContainer.get(ClientConstants.OP_ADDR).add("subsystem","infinispan");
cacheContainer.get(ClientConstants.OP_ADDR).add("cache-container","cache1");
cacheContainer.get(ClientConstants.OP_ADDR).add("transport","jgroups");
cacheContainer.get("lock-timeout").set(20);
cacheContainer.get(ClientConstants.OP).set(ClientConstants.ADD);
您首先在第一行中添加要修改的地址,然后在那里定义您想要设置的属性(在此示例中为lock-timeout),接着是操作 - 这里是ADD(例如,也可以是REMOVE)。
英文:
Got it finally. A good approach seems to be as follows:
- Search the API documentation for the aspects you want to address, e.g. the transport configuration for caches https://docs.wildfly.org/26.1/wildscribe/subsystem/infinispan/cache-container/transport/jgroups/index.html which pointed out that the jgroups type needs to be used here
- Use the jboss-cli to test what you constructed (helpful might be this overview for examples of the standard configuration: https://gist.github.com/cheinema/a68ae81f1bbdc695c755)
- When you found the right command(s), make translate them to the Java DMR API
The following example shows how this could look like:
/subsystem=infinispan/cache-container=cache1/transport=jgroups:add(lock-timeout=20)
Which can be used in Java like this:
ModelNode cacheContainer = new ModelNode();
cacheContainer.get(ClientConstants.OP_ADDR).add("subsystem","infinispan");
cacheContainer.get(ClientConstants.OP_ADDR).add("cache-container","cache1");
cacheContainer.get(ClientConstants.OP_ADDR).add("transport","jgroups");
cacheContainer.get("lock-timeout").set(20);
cacheContainer.get(ClientConstants.OP).set(ClientConstants.ADD);
You first add to the address you want to modify in the first lines, then define the attribute you want to set there (lock-timeout in this case), followed by the operation - here ADD (may also be REMOVE for example).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论