2

Can any one point to a good implementation, if one exists, of what I call Ticking collection/Map in Java. Where the elements in the collection has some expiry time. When a particular element of the collection is expired then collection raises a certain kind of alarm or call a handler.

I saw a Guava implementation of an expiring map which automatically removes the key which has been expired.

Expiring map

Community
  • 1
  • 1
mawia
  • 9,169
  • 14
  • 48
  • 57
  • So, why isn't the Guava implementation acceptable to you? – Gilbert Le Blanc May 08 '12 at 15:16
  • "I saw a Guava implementation of an expiring map which automatically removes the key which has been expired." Isn't that I said I need one which raise alarm on expiry of the element rather than removing them.Sure if it has that capability I will use it, If you can let me know if it has that capability. – mawia May 08 '12 at 15:44
  • 1
    How about registering a [removalListener](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html#removalListener%28com.google.common.cache.RemovalListener%29) to be informed of the items that expire? – assylias May 08 '12 at 16:27

1 Answers1

4

guava supports a callback on eviction:

    Cache<String, String> cache = CacheBuilder.newBuilder()
            .expireAfterAccess(100, TimeUnit.SECONDS)
            .removalListener(new RemovalListener<Object, Object>() {
                public void onRemoval(RemovalNotification<Object, Object> objectObjectRemovalNotification) {
                    //do something
                }
            })
            .build();
Jonas Adler
  • 905
  • 5
  • 9
  • oh, sorry i didn't see assylias comment! – Jonas Adler May 08 '12 at 16:37
  • No problem - I was not sure it solves the issue as somewhere in the javadoc it says that the map entry might be null if it has been GC'ed + if the OP wants to put the item back into the map, there is a short time window during which the entry is not in the map any longer. – assylias May 08 '12 at 17:35
  • Wouldn't that only apply if you use weak keys/values? I need to investigate more... – Jonas Adler May 08 '12 at 21:50
  • That's what I would have thought but I am not sure. – assylias May 08 '12 at 21:51
  • Humbled by your reply.Many thanks!Can you please let me know what exactly is the term of use for google provided libraries. Will it be fine to use it in enterprise application without voilating any terms of use. – mawia May 14 '12 at 22:59
  • Guava is distributed under Apache License 2.0: http://www.apache.org/licenses/LICENSE-2.0, I don't know the specifics, so i guess you'll have to read over it ;) – Jonas Adler May 15 '12 at 21:28