It seems JSR 107 is not going to make it as part of Java EE 7. Any change on this?
Too bad.
A while back, and I have not found the email, we talked about having callback interfaces in addition to the Future versions of APIs (to for example prevent blocking in large batch loops where you want to track the return but your behavior is not going to change other than logging that a key population failed).
I have been checking
https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Cache.java
from time to time, and see that this did not make it in there.
Future<V> load(K key);
Future<Map<K, ? extends V>> loadAll(Set<? extends K> keys);
The websockets JSR made up their own callback for a similar area where one might not want blocking.
We could use this from NIO:
http://docs.oracle.com/javase/7/docs/api/java/nio/channels/CompletionHandler.html
The CompletionHandler is similar to a future, but it calls your code when the task is complete instead of you calling it.
void load(K key, CompletionHandler <V,A> callMeWhenDone);
void loadAll(Set<? extends K> keys, CompletionHandler <V,A> callMeWhenDoneEachTime);
Callback methods:
void completed(V result, A attachment)
void failed(Throwable exc, A attachment)
Also I can see more use of Futures and CompletionHandler where you would like to do something with the cache (put an item), but do not want to block, but still need to know if something bad happen (e.g. log it), but you can do this on a foreign thread, i.e., you don't have to block.
Future<V> putWhenYouCan(K key, V value);
void put(K key, V value, CompletionHandler <V,A> callMeWhenDone);
The CompletionHandler allows you to have some sort of remediation of failure, but still not block
We could even remove the Future from the API and create a utility class that converts a Future wrapper into a CompletionHandler.
Hi all,
My name is Galder Zamarreño and I work on the Infinispan project (alongside Manik Surtani).
Also I can see more use of Futures and CompletionHandler where you would like to do something with the cache (put an item), but do not want to block, but still need to know if something bad happen (e.g. log it), but you can do this on a foreign thread, i.e., you don't have to block.
Future<V> putWhenYouCan(K key, V value);
In Infinispan we have a slightly similar method called:
void putForExternalRead(K key, V value);
The idea of this put is to store data in the cache read from another persistent store efficiently. We use it extensively in the Hibernate second level cache to cache data read from the database. This put fails fast (waits a minimum time to acquire the lock) and silently (exception is not propagated to client) if another thread is trying to store the same data at the same time.
This performs particularly well when you have N threads reading the same thing from the database and they're trying to put it in the cache. If they were to use a normal put, you could have up to N-1 threads waiting to acquire the lock to update the same thing. With PFER, this wait is minimal, and eventually one of them will store the data in the cache and will be accessible to others.
It has other consequences for distributed caches, but this is not of interest for this list. Detailed javadoc information can be found in
http://docs.jboss.org/infinispan/5.2/apidocs/org/infinispan/Cache.html#putForExternalRead(K, V)
For our next version, we actually wanna change the signature slightly (https://issues.jboss.org/browse/ISPN-1986) and instead have:
boolean putForExternalRead(K key, V value)
I wonder what the rest of the list thought of such operation for inclusion in JSR-107. This operation would be extremely valuable for a potential JSR-107 implementation of Hibernate Second Level Cache.
Cheers,
--
You received this message because you are subscribed to the Google Groups "jsr107" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsr107+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hi Galder,It's an interesting idea. I'd certainly like to know more about the semantics.eg: If there are N threads reading the same entry using a Cache that is configured for "read-through", do all N threads hit the database, potentially reading the same value, and then only one places the value in the cache (if they are all the same)?
Wouldn't that then reduce the value of having a cache in the first place? I'm guessing I'm missing something here. Sorry :(
What happens if the database changes during the N threads reading? How is consistency maintained? Perhaps this style doesn't allow any consistency? ie: last write wins?
Would it be possible to have a more detailed example?