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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Data
  4. Using JCache Event Listeners, Loaders, and Writers

Using JCache Event Listeners, Loaders, and Writers

Here's a quick tutorial on event listeners that covers read-through and write-through when combining them when loaders and writers.

Abhishek Gupta user avatar by
Abhishek Gupta
CORE ·
Oct. 14, 16 · Tutorial
Like (5)
Save
Tweet
Share
8.06K Views

Join the DZone community and get the full member experience.

Join For Free

This blog will:

  • Zoom through JCache events, loaders, and writers.
  • Look at scenarios when they are used together.

You can download the sample maven project from this GitHub repo. It consists of:

  • A simple listener with read-through and write-through examples.
  • Some test cases to demonstrate the point.

Reacting to Cache Events and External Store Integration

JCache defines cache event listeners and cache loader/writer features as follows:

Event Listeners

  • React to cache events: create, update, delete, expiration.
  • Triggered as a result of many cache actions (methods on javax.cache.Cache) e.g. get, put, remove, etc.

Integrations

  • Provide transparent ways to integrate the cache with external stores.
  • Ability to implement this in the form of a cache loader (read from an external source if the key is not present in the cache) and cache writer (updates the external store in case of new entry, update, or delete)
  • Caches which implement this are also known as read through and/or write through

When They Are Used Together…

Let’s look at the sequence of events when everything works in tandem:

Loaders

An example where a key (which did not previously exist) is looked up from the cache is:

cache-reader-listerner-sequence

Writers

An example where a new key-value pair is inserted in the cache looks like this:

cache-writer-listerner-sequence

Things work similarly in a case of updated or removed cache entries.

A Few Things Worth Noting

  • Event listeners are synchronous: They are invoked in the same thread as the caller (e.g. the one invoking the get operation on the cache). Thus, that value can’t be returned to a caller while the event listener is still executing
  • Exceptions during cache entry listener invocation have no impact on the cache itself since it (the cache) has already undergone changes (create/update/delete)
  • One should not invoke cache operations from event listener callbacks, loader, or writer implementations – check spec page 133 (#10).
Event Loader (equipment) article writing Cache (computing)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • API Design Patterns Review
  • What Is a Kubernetes CI/CD Pipeline?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: