如何使用Detyped Java API在WildFly/JBoss中以编程方式添加缓存。

huangapple go评论84阅读模式
英文:

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)(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添加配置的匹配方法。

需要使用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:

&lt;cache-container name=&quot;cache1&quot;&gt;
	&lt;transport lock-timeout=&quot;60000&quot;/&gt;
	&lt;replicated-cache name=&quot;replicated1&quot;&gt;
		&lt;transaction mode=&quot;BATCH&quot;/&gt;
 	&lt;/replicated-cache&gt;
	&lt;replicated-cache name=&quot;replicated2&quot;&gt;
		&lt;transaction mode=&quot;BATCH&quot;/&gt;
 	&lt;/replicated-cache&gt;
&lt;/cache-container&gt;

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)
{
    &quot;address&quot; =&gt; [(&quot;core-service&quot; =&gt; &quot;module-loading&quot;)],
    &quot;operation&quot; =&gt; &quot;read-attribute&quot;,
    &quot;include-defaults&quot; =&gt; true,
    &quot;name&quot; =&gt; &quot;module-roots&quot;
}

答案2

得分: 1

终于明白了。一个好的方法似乎是按照以下步骤进行:

  1. 搜索你想要处理的方面的API文档,例如缓存的传输配置 https://docs.wildfly.org/26.1/wildscribe/subsystem/infinispan/cache-container/transport/jgroups/index.html,它指出这里需要使用jgroups类型。
  2. 使用jboss-cli来测试你构建的内容(这个概述可能会有帮助,其中包含了标准配置的示例:https://gist.github.com/cheinema/a68ae81f1bbdc695c755)。
  3. 当你找到正确的命令后,将其转换为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:

  1. 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
  2. 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)
  3. 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(&quot;subsystem&quot;,&quot;infinispan&quot;);
    
cacheContainer.get(ClientConstants.OP_ADDR).add(&quot;cache-container&quot;,&quot;cache1&quot;);		
cacheContainer.get(ClientConstants.OP_ADDR).add(&quot;transport&quot;,&quot;jgroups&quot;);		
    
cacheContainer.get(&quot;lock-timeout&quot;).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).

huangapple
  • 本文由 发表于 2023年8月9日 16:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定