[infinispan-dev] Async Notification API

Elias Ross genman at noderunner.net
Fri May 15 20:00:26 EDT 2009


On Fri, May 15, 2009 at 4:27 PM, Manik Surtani <manik at jboss.org> wrote:

> While the new async API has been getting rave reviews, someone did bring up
> async notifications.  E.g.,
>
>        Future f = cache.putAsync(k, v);
>        f.get();
>
> But what if I don't want to wait on f.get(), I'd rather be told when the
> future is done so I can do an f.get()?

Kind of hard to understand the use case here. There's Future.isDone()
and a Future.get() with timeout.

I think the biggest problem I see with the Async design is you're not
sure which order multiple operations will complete in. For instance,
if you are doing two puts you don't necessary know which order they
might complete in, but you want to know when they might complete.

Which may require an API redesign...

To me, the API is good but not great. I'd be possibly better to make
use of the java.util.concurrent.CompletionService interface and have
the mutation operations, like put and remove simply be static builder
methods that return Callable/Runnable instances.

Cache cache;
CompletionService c = cache.getCompletionService();
c.submit(CacheOperation.put( key, value )); // replace with static import
c.submit(CacheOperation.put( key2, value2 ));
// we're not sure which operation will complete first...
Future f1 = c.poll();
Future f2 = c.poll();

You'd probably need to have every call to "getCompletionService"
return a new instance. This is so multiple users don't see completed
tasks.

If you want a notification handler, simply tell users to create a
thread that polls on operations completed.

And the really good news here is you can add additional cache
operations without having to change an interface.

>
> Another approach is to design a sub-interface to Future.  NotifyingFuture.
>
> public interface NotifyingFuture extends Future {
>  void addListener(NotifyingFutureListener l);
> }
>
> public interface NotifyingFutureListener {
>  void futureDone(Future f);
> }

I think this, again, is best done using CompletionService.




More information about the infinispan-dev mailing list