Skip to content

Instantly share code, notes, and snippets.

@dbrimley
Created November 26, 2016 15:02
Show Gist options
  • Save dbrimley/7c65b9bfd39fb9615334bc6f5e7703a6 to your computer and use it in GitHub Desktop.
Save dbrimley/7c65b9bfd39fb9615334bc6f5e7703a6 to your computer and use it in GitHub Desktop.
Expect throwing exception from the interceptRemove would short circuit the eviction.
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.MapInterceptor;
import java.util.concurrent.TimeUnit;
public class MapInterceptorMember {
public static void main(String[] args) throws InterruptedException {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hz.getMap("themap");
map.addInterceptor(new MyMapInterceptor());
map.put("1", "1",1, TimeUnit.SECONDS);
System.out.println(map.get("1"));
Thread.sleep(2000);
System.out.println(map.get("1"));
}
private static class MyMapInterceptor implements MapInterceptor {
@Override
public Object interceptGet(Object value) {
return null;
}
@Override
public void afterGet(Object value) {
}
@Override
public Object interceptPut(Object oldValue, Object newValue) {
return null;
}
@Override
public void afterPut(Object value) {
}
@Override
public Object interceptRemove(Object removedValue) {
throw new RuntimeException("Some external service is down so we don't want to evict this entry");
}
@Override
public void afterRemove(Object value) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment