WildFly 10, JCache - method caching - 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 :
excluded tomcat
exluded spring-boot-starter-cache
added infinispan-jcache (because i want to use cache configured / managed by wildfly in standalone/domain.xml)
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 :
It is possible to use JCache method caching on WildFly 10 using Infinispan managed by WildFly ?
Or Infinispan should be used for method caching like JCache, hence JCache has "more functionality" than Infinispan.
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 ...

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.

Related

Why would Spring Boot/EhCache NOT use the specified configuration file?

I'm trying to enable EhCache 3 caching inside a Spring Boot 2 application and as near as I can tell I've set this up correctly but caching is just...NOT working and I'm being given no information as to why.
I've added to my maven POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
I've added an #EnableCaching annotation.
I've specified the config, I'm using bootstrap.yml so:
spring:
cache:
jcache:
config: classpath:ehcache.xml
But just in case I also put the spring.cache.jcache.config version in a properties file.
I've written a basic src/main/resources/ehcache.xml file...
Finally, I've set a method as #Cacheable but it errors out when I call it with "Cannot find cache named..."
It literally doesn't matter one iota what I write in ehcache.xml. I can paste in Lorem Ipsum text instead of XML and have no errors or indication it even opened the file.
Spring Boot itself seems to have found the appropriate pre-requisites for JSR 107 autoconfiguration:
JCacheCacheConfiguration matched:
- #ConditionalOnClass found required classes 'javax.cache.Caching', 'org.springframework.cache.jcache.JCacheCacheManager' (OnClassCondition)
- Cache org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration automatic cache type (CacheCondition)
I've tried going back to EhCache 2, so I replaced org.ehcache with net.sf.ehcache, switched my YML to ehcache instead of jcache and removed javax.cache. This puts this into my auto-configuration report:
EhCacheCacheConfiguration matched:
- #ConditionalOnClass found required classes 'net.sf.ehcache.Cache', 'org.springframework.cache.ehcache.EhCacheCacheManager' (OnClassCondition)
- Cache org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration automatic cache type (CacheCondition)
- ResourceCondition (EhCache) found resource 'classpath:/ehcache.xml' (EhCacheCacheConfiguration.ConfigAvailableCondition)
But I still don't have any caches available to me. Why would Spring's autoconfiguration mechanism go so far as to mention that it found the EhCache 2 config file but not actually configure anything? Is there some other auto-configuration class I need to also look up in the report like a GenericCache, CacheConfiguration, something else?
What I can't see is any indication that it tried to do anything to start up a caching subsystem, read my configs, did anything.
As convenient as autoconfiguration is, when it doesn't feel like doing the thing it frustratingly tells me absolutely nothing about why it's deciding to blow me off so I could really use some help figuring out how to tease those details out of the thing.
Did I miss something? What can I debug and where can I look to figure out why it's ignoring the XML configuration I'm trying to tell it about? I tried adding some breakpoints to Spring CacheProperties around the jcache settings but it didn't hit them when I attached the debugger.

Spring Infinispan - Setting expiry time for cached objects

We are currently using below. It's quite old, but cannot upgrade to higher version now
`
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring3</artifactId>
<version>6.4.0.Final-redhat-4</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
<version>6.4.0.Final-redhat-4</version>
</dependency>
`
We have modified our code from something with a direct JDG implementation (as shown below) to SpringRemoteCacheManager in an XML based configuration file and are using Spring cache:advice to define cacheable, cahce-put, cache-evict methods.
See Current code where we have control to add expiry time. We want to do similar thing with Spring - Infinispan as well. With Spring - Infinispan we do not write any application code that puts/gets objects in/from cache as its handled by Spring annotations (#Cacheable / #CachePut)
Appreciate if anyone can provide any pointers
RemoteCache<Object, Object> cache = jdgRemoteCacheManager.getCache(cacheName);
cache.put(keyName, object, 15, TimeUnit.MINUTES);
Unfortunately Spring Cache support doesn't provide such methods (see the Javadocs). So it seems the only way is to use the RemoteCache API provided by Infinispan.
Perhaps you could implement your own #Cacheable annotation and implement all the functionality you need. But that's a Spring question really...

spring boot 1.3.1 devtools hot reload not work if your project name is spring-boot

Spring boot 1.3.1.RELEASE, used devtools.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Now I add some code in controller method, e.g.
logger.info("delete {} ", name);
but in eclipse console, nothing output, and access this url , the new add logger did not output. So why is so? why hot reload not work?
I found something could explain this phenomenon. Because my project name is spring-boot, and run Application class in eclispe you could see:
you could see after Application name appended (1)
I don't know what's this mean.
After rename project name to anything else(e.g. spring-boot-reservation), now I found devtools could work normally.
And this time the number after Application name is changed to 2,

Spring-Social-facebook + Spring MVC integration

I am trying to access facebook data via spring social facebook integration using the instructions provided at http://spring.io/guides/gs/accessing-facebook.
But currently i am facing 2 type of problem
When i run example as mentioned in tutorial i get following error
No matching bean of type [org.springframework.social.facebook.api.Facebook] found for dependency
When i run this with #Configuration on FacebookConfig class, i get below mentioned error
A ConnectionFactory for provider 'facebook' has already been registered
Is there a workaround to it?
I have kept the war file with source code at https://skydrive.live.com/redir?resid=EA49CD7184E0E40!168&authkey=!AIkoKKx5-Um8AQE
What version are you using?
Try using the version 1.1.0.RELEASE
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
If it not works, please try post the stacktrace printed.
You need to create a the beans to your class, please post more information like your pom.xml and your spring context configuration.
Ihad the same problem. Spring Social Facebook will automatically add connection factory based on the configuration in application.properties. This auto-configured connection factory is clashing with the one that you're trying to add. Try just to remove your connection factory you add through addConnectionFactories.
Try to use different setting to load your custom connection factory...
E.g. Instead of using OOTB keys use different keys:
#Facebook Social App Details:
# Commented below 2 OOTB Keys & Bingo it worked.
#spring.social.facebook.appId=APP_ID
#spring.social.facebook.appSecret=APP_SECRET
facebook.app.id=APP_ID
facebook.app.secret=APP_SECRET
This will resolve your problem.

using javax.cache.CacheManager with EhCache

I am trying to use javax.cache.CacheManager JSR107 API using EhCache as caching solutioarin provider. But I am unable to find any such resources.
As per the link at http://ehcache.org/documentation/integrations/jsr107 , it says that ehcache jsr107 is still in draft phase. Can any one please confirm if it's still the case?
Any sample code to use net sf cacheManager using JSR107 javax.cache.* classes?
Thanks,
Harish
As specified in this page:
Because JCACHE has not yet been released the JCACHE API that Ehcache
implements has been released as net.sf.jsr107cache.
This effort can be found in the Github repository. If you see JCacheManager implements javax.cache.CacheManager
Here's a better answer now the API is finalised. Stick this in your pom:
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>jcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.jsr107.ri</groupId>
<artifactId>cache-annotations-ri-guice</artifactId>
<version>1.0.0</version>
</dependency>
And you're off. Annotate with any of the annotations like this: (good luck finding the javadocs!)
#CacheResult(cacheName = "monthly")
public List<QueryResult> monthly(String prefix) {
//...
}
I agree though, the documentation sucks.
Here's more javax.cache info if you're interested.

Resources