mawia
mawia

Reputation: 9349

Expiring Map with signaling capability when element expires

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

Upvotes: 2

Views: 1525

Answers (1)

Jonas Adler
Jonas Adler

Reputation: 913

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();

Upvotes: 4

Related Questions