Infinispan web application - working example

334 views
Skip to first unread message

Mansour Al Akeel

unread,
May 5, 2024, 12:34:50 AM5/5/24
to WildFly
I am trying to understand how to use infinispan in a webapp. As a started I would just like to put regular String objects, retrieve them, add events listeners, before I move further to clustering, and jgroup. I was not able to get a simple application to use infinispan properly. The following error is what I got after hours of struggle:

jakarta.ejb.EJBException: org.infinispan.commons.CacheConfigurationException: ISPN000436: Cache 'passivation' has been requested, but no matching cache configuration exists

This is the same error I get when trying to inject my a custom CacheContainer:

@Resource(lookup = "java:jboss/infinispan/container/web")

private EmbeddedCacheManager cacheContainer;


Looking around, I was not able to find a complete A-Z example to use

it as a reference and compare what I have here. I have seen the
examples repository for infinispan, but no example exist !

https://github.com/wildfly-clustering/wildfly-clustering-examples

My question, is there a place where I can get a working inifinispan
web application, where I can just add and retrieve a value ?

Thank you




Stephen Sill II

unread,
May 6, 2024, 10:01:48 AM5/6/24
to WildFly
yeah I had to figure that same thing out msyelf.

I have an application that runs as a .ear with an accompanying .war and here's what I had to do.

This assumes you've configured a cache container and cache in wildfly

in the .war, I added this to src/main/webapp/WEB-INF/jboss-deployment-structure.xml
<jboss-deployment-structure>
   <deployment>
      <dependencies>
         <module name="org.infinispan" export="TRUE" />
      </dependencies>
   </deployment>

And in web.xml of the same .war project
    <resource-env-ref>
        <resource-env-ref-name>myCache</resource-env-ref-name>
        <lookup-name>java:jboss/infinispan/cache/my-cache-container/my-cache</lookup-name>
    </resource-env-ref>


And then a named bean 
@Named
public class CacheService
{
    @Resource(name = "myCache")
    private Cache<String, String> cache;

    public CacheService()
    {
    }

    public void putCacheValue(String key, String value)
    {
        cache.put(key, value);
    }

    public String getCacheValue(String key)
    {
        return cache.get(key);
    }
}


And finally a webservice to use the cache

@Path("/cache")
public class CacheTestEndpoints
{

    private static final Logger LOG = Logger.getLogger(CacheTestEndpoints.class.getName());

    @Inject
    private CacheService cacheService;

    @GET
    @Path("/put")
    public Response putCacheValue(@QueryParam("key") String key, @QueryParam("value") String value)
    {
        cacheService.putCacheValue(key, value);
        return Response.ok("ok").build();
    }

    @GET
    @Path("/get")
    public Response getCacheValue(@QueryParam("key") String key)
    {
        return Response.ok(cacheService.getCacheValue(key)).build();
    }

Stephen Sill II

unread,
May 6, 2024, 10:02:42 AM5/6/24
to WildFly
Getting access to the cache from an EJB inside .ear is similar, let me know if you need that too.

Mansour Al Akeel

unread,
May 15, 2024, 7:58:07 PM5/15/24
to WildFly
Stephen,
thank you. This was very helpful. 
I was able to get the cache injected using @Resource, like you described.

Nguyễn Toàn

unread,
May 21, 2024, 10:48:35 AM5/21/24
to WildFly
Hi Stephen and all,
I have the same error "org.infinispan.commons.CacheConfigurationException: ISPN000436: Cache 'cuscache' has been requested, but no matching cache configuration exists" while it is deploying the sar file on WF 31.
This is a infinispan configuration in the standalone-full-ha.xml file:
<subsystem xmlns="urn:jboss:domain:infinispan:14.0">
  <cache-container name="my-cache" default-cache="security">
      <local-cache name=" cuscache">
                    <locking acquire-timeout="290000" isolation="READ_COMMITTED" striping="false"/>
                    <transaction locking="OPTIMISTIC" mode="BATCH"/>
                    <expiration lifespan="1800000" max-idle="1000000"/>
        </local-cache>
  </cache-container>
...
And in the applicationContext.xml, it will lookup the cache container and inject to the bean:
    <bean id="csManager" class="com.CustomCacheManager" init-method="register">
    <constructor-arg ref="cacheManager"/>
    </bean>
     <jee:jndi-lookup id="cacheManager" jndi-name="java:jboss/infinispan/container/my-cache" />
Then, in the bean will get the cache "cuscache", but it throws above error:
public  CustomCacheManager  (EmbeddedCacheManager cacheManager) {
cacheManager.getCache("cuscache"); //throw error:   ISPN000436: Cache 'cuscache' has been requested, but no matching cache configuration exists
}
This code works fine with WF 22, but after I migrate to WF 31, it can't work.
Can you help resolve this issue?

Misha Shaygu

unread,
Feb 13, 2025, 5:53:16 AMFeb 13
to WildFly
We need it, please help us!

понедельник, 6 мая 2024 г. в 17:02:42 UTC+3, Stephen Sill II:

Paul Ferraro

unread,
Mar 3, 2025, 3:56:01 AMMar 3
to WildFly
Misha,

What is the problem you are trying to solve?

Assuming that your issue is similar to the others in this thread...

Cache configurations defined by the Infinispan subsystem are installed on-demand.
To ensure that a given cache configuration is available to your deployment, you must establish a dependency between your application and a given cache configuration.
This is most easily done by injecting cache instances directly.
e.g.

@Resource(lookup = "java:jboss/infinispan/cache/containerName/default")
Cache<?, ?> defaultNache;

@Resource(lookup = "java:jboss/infinispan/cache/containerName/cacheName")
Cache<?, ?> namedCache;

Alternatively, you could obtain the cache via its cache container, but you must also ensure that the associated cache configuration is installed.
e.g.
Cache<?, ?> namedCache;
Cache<?, ?> defaultCache;
@Resource(lookup = "java:jboss/infinispan/container/containerName")
EmbeddedCacheManager manager;
@Resource(lookup = "java:jboss/infinispan/configuration/containerName/default")
Configuration defaultConfiguration;
@Resource(lookup = "java:jboss/infinispan/configuration/containerName/cacheName")
Configuration namedConfiguration;

@PostConstruct
public void init() {
    this.defaultCache = this.manager.getCache();
    this.namedCache = this.manager.getCache("cacheName");
Reply all
Reply to author
Forward
0 new messages