public CacheEventDispatcherFactoryImpl(CacheEventDispatcherFactoryConfiguration configuration) { this.defaultThreadPoolAlias = configuration.getThreadPoolAlias(); }
/** * Builds the {@link CacheEventListenerConfiguration} this builder represents. * * @return the {@code CacheEventListenerConfiguration} */ public DefaultCacheEventListenerConfiguration build() { DefaultCacheEventListenerConfiguration defaultCacheEventListenerConfiguration; if (this.listenerClass != null) { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerClass, this.listenerArguments); } else { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerInstance); } if (eventOrdering != null) { defaultCacheEventListenerConfiguration.setEventOrderingMode(this.eventOrdering); } if (eventFiringMode != null) { defaultCacheEventListenerConfiguration.setEventFiringMode(this.eventFiringMode); } return defaultCacheEventListenerConfiguration; }
/** * 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 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(); }
/** * Adds a {@link CacheEventDispatcherFactoryConfiguration}, that specifies the thread pool to use, to the returned * builder. * * @param threadPoolAlias the thread pool alias * @return a new builder with the added configuration * * @see PooledExecutionServiceConfigurationBuilder */ public CacheManagerBuilder<T> withDefaultEventListenersThreadPool(String threadPoolAlias) { CacheEventDispatcherFactoryConfiguration config = configBuilder.findServiceByClass(CacheEventDispatcherFactoryConfiguration.class); if (config == null) { return new CacheManagerBuilder<>(this, configBuilder.addService(new CacheEventDispatcherFactoryConfiguration(threadPoolAlias))); } else { ConfigurationBuilder builder = configBuilder.removeService(config); return new CacheManagerBuilder<>(this, builder.addService(new CacheEventDispatcherFactoryConfiguration(threadPoolAlias))); } }
@Test(expected = IllegalArgumentException.class) public void testFailsToConstructWithEmptyEventSetAndClass() { Set<EventType> fireOn = emptySet(); Class<TestCacheEventListener> eventListenerClass = TestCacheEventListener.class; new DefaultCacheEventListenerConfiguration(fireOn, eventListenerClass); }
/** * Adds {@link StoreEventSourceConfiguration} with the specified dispatcher concurrency * to the configured builder. * * @param dispatcherConcurrency the level of concurrency in the dispatcher for ordered events * @return a new builder with the added configuration */ public CacheConfigurationBuilder<K, V> withDispatcherConcurrency(int dispatcherConcurrency) { return addOrReplaceConfiguration(new DefaultEventSourceConfiguration(dispatcherConcurrency)); }
@Test public void unparseServiceConfigurationWithInstance() { TestCacheEventListener testCacheEventListener = new TestCacheEventListener(); DefaultCacheEventListenerConfiguration listenerConfig = new DefaultCacheEventListenerConfiguration(EnumSet.of(CREATED, REMOVED), testCacheEventListener); listenerConfig.setEventFiringMode(SYNCHRONOUS); listenerConfig.setEventOrderingMode(UNORDERED); CacheConfiguration<?, ?> cacheConfig = newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(listenerConfig).build(); CacheType cacheType = new CacheType(); assertThatExceptionOfType(XmlConfigurationException.class).isThrownBy(() -> new DefaultCacheEventListenerConfigurationParser().unparseServiceConfiguration(cacheConfig, cacheType)) .withMessage("%s", "XML translation for instance based initialization for " + "DefaultCacheEventListenerConfiguration is not supported"); } }
@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); }
@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 unparseServiceCreationConfiguration() { Configuration config = ConfigurationBuilder.newConfigurationBuilder() .addService(new CacheEventDispatcherFactoryConfiguration("foo")).build(); ConfigType configType = new CacheEventDispatcherFactoryConfigurationParser().unparseServiceCreationConfiguration(config, new ConfigType()); assertThat(configType.getEventDispatch().getThreadPool()).isEqualTo("foo"); } }
@Test(expected = IllegalArgumentException.class) public void testFailsToConstructWithEmptyEventSetAndInstance() { Set<EventType> fireOn = emptySet(); new DefaultCacheEventListenerConfiguration(fireOn, mock(CacheEventListener.class)); }
@Test public void parseServiceCreationConfiguration() throws SAXException, JAXBException, ParserConfigurationException, IOException, ClassNotFoundException { Configuration xmlConfig = new XmlConfiguration(getClass().getResource("/configs/ehcache-cacheEventListener.xml")); assertThat(xmlConfig.getServiceCreationConfigurations()).hasSize(1); ServiceCreationConfiguration<?> configuration = xmlConfig.getServiceCreationConfigurations().iterator().next(); assertThat(configuration).isInstanceOf(CacheEventDispatcherFactoryConfiguration.class); CacheEventDispatcherFactoryConfiguration providerConfiguration = (CacheEventDispatcherFactoryConfiguration) configuration; assertThat(providerConfiguration.getThreadPoolAlias()).isEqualTo("events-pool"); }
@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 testNoDefaultThreadPoolSucceedsWithExplicitConfig() throws Exception { PooledExecutionServiceConfiguration executionServiceConfiguration = new PooledExecutionServiceConfiguration(); executionServiceConfiguration.addPool("foo", 2, 4); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(new CacheEventDispatcherFactoryConfiguration("foo")) .using(executionServiceConfiguration) .build(true); cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build()); cacheManager.close(); }
@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"); } }
@Test public void testDefaultThreadPoolFailsWithExplicitConfig() throws Exception { PooledExecutionServiceConfiguration executionServiceConfiguration = new PooledExecutionServiceConfiguration(); executionServiceConfiguration.addDefaultPool("dflt", 2, 4); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(new CacheEventDispatcherFactoryConfiguration("foo")) .using(executionServiceConfiguration) .build(true); try { cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build()); fail("expected IllegalStateException"); } catch (IllegalStateException ise) { // expected } cacheManager.close(); }