Java Code Examples for javax.cache.configuration.MutableConfiguration#addCacheEntryListenerConfiguration()

The following examples show how to use javax.cache.configuration.MutableConfiguration#addCacheEntryListenerConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Eh107XmlIntegrationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateAddsListeners() throws Exception {
  CacheManager cacheManager = cachingProvider.getCacheManager(getClass().getResource("/ehcache-107-listeners.xml")
      .toURI(), getClass().getClassLoader());

  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(Test107CacheEntryListener::new, null, false, true);
  configuration.addCacheEntryListenerConfiguration(listenerConfiguration);

  Cache<String, String> cache = cacheManager.createCache("foos", configuration);
  cache.put("Hello", "Bonjour");

  assertThat(Test107CacheEntryListener.seen.size(), Matchers.is(1));
  assertThat(TestCacheEventListener.seen.size(), Matchers.is(1));
}
 
Example 2
Source File: AdditionalCacheListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Override
protected MutableConfiguration<Integer, String> extraSetup(MutableConfiguration<Integer, String> cfg) {
  // cfg.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 5)));
  cacheEntryListenerServer = new CacheEntryListenerServer<>(10011, Integer.class, String.class);
  try {
    cacheEntryListenerServer.open();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  listener = new RecordingListener<>();
  cacheEntryListenerServer.addCacheEventListener(listener);
  CacheEntryListenerClient<Integer, String> clientListener =
    new CacheEntryListenerClient<>(cacheEntryListenerServer.getInetAddress(), cacheEntryListenerServer.getPort());
  boolean _isSynchronous = false;
  listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(
    FactoryBuilder.factoryOf(clientListener), null, true, _isSynchronous);
  return cfg.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 3
Source File: CacheListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Override
protected MutableConfiguration<Long, String> extraSetup(MutableConfiguration<Long, String> configuration) {
  cacheEntryListenerServer = new CacheEntryListenerServer<Long, String>(10011, Long.class, String.class);
  try {
    cacheEntryListenerServer.open();
  } catch (IOException e) {
    e.printStackTrace();
  }

  //establish and open a CacheEntryListenerServer to handle cache
  //cache entry events from a CacheEntryListenerClient
  listener = new MyCacheEntryListener<Long, String>();
  cacheEntryListenerServer.addCacheEventListener(listener);

  //establish a CacheEntryListenerClient that a Cache can use for CacheEntryListening
  //(via the CacheEntryListenerServer)
  CacheEntryListenerClient<Long, String> clientListener =
    new CacheEntryListenerClient<>(cacheEntryListenerServer.getInetAddress(), cacheEntryListenerServer.getPort());
  listenerConfiguration = new MutableCacheEntryListenerConfiguration<Long, String>(FactoryBuilder.factoryOf(clientListener), null, true, true);
  return configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 4
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
protected MutableConfiguration<Integer, Integer> extraSetup(MutableConfiguration<Integer, Integer> configuration) {
  listener = new CacheTestSupport.MyCacheEntryListener<Integer, Integer>();

  //establish a CacheEntryListenerClient that a Cache can use for CacheEntryListening
  //(via the CacheEntryListenerServer)

  listenerConfiguration =
    new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(cacheEntryListerClient), null, true, true);
  cacheEntryListenerServer.addCacheEventListener(listener);
  return configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
}
 
Example 5
Source File: JCacheAdapter.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <C extends Configuration<K, V>> C getConfiguration(Class<C> _class) {
  if (CompleteConfiguration.class.isAssignableFrom(_class)) {
    MutableConfiguration<K, V> cfg = new MutableConfiguration<K, V>();
    cfg.setTypes(keyType, valueType);
    cfg.setStatisticsEnabled(jmxStatisticsEnabled);
    cfg.setManagementEnabled(jmxEnabled);
    cfg.setStoreByValue(storeByValue);
    Collection<CacheEntryListenerConfiguration<K,V>> _listenerConfigurations = eventHandling.getAllListenerConfigurations();
    for (CacheEntryListenerConfiguration<K,V> _listenerConfig : _listenerConfigurations) {
      cfg.addCacheEntryListenerConfiguration(_listenerConfig);
    }
    return (C) cfg;
  }
  return (C) new Configuration<K, V>() {
    @Override
    public Class<K> getKeyType() {
      return keyType;
    }

    @Override
    public Class<V> getValueType() {
      return valueType;
    }

    @Override
    public boolean isStoreByValue() {
      return storeByValue;
    }
  };
}