How can I cluster application-scope state in wildfly?

366 Views Asked by At

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?

2

There are 2 best solutions below

1
On

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
On

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.