RemovalListener problem

471 views
Skip to first unread message

andre.rigon

unread,
Mar 9, 2012, 12:40:24 PM3/9/12
to guava-discuss
Hi folks,

I'm using a RemovalListener, but it does not get called. The entries
from the cache are removed, but
my removel logic never gets invoked. Any help?

I'm using a cache configured as follows:

CacheBuilder.newBuilder().expireAfterAccess(duration,
unit).removalListener(fileDeleteRemovalListener())
.build();

and this is my RemovalListener:

private RemovalListener<String, String> fileDeleteRemovalListener() {
return new RemovalListener<String, String>() {

@Override
public void onRemoval(RemovalNotification<String, String> not) {

try {
FileUtils.forceDelete(new File(not.getValue()));
} catch (IOException e) {
Log.error("could not delete file " + not.getValue(), e);
}
}
};
}

Thanks!

Charles Fry

unread,
Mar 9, 2012, 12:45:43 PM3/9/12
to andre.rigon, guava-discuss
Did you add logging to onRemoval to see if it was getting called?


André Gonçalves

unread,
Mar 9, 2012, 12:48:23 PM3/9/12
to Charles Fry, guava-discuss
Hello Charles, thanks for the answer.

I tried to log, bot nothing happens. It doesnt get called at all.

Kevin Bourrillion

unread,
Mar 9, 2012, 12:50:13 PM3/9/12
to André Gonçalves, Charles Fry, guava-discuss
It would help to attach a complete compilable and runnable program (simple and self-contained) -- thanks!
--
Kevin Bourrillion @ Google
Java Core Libraries Team
http://guava-libraries.googlecode.com

Charles Fry

unread,
Mar 9, 2012, 12:50:53 PM3/9/12
to André Gonçalves, guava-discuss
If this is in a test case you might need to call Cache.cleanUp(). 

André Gonçalves

unread,
Mar 9, 2012, 1:01:25 PM3/9/12
to Charles Fry, guava-discuss
Charles,

I put an cache.cleanUp(); after a .put() call, and stoped the debug and waited until the timeout ends.
That way, the listener fired up.
Why this behavior?

thanks in advance,

Andre

2012/3/9 Charles Fry <f...@google.com>

Charles Fry

unread,
Mar 9, 2012, 1:15:14 PM3/9/12
to André Gonçalves, guava-discuss
As explained in the CacheBuilder javadoc:

"If expireAfterWrite or expireAfterAccess is requested entries may be evicted on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(). Expired entries may be counted inCache.size(), but will never be visible to read or write operations."

The point is that we don't run any background cleanup threads, so need to hook onto user requests. And for scalability reasons we do that on a per-segment basis.

In a production environment it will all just work out. You just need to do extra stuff (like calling cleanUp explicitly) when you're writing tests.

Charles

André Gonçalves

unread,
Mar 9, 2012, 1:20:32 PM3/9/12
to Charles Fry, guava-discuss
Thanks for the help!

2012/3/9 Charles Fry <f...@google.com>
Reply all
Reply to author
Forward
0 new messages