Example usage for com.google.common.cache RemovalCause COLLECTED

List of usage examples for com.google.common.cache RemovalCause COLLECTED

Introduction

In this page you can find the example usage for com.google.common.cache RemovalCause COLLECTED.

Prototype

RemovalCause COLLECTED

To view the source code for com.google.common.cache RemovalCause COLLECTED.

Click Source Link

Document

The entry was removed automatically because its key or value was garbage-collected.

Usage

From source file:com.infinities.skyport.distributed.impl.hazelcast.hazeltask.executor.DistributedFutureTracker.java

public DistributedFutureTracker(final IExecutorTopologyService topologyService, long maxFutureWaitTime,
        TimeUnit unit) {//from w  w w .  j a v  a2s .com
    this.topologyService = topologyService;
    futures = CacheBuilder.newBuilder()
            // no future will wait for more than this time
            .expireAfterAccess(maxFutureWaitTime, unit)
            .removalListener(new RemovalListener<String, DistributedFuture<Object>>() {

                @Override
                public void onRemoval(RemovalNotification<String, DistributedFuture<Object>> notification) {
                    if (notification.getCause() == RemovalCause.EXPIRED) {
                        DistributedFuture<Object> future = notification.getValue();
                        long waitTimeMillis = System.currentTimeMillis() - future.getCreatedTime();
                        notification.getValue()
                                .setException(new TimeoutException("Future timed out waiting.  Waited "
                                        + (TimeUnit.MILLISECONDS.toMinutes(waitTimeMillis)) + " minutes"));

                        topologyService.cancelTask(future.getTaskId());
                        topologyService.removePendingTask(future.getTaskId());
                    } else if (notification.getCause() == RemovalCause.COLLECTED) {
                        // future was GC'd because we didn't
                        // want to track it
                        logger.debug("Future {} was garabge collected and removed from the tracker",
                                notification.getKey());
                    }
                }
            }).build();
}