@Test public void testConfigurationOfThreadPoolAlias() { @SuppressWarnings("unchecked") ServiceProvider<Service> serviceProvider = mock(ServiceProvider.class); when(serviceProvider.getService(ExecutionService.class)).thenReturn(mock(ExecutionService.class)); CacheEventDispatcherFactoryImpl factory = new CacheEventDispatcherFactoryImpl(); factory.start(serviceProvider); DefaultCacheEventDispatcherConfiguration config = spy(new DefaultCacheEventDispatcherConfiguration("aName")); @SuppressWarnings("unchecked") Store<Object, Object> store = mock(Store.class); factory.createCacheEventDispatcher(store, config); verify(config).getThreadPoolAlias(); }
@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); }
/** * Adds a {@link ServiceConfiguration} for the {@link org.ehcache.core.events.CacheEventDispatcherFactory} specifying * the thread pool alias to use. * * @param threadPoolAlias the thread pool alias to use * @return a new builder with the added configuration */ public CacheConfigurationBuilder<K, V> withEventListenersThreadPool(String threadPoolAlias) { return addOrReplaceConfiguration(new DefaultCacheEventDispatcherConfiguration(threadPoolAlias)); }
public DefaultCacheEventDispatcherConfigurationParser() { super(DefaultCacheEventDispatcherConfiguration.class, CacheTemplate::listenersConfig, config -> ofNullable(config.threadPool()).map(DefaultCacheEventDispatcherConfiguration::new).orElse(null), CacheType::getListeners, CacheType::setListeners, config -> new ListenersType().withDispatcherThreadPool(config.getThreadPoolAlias()), (initial, additional) -> initial.withDispatcherThreadPool(additional.getDispatcherThreadPool())); } }
@Test @SuppressWarnings("unchecked") public void testCreateCacheEventReturnsDisabledDispatcherWhenThreadPoolFound() throws Exception { ServiceProvider<Service> serviceProvider = mock(ServiceProvider.class); ExecutionService executionService = mock(ExecutionService.class); when(serviceProvider.getService(ExecutionService.class)).thenReturn(executionService); when(executionService.getOrderedExecutor(eq("myAlias"), any(BlockingQueue.class))).thenReturn(mock(ExecutorService.class)); when(executionService.getUnorderedExecutor(eq("myAlias"), any(BlockingQueue.class))).thenReturn(mock(ExecutorService.class)); CacheEventDispatcherFactoryImpl cacheEventDispatcherFactory = new CacheEventDispatcherFactoryImpl(); cacheEventDispatcherFactory.start(serviceProvider); Store<Object, Object> store = mock(Store.class); CacheEventDispatcher<Object, Object> dispatcher = cacheEventDispatcherFactory.createCacheEventDispatcher(store, new DefaultCacheEventDispatcherConfiguration("myAlias")); assertThat(dispatcher, instanceOf(CacheEventDispatcherImpl.class)); } }
@Test public void parseServiceConfiguration() throws Exception { CacheConfiguration<?, ?> cacheConfiguration = new XmlConfiguration(getClass().getResource("/configs/ehcache-cacheEventListener.xml")).getCacheConfigurations().get("template1"); DefaultCacheEventDispatcherConfiguration eventDispatcherConfig = findSingletonAmongst(DefaultCacheEventDispatcherConfiguration.class, cacheConfiguration.getServiceConfigurations()); assertThat(eventDispatcherConfig).isNotNull(); assertThat(eventDispatcherConfig.getThreadPoolAlias()).isEqualTo("listeners-pool"); }
@Test @SuppressWarnings("unchecked") public void testCreateCacheEventDispatcherReturnsDisabledDispatcherWhenNoThreadPool() throws Exception { ServiceProvider<Service> serviceProvider = mock(ServiceProvider.class); ExecutionService executionService = mock(ExecutionService.class); when(serviceProvider.getService(ExecutionService.class)).thenReturn(executionService); when(executionService.getOrderedExecutor(eq("myAlias"), any(BlockingQueue.class))).thenThrow(IllegalArgumentException.class); when(executionService.getUnorderedExecutor(eq("myAlias"), any(BlockingQueue.class))).thenThrow(IllegalArgumentException.class); CacheEventDispatcherFactoryImpl cacheEventDispatcherFactory = new CacheEventDispatcherFactoryImpl(); cacheEventDispatcherFactory.start(serviceProvider); @SuppressWarnings("unchecked") Store<Object, Object> store = mock(Store.class); try { cacheEventDispatcherFactory.createCacheEventDispatcher(store, new DefaultCacheEventDispatcherConfiguration("myAlias")); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException iae) { // expected } }
@Test public void testCacheEventListenerThreadPoolName() throws Exception { Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/ehcache-cacheEventListener.xml")); CacheConfiguration<?, ?> template1 = configuration.getCacheConfigurations().get("template1"); DefaultCacheEventDispatcherConfiguration eventDispatcherConfig = null; for (ServiceConfiguration<?> serviceConfiguration : template1.getServiceConfigurations()) { if (serviceConfiguration instanceof DefaultCacheEventDispatcherConfiguration) { eventDispatcherConfig = (DefaultCacheEventDispatcherConfiguration) serviceConfiguration; } } assertThat(eventDispatcherConfig.getThreadPoolAlias(), is("listeners-pool")); }
@Test public void unparseServiceConfiguration() { CacheConfiguration<?, ?> cacheConfig = newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(new DefaultCacheEventDispatcherConfiguration("foo")).build(); CacheType cacheType = new CacheType(); cacheType = new DefaultCacheEventDispatcherConfigurationParser().unparseServiceConfiguration(cacheConfig, cacheType); assertThat(cacheType.getListeners().getDispatcherThreadPool()).isEqualTo("foo"); } }
@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); }
/** * Adds a {@link ServiceConfiguration} for the {@link org.ehcache.core.events.CacheEventDispatcherFactory} specifying * the thread pool alias to use. * * @param threadPoolAlias the thread pool alias to use * @return a new builder with the added configuration */ public CacheConfigurationBuilder<K, V> withEventListenersThreadPool(String threadPoolAlias) { return addOrReplaceConfiguration(new DefaultCacheEventDispatcherConfiguration(threadPoolAlias)); }
public DefaultCacheEventDispatcherConfigurationParser() { super(DefaultCacheEventDispatcherConfiguration.class, CacheTemplate::listenersConfig, config -> ofNullable(config.threadPool()).map(DefaultCacheEventDispatcherConfiguration::new).orElse(null), CacheType::getListeners, CacheType::setListeners, config -> new ListenersType().withDispatcherThreadPool(config.getThreadPoolAlias()), (initial, additional) -> initial.withDispatcherThreadPool(additional.getDispatcherThreadPool())); } }