List of usage examples for com.google.common.cache RemovalListeners asynchronous
public static <K, V> RemovalListener<K, V> asynchronous(final RemovalListener<K, V> listener, final Executor executor)
From source file:com.ning.metrics.collector.processing.db.InMemoryCounterCacheProcessor.java
@Inject public InMemoryCounterCacheProcessor(final CollectorConfig config, final CounterStorage counterStorage) { this.executorShutdownTimeOut = config.getSpoolWriterExecutorShutdownTime(); this.counterEventDBFlushTime = config.getCounterEventMemoryFlushTime(); this.executorService = new LoggingExecutor(1, 1, Long.MAX_VALUE, TimeUnit.DAYS, new ArrayBlockingQueue<Runnable>(2), new NamedThreadFactory("CounterEvents-Storage-Threads"), new ThreadPoolExecutor.CallerRunsPolicy()); this.counterEventsBySubscriptionId = CacheBuilder.newBuilder() .expireAfterAccess(counterEventDBFlushTime.getPeriod(), counterEventDBFlushTime.getUnit()) .maximumWeight(config.getMaxCounterEventFlushCacheCount()) .weigher(new Weigher<String, Queue<CounterEventData>>() { @Override//from w w w.j ava 2 s.com public int weigh(String key, Queue<CounterEventData> value) { return value.size(); } }).removalListener( RemovalListeners.asynchronous(new RemovalListener<String, Queue<CounterEventData>>() { @Override public void onRemoval( RemovalNotification<String, Queue<CounterEventData>> removalNotification) { if (!Objects.equal(removalNotification.getCause(), RemovalCause.REPLACED) && !Objects.equal(null, removalNotification.getValue()) && !removalNotification.getValue().isEmpty()) { Multimap<String, CounterEventData> multimap = ArrayListMultimap.create(); List<CounterEventData> counterEventDataList = Lists .newArrayList(removalNotification.getValue()); removalNotification.getValue().clear(); Map<String, CounterEventData> groupMap = new ConcurrentHashMap<String, CounterEventData>(); for (CounterEventData counterEventData : counterEventDataList) { CounterEventData groupedData = groupMap .get(counterEventData.getUniqueIdentifier() + counterEventData.getFormattedDate()); if (Objects.equal(null, groupedData)) { groupMap.put( counterEventData.getUniqueIdentifier() + counterEventData.getFormattedDate(), counterEventData); continue; } groupedData.mergeCounters(counterEventData.getCounters()); groupMap.put(counterEventData.getUniqueIdentifier() + counterEventData.getFormattedDate(), groupedData); } multimap.putAll(removalNotification.getKey(), groupMap.values()); counterStorage.bufferMetrics(multimap); } } }, this.executorService)) .recordStats().build(); }
From source file:io.pravega.segmentstore.server.host.stat.AutoScaleProcessor.java
AutoScaleProcessor(AutoScalerConfig configuration, Executor executor, ScheduledExecutorService maintenanceExecutor) { this.configuration = configuration; this.maintenanceExecutor = maintenanceExecutor; this.executor = executor; serializer = new JavaSerializer<>(); writerConfig = EventWriterConfig.builder().build(); writer = new AtomicReference<>(); cache = CacheBuilder.newBuilder().initialCapacity(INITIAL_CAPACITY).maximumSize(MAX_CACHE_SIZE) .expireAfterAccess(configuration.getCacheExpiry().getSeconds(), TimeUnit.SECONDS).removalListener( RemovalListeners.asynchronous((RemovalListener<String, Pair<Long, Long>>) notification -> { if (notification.getCause().equals(RemovalCause.EXPIRED)) { triggerScaleDown(notification.getKey(), true); }/*from w w w. j ava 2 s . c o m*/ }, maintenanceExecutor)) .build(); CompletableFuture.runAsync(this::bootstrapRequestWriters, maintenanceExecutor); }
From source file:alluxio.worker.block.UfsInputStreamManager.java
/** * Creates a new {@link UfsInputStreamManager}. *///from www. java 2 s . c o m public UfsInputStreamManager() { mFileIdToInputStreamIds = new HashMap<>(); mRemovalThreadPool = ExecutorServiceFactories .fixedThreadPoolExecutorServiceFactory(Constants.UFS_INPUT_STREAM_CACHE_EXPIRATION, 2).create(); // A listener to the input stream removal. RemovalListener<Long, CachedSeekableInputStream> listener = ( RemovalNotification<Long, CachedSeekableInputStream> removal) -> { CachedSeekableInputStream inputStream = removal.getValue(); boolean shouldClose = false; synchronized (mFileIdToInputStreamIds) { if (mFileIdToInputStreamIds.containsKey(inputStream.getFileId())) { UfsInputStreamIdSet resources = mFileIdToInputStreamIds.get(inputStream.getFileId()); synchronized (resources) { // remove the key resources.removeInUse(removal.getKey()); if (resources.removeAvailable(removal.getKey())) { // close the resource LOG.debug("Removed the under file input stream resource of {}", removal.getKey()); shouldClose = true; } if (resources.isEmpty()) { // remove the resources entry mFileIdToInputStreamIds.remove(inputStream.getFileId()); } } } else { LOG.warn("Try to remove the resource entry of {} but not exists any more", removal.getKey()); } } if (shouldClose) { try { inputStream.close(); } catch (IOException e) { LOG.warn("Failed to close the input stream resource of file {} with file id {}", " and resource id {}", inputStream.getFilePath(), inputStream.getFileId(), removal.getKey()); } } }; mUnderFileInputStreamCache = CacheBuilder.newBuilder() .maximumSize(Configuration.getInt(PropertyKey.WORKER_UFS_INSTREAM_CACHE_MAX_SIZE)) .expireAfterAccess(Configuration.getMs(PropertyKey.WORKER_UFS_INSTREAM_CACHE_EXPIRARTION_TIME), TimeUnit.MILLISECONDS) .removalListener(RemovalListeners.asynchronous(listener, mRemovalThreadPool)).build(); }