How can I cluster application-scope state in wildfly?

无人久伴 提交于 2021-01-29 08:43:09

问题


I would like to cluster a map that is kept on application-level scope.

A first thought was to use a @Singleton, @Clustered bean with a field holding my data.
This does not seem to work and my guess is that it was never implemented

This post proposes ways to implement clustered singletons but they seem complex.

The only alternative that I see, apart from manually updating db table(s), is to use a replicated cache.

My question is: Is it advised to declare and use an infinispan cache (like this) for solving this problem?
If yes, what settings should I use to avoid dirty reads?
Is there any other option to this relatively simple problem in the era of wildfly-18?


回答1:


As you mentioned a cluster, you need to use standalone-full-ha.xml

Edit the file and add the following configuration

<cache-container module="org.infinispan.extension" name="infinispan_container" default-cache="default">
    <transport lock-timeout="60000"/>
    <global-state/>
    <distributed-cache name="default"/>
    <local-cache name="localCache"/>
    <replicated-cache name="replicatedCache"/>
</cache-container>

You have different caches available for your application, if you wish use a replicated cache, you can inject it in your app with

@Resource(lookup = "java:jboss/datagrid-infinispan/container/infinispan_container/cache/replicatedCache")
Cache cache;



回答2:


Diego's response assumes that you are using the WF modules distributed by Infinispan (note the datagrid-infinispan namespace).

To use an arbitrary Infinispan cache defined in WF's Infinispan subsystem, use jndi names of the form: java:jboss/infinispan/cache/your-container-name/your-cache-name.

e.g.

@Resource(lookup="java:jboss/infinispan/cache/foo/bar")
private Cache<K, V> cache;

WF will manage the lifecycle of the cache, ensuring it is started/stopped when necessary.



来源:https://stackoverflow.com/questions/59156357/how-can-i-cluster-application-scope-state-in-wildfly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!