Mato.Duris
Mato.Duris

Reputation: 253

WildFly 10, JCache - method caching

i have simple application using Spring Boot. I wanted allow method caching with JSR107 - JCache. So with help of tutorial i put together this code :

@CacheResult(cacheName = "testpoc")
public Country getCountry(Integer id){
    System.out.println("---> Loading country with code '" + id + "'");
    return new Country(id, "X", "Title");
}

with this POM file

...
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.4.0.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-cache</artifactId>
         <version>1.4.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>
...

(dependency 'spring-boot-starter-web' is there for simple REST service which call getCountry method)

Everything works like documentations says - method is invoked only once.

Now i wanted to try it on WildFly 10 application server

I have modified pom file :

Check pom file here on pastebin.

Problem is, that i am receiving following error : Cannot find cache named 'java:jboss/infinispan/app-cache'

(i have tried to use both JNDI assigned and name to infinispan cache configured in wildfly).

Following code created Cache object (so i can used it) :

CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = cacheManager.createCache("testpoc", new MutableConfiguration<String, String>());

Question :

Thank you very much

PS :It is not problem for me to put whole code on github and post link - it is few lines of code ...

Upvotes: 0

Views: 894

Answers (1)

Sebastian Łaskawiec
Sebastian Łaskawiec

Reputation: 2737

There are a couple of problems with your approach so let me go through them in steps.

At first you need to use proper Infinispan setup. Infinispan bits shipped with WF should be considered as internal or private. In order to use Infinispan in your app properly - either add org.infinispan:infinispan-embedded to your deployment or install Infinispan Wildfly modules. You can find installation guide here (it's a bit outdated but still, the procedure is exactly the same - unpack modules into WF and use Dependencies MANIFEST.MF entry).

Once you have successfully installed Infinispan (or added it to your app), you need to consider whether you want to use Spring Cache or JCache. If you're only interested in using annotations - I would recommend the former since it's much easier to setup (all you need to do is to add @EnableCaching to one of your configurations). Finally with Spring Cache you will create an Infinispan CacheManager bean. An example can be found here.

Final note - if you still need to use JCache - use this manual to setup Caching Provider.

Upvotes: 3

Related Questions