JCache (JSR 107) Example

119 views
Skip to first unread message

Phillip Kruger

unread,
Sep 3, 2018, 2:16:56 AM9/3/18
to Thorntail
Hi All.

I am trying to get JCache (JSR 107) working on v2.1.0.

Does anyone have some examples / pointers ? Or even example to get infinispan going on thorntail.

Thanks

Phillip


Ken Finnigan

unread,
Sep 4, 2018, 8:11:33 AM9/4/18
to Thorntail
Hi Phillip,

https://github.com/wildfly-swarm-openshiftio-boosters/wfswarm-cache is an example of connecting to an external Infinispan cache, but I don't think we've got examples of Cache or Infinispan server usage.

Ken

Phillip Kruger

unread,
Sep 4, 2018, 9:36:11 AM9/4/18
to Thorntail
Thanks for the reply Ken. I'll have a look at that repo.

markus.s

unread,
Sep 6, 2018, 2:59:01 AM9/6/18
to Thorntail

Hello Phillip,

what worked for me is the following JCache/CDI based approach:

1) declaring basic infinispan dependencies (NOT io.thorntail):

<dependencyManagement>
   
<dependencies>
    ...
       
<dependency>
           
<groupId>org.infinispan</groupId>
           
<artifactId>infinispan-bom</artifactId>
           
<version>${version.infinispan}</version>
           
<scope>import</scope>
           
<type>pom</type>
       
</dependency>
    ...
   
</dependencies>
</dependencyManagement>

<dependencies>
    ...
   
<dependency>
     
<groupId>javax.cache</groupId>
     
<artifactId>cache-api</artifactId>
       
</dependency>
       
<dependency>
           
<groupId>org.infinispan</groupId>
           
<artifactId>infinispan-jcache</artifactId>
       
</dependency>
       
<dependency>
           
<groupId>org.infinispan</groupId>
           
<artifactId>infinispan-cdi-embedded</artifactId>
       
</dependency>
    ...
</dependencies>


2) Providing a global cache configuration as CDI producer


public class GlobalCacheConfig {

   
@Produces
   
public Configuration globalCacheConfiguration() {
       
return new ConfigurationBuilder().expiration().lifespan(60000, TimeUnit.SECONDS).build();
   
}

}

3) Declaring infinispan CDI interceptors in beans.xml

    <interceptors>
       
<class>org.infinispan.jcache.annotation.InjectedCacheResultInterceptor</class>
       
<class>org.infinispan.jcache.annotation.InjectedCachePutInterceptor</class>
       
<class>org.infinispan.jcache.annotation.InjectedCacheRemoveEntryInterceptor</class>
       
<class>org.infinispan.jcache.annotation.InjectedCacheRemoveAllInterceptor</class>
   
</interceptors>

4) Using JCache annotations api for caching (i.e. method results)

public class MyService {

   
@javax.cache.annotation.CacheResult
   
public MyObject load() {
     
...
       
return new MyObject();
   
}
}


What not worked:

declaring individual cache configurations with custom qualifier annotations like explained here. (@ConfigureCache("my-cache") ...)

Maybe some of the thornatil experts are having a hint for us...

KR
Markus

Phillip Kruger

unread,
Sep 6, 2018, 3:17:02 AM9/6/18
to Thorntail
Hi Markus.

This is awesome ! Thanks, I am going to try and get an example running on this. Will refer back once I have something.

Cheers

Phillip Kruger

unread,
Sep 10, 2018, 10:27:40 AM9/10/18
to Thorntail
Hi Markus (and others)

@Markus - your example got me going and I now have a working example.


(It's very easy to get this running on your PC - see the README)

I made some changes to your original suggestion, my thorntail use autodetection of the fractions, so as soon as I use Infinispan code, the infinispan fraction is included, so I made it provided in the pom.xml.

I go it working to cache on one thorntail server, and read the cache on the other server (distributed grid). The cache also expire after a minute, and is configured with infinispan.xml.

However, I could not get the notifications / events to work :(

For JCache (ideally what I would want to use) :

@Log @RequestScoped
public class CacheListener {

   
public void cacheEntryCreatedEvent(@Observes CacheEntryCreatedEvent event) {
       
System.out.println(">>>>>>> JCACHE >> New entry " + event.getKey() + " created in the cache");
   
}

   
public void cacheEntryEvent(@Observes CacheEntryEvent event) {
       
System.out.println(">>>>>>> JCACHE >> entry event " + event.getKey() + " [" + event + "]");
   
}
   
   
public void cacheEntryExpiredEvent(@Observes CacheEntryExpiredEvent event) {
       
System.out.println(">>>>>>> JCACHE >> Old entry " + event.getKey() + " expiring");
   
}
   
   
public void cacheStartedEvent(@Observes CacheStartedEvent event) {
       
System.out.println(">>>>>>> JCACHE >> New cache " + event.getCacheName() + " started");
   
}
}



For Infinispan:

@Listener (clustered = true)
public class PrintWhenAdded {
   
@CacheEntryCreated
   
public void print(CacheEntryCreatedEvent event) {
       
System.out.println(">>>>>>> INFINISPAN >> New entry " + event.getKey() + " created in the cache");
   
}
}



Any suggestions ? Should I rather as on the Infinispan group ?

Thanks in advance

markus.s

unread,
Sep 14, 2018, 5:30:05 AM9/14/18
to Thorntail
Hello Phillip,

have you found a solution for your listener problem ?

KR
Markus

Phillip Kruger

unread,
Sep 14, 2018, 5:48:02 AM9/14/18
to Thorntail
Hi Markus,

Not yet (for Infinispan) although I believe it's just a configuration that I am missing.

I also added Hazelcast to see if I can use JCache in Thorntail with Hazelcast IMDG, and there I could get the listeners to work.

I'll spend some more time on it this weekend and let you know. The latest source in the above mentioned Github project has got the working Hazelcast.

Thanks
Reply all
Reply to author
Forward
0 new messages