DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Is your software supply chain secure? Calling all security savants to share your experiences, tips, and insights with our dev community!

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Related

  • A Systematic Approach for Java Software Upgrades
  • Implementation Best Practices: Microservice API With Spring Boot
  • The Next Evolution of Java: Faster Innovation, Simpler Adoption
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • The Role of Functional Programming in Modern Software Development
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • Java Virtual Threads and Scaling
  • Debugging Core Dump Files on Linux - A Detailed Guide
  1. DZone
  2. Coding
  3. Java
  4. Sneak Peek into the JCache API (JSR 107)

Sneak Peek into the JCache API (JSR 107)

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Feb. 23, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

This post covers the JCache API at a high level and provides a teaser – just enough for you to (hopefully) start itching about it ;-)

In this post ….

  • JCache overview
  • JCache API, implementations
  • Supported (Java) platforms for JCache API
  • Quick look at Oracle Coherence
  • Fun stuff – Project Headlands (RESTified JCache by Adam Bien) , JCache related talks at Java One 2014, links to resources for learning more about JCache

What is JCache?

JCache (JSR 107) is a standard caching API for Java. It provides an API for applications to be able to create and work with in-memory cache of objects. Benefits are obvious – one does not need to concentrate on the finer details of implementing the Caching and time is better spent on the core business logic of the application.

JCache components

The specification itself is very compact and surprisingly intuitive. The API defines high level components (interfaces) some of which are listed below

  • Caching Provider – used to control Caching Managers and can deal with several of them,
  • Cache Manager – deals with create, read, destroy operations on a Cache
  • Cache – stores entries (the actual data) and exposes CRUD interfaces to deal with the entries
  • Entry – abstraction on top of a key-value pair akin to a java.util.Map
jcache-high-level-components

Hierarchy of JCache API components

JCache Implementations

JCache defines the interfaces which of course are implemented by different vendors a.k.a Providers.

  • Oracle Coherence
  • Hazelcast
  • Infinispan
  • ehcache
  • Reference Implementation – this is more for reference purpose rather than a production quality implementation. It is per the specification though and you can be rest assured of the fact that it does in fact pass the TCK as well

From the application point of view, all that’s required is the implementation to be present in the classpath. The API also provides a way to further fine tune the properties specific to your provider via standard mechanisms.

You should be able to track the list of JCache reference implementations from the JCP website link

public class JCacheUsage{
    public static void main(String[] args){
        //bootstrap the JCache Provider
        CachingProvider jcacheProvider = Caching.getCachingProvider();
        CacheManager jcacheManager = jcacheProvider.getCacheManager();
        //configure cache
        MutableConfiguration<String, Integer> jcacheConfig = new MutableConfiguration<>();
        jcacheConfig.setTypes(String.class, MyPreciousObject.class);
        //create cache
        Cache<String, MyPreciousObject> cache = jcacheManager.createCache("PreciousObjectCache", jcacheConfig);
        //play around
        String key = UUID.randomUUID().toString();
        cache.put(key, new MyPreciousObject());
        MyPreciousObject inserted = cache.get(key);
        cache.remove(key);
        cache.get(key); //will throw javax.cache.CacheException since the key does not exist
    }
}

JCache provider detection

  • JCache provider detection happens automatically when you only have a single JCache provider on the class path
  • You can choose from the below options as well
//set JMV level system property
-Djavax.cache.spi.cachingprovider=org.ehcache.jcache.JCacheCachingProvider
 
//code level config
System.setProperty("javax.cache.spi.cachingprovider","org.ehcache.jcache.JCacheCachingProvider
 
//you want to choose from multiple JCache providers at runtime
CachingProvider ehcacheJCacheProvider = Caching.getCachingProvider("org.ehcache.jcache.JCacheCachingProvider");
 
//which JCache providers do I have on the classpath?
Iterable<CachingProvider> jcacheProviders = Caching.getCachingProviders();

Java Platform support

  • Compliant with Java SE 6 and above
  • Does not define any details in terms of Java EE integration. This does not mean that it cannot be used in a Java EE environment – it’s just not standardized yet.
  • Could not be plugged into Java EE 7 as a tried and tested standard
  • Candidate for Java EE 8

Project Headlands: Java EE and JCache in tandem

  • By none other than Adam Bien himself !
  • Java EE 7, Java SE 8 and JCache in action
  • Exposes the JCache API via JAX-RS (REST)
  • Uses Hazelcast as the JCache provider
  • Highly recommended !

Oracle Coherence

This post deals with high level stuff w.r.t JCache in general. However, a few lines about Oracle Coherence in general would help put things in perspective

coherence-logo

  • Oracle Coherence is a part of Oracle’s Cloud Application Foundation stack
  • It is primarily an in-memory data grid solution
  • Geared towards making applications more scalable in general
  • What’s important to know is that from version 12.1.3 onwards, Oracle Coherence includes a reference implementation for JCache (more in the next section)

JCache support in Oracle Coherence

  • Support for JCache implies that applications can now use a standard API to access the capabilities of Oracle Coherence
  • This is made possible by Coherence by simply providing an abstraction over its existing interfaces (NamedCache etc). Application deals with a standard interface (JCache API) and the calls to the API are delegated to the existing Coherence core library implementation
  • Support for JCache API also means that one does not need to use Coherence specific APIs in the application resulting in vendor neutral code which equals portability
    How ironic – supporting a standard API and always keeping your competitors in the hunt ;-) But hey! That’s what healthy competition and quality software is all about !
  • Talking of healthy competition – Oracle Coherence does support a host of other features in addition to the standard JCache related capabilities.
  • The Oracle Coherence distribution contains all the libraries for working with the JCache implementation

coherence_lib

  • The service definition file in the coherence-jcache.jar qualifies it as a valid JCache provider implementation

service-definition

Curious about Oracle Coherence ?

  • Quick Starter page
  • Documentation
  • Installation
  • Further reading about Coherence and JCache combo – Oracle Coherence documentation

JCache at Java One 2014

Couple of great talks revolving around JCache at Java One 2014

  • Come, Code, Cache, Compute! by Steve Millidge
  • Using the New JCache by Brian Oliver and Greg Luck

Hope this was fun :-)

Cheers !

API Oracle Coherence Coherence (units of measurement) Java EE Java (programming language) application Implementation Cache (computing) Peek (software)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Systematic Approach for Java Software Upgrades
  • Implementation Best Practices: Microservice API With Spring Boot
  • The Next Evolution of Java: Faster Innovation, Simpler Adoption
  • Effective Java Collection Framework: Best Practices and Tips

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!