Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building the JCache Java EE 8 Bridge...One Brick at a Time

DZone's Guide to

Building the JCache Java EE 8 Bridge...One Brick at a Time

All in all it's just another brick in the JCache Java EE 8 bridge.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 existing (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. It's 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 in 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 also scolds you if you ask for it again and again since it has to access the cache of course ;-)
@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 it's 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
<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!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,jcache ,javaee

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}