About Greg Luck

How JSR107 Caching Annotations are meant to be used

I am getting a few questions lately on JSR107 caching annotations and whether implementations of JSR107 are providing them.

Caching annotations can be added to your Java classes and will invoke caching operations as the method. For example below is an annotated BlogManager.
 
 
 
 
 
 

@CacheDefaults(cacheName = "blgMngr")
public class BlogManagerImpl implements BlogManager {private static Map<String, Blog> map = new HashMap<String, Blog>();@CacheResult
public Blog getEntryCached(String title) {
return map.get(title);
}

public Blog getEntryRaw(String title) {
return map.get(title);
}

/**
* @see manager.BlogManager#clearEntryFromCache(java.lang.String)
*/
@CacheRemove
public void clearEntryFromCache(String title) {
}

public void clearEntry(String title) {
map.put(title, null);
}

@CacheRemoveAll
public void clearCache() {
}

public void createEntry(Blog blog) {
map.put(blog.getTitle(), blog);
}

@CacheResult
public Blog getEntryCached(String randomArg, @CacheKey String title,
String randomArg2) {
return map.get(title);
}

}

Caching annotations, though defined in JSR107, are not meant to be provided by a CachingProvider such as Hazelcast. Instead they must be provided by the dependency injection containers: Spring, Guice, CDI (for Java EE). This will happen with EE in 8 which is a couple of years away. Spring support is coming in 4.1 and is available now for developers to play with in snapshot. See https://spring.io/blog/2014/04/14/cache-abstraction-jcache-jsr-107-annotations-support for how to use it.

While it will take time for the DIs to add support, in the JSR107 RI we have written a module for each of them. That code can be added to your existing DI Container and it will enable caching annotations processing. See https://github.com/jsr107/RI/tree/master/cache-annotations-ri.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


one + 5 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books