Let’s quickly look at the Java EE 8-JCache bridge project. This is another concrete step as far as Java EE 8 integration efforts are concerned..
Background
Details on the general idea behind this can be picked up from one of my recent posts on this topic
A bridge ?? What for ?
- Serve as the integration point for JCache & Java EE 8
- Provide decoupling: the Java EE 8 platform and the JCache API can be allowed to evolve separately and the bridge would provide a much needed cushion. Think of it as a regular Bridge Pattern (from the GoF) – just implemented in the form of a JSR 😉
Goals
- Derive from exiting (and popular) specifications like JPA which can make it easier to adopt and use
- Standardise a JCache descriptor (configuration)
- Implement Cache injection
- Look at support for custom cache properties (which vary across different providers)
- etc…..
Crossing the bridge
Here is a rather simple example. Its a Java EE 7 Maven project on GitHub. You should be able to pull in into any IDE (Netbeans recommended) and get going
All it does is
- Defines a named cache in cache.xml placed within WEB-INF/classes/META-INF (this is a pre-requisite for WAR based artifacts)
- Inject the cache in a JAX-RS resource
- Save the JSR info in the cache for future use (if it already does not exist there)
- It provides you JSR information e.g. http://localhost:9000/jcachejavaee8bridge/370 and tells you whether it has been picked up from the cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("{jsrNum}") | |
public class JavaEE8Resource { | |
@Inject | |
@CacheContext("jsrinfocache") | |
Cache<String, String> cache; | |
….. | |
@GET | |
public Response getinfo(@PathParam("jsrNum") String jsrNum) { | |
String resp = null; | |
if(!cache.containsKey(jsrNum)){ | |
//do something ! | |
}else{ | |
//use the cache…. ! | |
} | |
return Response.ok(resp).build(); | |
} | |
….. |
Other notable points
- The cache bridge API source has been included directly into the sample project to make dependency management easier (since its not in Maven yet). You might want to have the latest fork for your experiments
- The JCache RI is being used by internally by the bridge. Experiment with another JCache provider (Hazelcast, Infinispan etc.), include its dependency and include it in the cache.xml configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<caches> | |
<caching-provider>com.hazelcast.cache.HazelcastCachingProvider</caching-provider> | |
<cache name="jsrinfocache"> | |
<configuration> | |
<property name="store.by.value" value="true"/> | |
<property name="management.enabled" value="false"/> | |
<property name="statistics.enabled" value="true"/> | |
</configuration> | |
</cache> | |
<cache name="anotherone"> | |
<configuration> | |
<property name="store.by.value" value="false"/> | |
<property name="management.enabled" value="true"/> | |
<property name="statistics.enabled" value="false"/> | |
</configuration> | |
</cache> | |
</caches> |
How can you contribute ?
- Explore the project
- Provide ideas, files issues
- Write tests, break stuff, file some more issues !
- Get in touch with Adam Bien, translate your ideas into code and submit a PR…
Cheers!
Pingback: Java EE 8 - Utilizando o JCache para suas aplicações -
Pingback: Java EE 8 – Utilizando o JCache para suas aplicações – Agency Major