/** * 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)); }
@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 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(); }
@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 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"); } }
/** * 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)); }