Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Using embedded infinispan in Wildfly

47 views
Skip to first unread message

Dalibor Kalna

unread,
May 1, 2025, 12:10:26 PMMay 1
to WildFly
Hi there

I have tried following example from Wildfly testsuite: CDI-Embedded

It works great in arquillian test, but as soon as I deploy it as WAR on Wildfly I get following error: 

ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "cache-demo-1.0-SNAPSHOT.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"cache-demo-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Cache<String, String> with qualifiers @GreetingCache
  at injection point [BackedAnnotatedField] @Inject @GreetingCache private ch.kalna.cachedemo.deployment.GreetingService.cache
  at ch.kalna.cachedemo.deployment.GreetingService.cache(GreetingService.java:0)

Any idea why this is not working?
 
Thank you and best regards
Dalibor 

Stephen Sill II

unread,
May 1, 2025, 12:27:59 PMMay 1
to WildFly
you have to do some configuration to do that.  I do this in a few of my war applications.  let me find you the magic

Stephen Sill II

unread,
May 1, 2025, 12:37:21 PMMay 1
to WildFly
actually the one project I had where I was doing has been moved to doing it in an ejb that's called from the war.

the war version is similar though

in my ejb-jar.xml under the <session> element you can put something like this
            <resource-ref>
                <res-ref-name>infinispan/RateLimitCache</res-ref-name>
                <lookup-name>java:jboss/infinispan/cache/auth-cache-container/rate-limit-cache</lookup-name>
            </resource-ref>

Then in your ejb you can inject an instance
    @Resource(lookup = "infinispan/RateLimitCache")
    private Cache<String, String> rateLimitCache;

As I recall though a strictly war implementation, no ejbs, was similar.

Stephen Sill II

unread,
May 4, 2025, 12:52:59 PMMay 4
to WildFly
ok I actually found that old war project that I had rigged up to use an embedded infinispan cache.  Hope it helps.

The subsequent snippets assume you've configured your cache this way via jboss-cli/whatever

/subsystem=infinispan/cache-container=vaadin-cache-container:add(marshaller=JBOSS)
/subsystem=infinispan/cache-container=vaadin-cache-container/transport=jgroups:add()
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache:add()
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=expiration:add(max-idle=300000)
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=locking:add()
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=locking:write-attribute(name=isolation, value=REPEATABLE_READ)
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=locking:write-attribute(name=acquire-timeout, value=5000L)
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=transaction:add()
/subsystem=infinispan/cache-container=vaadin-cache-container/replicated-cache=vaadin-session-cache/component=transaction:write-attribute(name=mode, value=BATCH)
/subsystem=infinispan/cache-container=vaadin-cache-container:write-attribute(name=default-cache, value=vaadin-session-cache)

I ended up putting this in my src/main/webapp/WEB-INF/web.xml
    <resource-env-ref>
        <resource-env-ref-name>vaadinSessionCache</resource-env-ref-name>
        <lookup-name>java:jboss/infinispan/cache/vaadin-cache-container/vaadin-session-cache</lookup-name>
    </resource-env-ref>



This in cat src/main/webapp/WEB-INF/jboss-deployment-structure.xml
<jboss-deployment-structure>
   <deployment>
      <dependencies>
         <module name="org.infinispan" export="TRUE" />
      </dependencies>
   </deployment>
</jboss-deployment-structure>

Then create a CacheService.java
@Named
public class CacheService
{

    @Resource(name = "vaadinSessionCache")
    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 then use the CacheService in your JAX-RS endpoint class

    @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();
    }

Dalibor Kalna

unread,
May 5, 2025, 2:25:59 AMMay 5
to WildFly
Dear Stephen

Thank you very much for your time and effort to explain how this could be done. I have succesfully implemented it and it works flawlesly.
I was just somehow hoping, one can do this with CDI only, that is, without web.xml, jboss-deployment-structure.xml. But looks like it has
to be this way.

Thank you and best regards
Dalibor

Stephen Sill II

unread,
May 5, 2025, 7:33:10 AMMay 5
to WildFly
Glad that worked for you.  I don't know if that's the RIGHT way to do it,  but that's how I ended up getting it to work.
Reply all
Reply to author
Forward
0 new messages