package org.ehcache.impl.internal.events;
import org.ehcache.impl.config.event.CacheEventDispatcherFactoryConfiguration;
import org.ehcache.impl.config.event.DefaultCacheEventDispatcherConfiguration;
import org.ehcache.core.events.CacheEventDispatcherFactory;
import org.ehcache.core.events.CacheEventDispatcher;
import org.ehcache.impl.events.CacheEventDispatcherImpl;
import org.ehcache.spi.service.ServiceProvider;
import org.ehcache.core.spi.store.Store;
import org.ehcache.core.spi.service.ExecutionService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceConfiguration;
import org.ehcache.spi.service.ServiceDependencies;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import static org.ehcache.core.spi.service.ServiceUtils.findSingletonAmongst;
@ServiceDependencies(ExecutionService.class)
public class CacheEventDispatcherFactoryImpl implements CacheEventDispatcherFactory {
private final String defaultThreadPoolAlias;
private volatile ExecutionService executionService;
public CacheEventDispatcherFactoryImpl() {
this.defaultThreadPoolAlias = null;
}
public CacheEventDispatcherFactoryImpl(CacheEventDispatcherFactoryConfiguration configuration) {
this.defaultThreadPoolAlias = configuration.getThreadPoolAlias();
}
@Override
public void start(ServiceProvider<Service> serviceProvider) {
executionService = serviceProvider.getService(ExecutionService.class);
}
@Override
public void stop() {
}
@Override
public <K, V> CacheEventDispatcher<K, V> createCacheEventDispatcher(Store<K, V> store, ServiceConfiguration<?>... serviceConfigs) {
String threadPoolAlias = defaultThreadPoolAlias;
DefaultCacheEventDispatcherConfiguration config = findSingletonAmongst(DefaultCacheEventDispatcherConfiguration.class, (Object[]) serviceConfigs);
if (config != null) {
threadPoolAlias = config.getThreadPoolAlias();
}
ExecutorService orderedExecutor = executionService.getOrderedExecutor(threadPoolAlias, new LinkedBlockingQueue<>());
ExecutorService unOrderedExecutor = executionService.getUnorderedExecutor(threadPoolAlias, new LinkedBlockingQueue<>());
return new CacheEventDispatcherImpl<>(unOrderedExecutor, orderedExecutor);
}
@Override
public <K, V> void releaseCacheEventDispatcher(CacheEventDispatcher<K, V> eventDispatcher) {
if (eventDispatcher != null) {
eventDispatcher.shutdown();
}
}
}