javax.cache.configuration.CacheEntryListenerConfiguration Java Examples

The following examples show how to use javax.cache.configuration.CacheEntryListenerConfiguration. 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: JCacheBuilder.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void setupEventHandling() {
  if ((config.getCacheEntryListenerConfigurations() == null ||
    !config.getCacheEntryListenerConfigurations().iterator().hasNext()) &&
    !extraConfiguration.isSupportOnlineListenerAttachment()) {
    eventHandling = (EventHandling<K,V>) EventHandling.DISABLED;
    return;
  }
  EventHandlingImpl<K,V> _eventHandling = new EventHandlingImpl<K, V>(manager, Executors.newCachedThreadPool());
  _eventHandling.addInternalListenersToCache2kConfiguration(cache2kConfiguration);
  for (CacheEntryListenerConfiguration<K,V> cfg : config.getCacheEntryListenerConfigurations()) {
    _eventHandling.registerListener(cfg);
  }
  eventHandling = _eventHandling;
  cache2kConfiguration.getCacheClosedListeners().add(
    new CustomizationReferenceSupplier<CacheClosedListener>(_eventHandling));
}
 
Example #2
Source File: JCSListener.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
{
    oldValue = cacheEntryListenerConfiguration.isOldValueRequired();
    synchronous = cacheEntryListenerConfiguration.isSynchronous();

    final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration
            .getCacheEntryEventFilterFactory();
    if (filterFactory == null)
    {
        filter = NoFilter.INSTANCE;
    }
    else
    {
        filter = filterFactory.create();
    }

    delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create();
    remove = CacheEntryRemovedListener.class.isInstance(delegate);
    expire = CacheEntryExpiredListener.class.isInstance(delegate);
    update = CacheEntryUpdatedListener.class.isInstance(delegate);
    create = CacheEntryCreatedListener.class.isInstance(delegate);
}
 
Example #3
Source File: CacheContinuousQueryManager.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cfg Listener configuration.
 * @param onStart Whether listener is created on node start.
 * @throws IgniteCheckedException If failed.
 */
public void executeJCacheQuery(CacheEntryListenerConfiguration cfg, boolean onStart, boolean keepBinary)
    throws IgniteCheckedException {
    JCacheQuery lsnr = new JCacheQuery(cfg, onStart, keepBinary);

    JCacheQuery old = jCacheLsnrs.putIfAbsent(cfg, lsnr);

    if (old != null)
        throw new IllegalArgumentException("Listener is already registered for configuration: " + cfg);

    try {
        lsnr.execute();
    }
    catch (IgniteCheckedException e) {
        cancelJCacheQuery(cfg);

        throw e;
    }
}
 
Example #4
Source File: ListenerCollection.java    From triava with Apache License 2.0 6 votes vote down vote up
/**
 * Enables a listener, without adding it to the Configuration. An  enabled listener can send events after this method returns.  
 * The caller must make sure that the
 * corresponding Configuration object reflects the change.
 * 
 * @param listenerConfiguration
 * @return
 */
private synchronized boolean enableCacheEntryListener(CacheEntryListenerConfiguration<K, V> listenerConfiguration)
{
	DispatchMode dispatchMode = listenerConfiguration.isSynchronous() ? DispatchMode.SYNC : DispatchMode.ASYNC_TIMED;
	ListenerEntry<K, V> newListener = new ListenerEntry<K, V>(listenerConfiguration, tcache, dispatchMode);
	boolean added = listeners.add(newListener);
	for (EventType eventType : EventType.values())
	{
		if (newListener.isListeningFor(eventType))
		{
			listenerPresentMask |= (1 << eventType.ordinal());
		}
	}

	return added;
}
 
Example #5
Source File: ListenerCollection.java    From triava with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a cache listener.
 * @param listenerConfiguration The Cache Listener
 */
public synchronized void registerCacheEntryListener(CacheEntryListenerConfiguration<K, V> listenerConfiguration)
{
	throwISEwhenClosed();
	
	boolean added = enableCacheEntryListener(listenerConfiguration);
	if (!added)
	{
		throw new IllegalArgumentException("Cache entry listener may not be added twice to " + tcache.id() + ": "+ listenerConfiguration);
	}
	else
	{
		// Reflect listener change in the configuration, as required by JSR107
		builder.addCacheEntryListenerConfiguration(listenerConfiguration);
	}
}
 
Example #6
Source File: ListenerCollection.java    From triava with Apache License 2.0 6 votes vote down vote up
/**
 * Deregisters a cache listener.
 * @param listenerConfiguration The Cache Listener
 */
public synchronized void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> listenerConfiguration)
{
	throwISEwhenClosed();

	Iterator<ListenerEntry<K, V>> it = listeners.iterator();
	while (it.hasNext())
	{
		ListenerEntry<K, V> listenerEntry = it.next();
		if (listenerConfiguration.equals(listenerEntry.getConfig()))
		{
			listenerEntry.shutdown();
			it.remove();
			// Reflect listener change in the configuration, as required by JSR107
			builder.removeCacheEntryListenerConfiguration(listenerConfiguration);
			break; // Can be only one, as it is in the Spec that Listeners must not added twice.
		}
	}

	// Removing a listener invalidates the lookup array (just like in a bloom filter), thus rebuild it
	rebuildListenerPresent();		
}
 
Example #7
Source File: EventDispatcher.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a cache entry listener based on the supplied configuration.
 *
 * @param configuration the listener's configuration.
 */
@SuppressWarnings("PMD.CloseResource")
public void register(CacheEntryListenerConfiguration<K, V> configuration) {
  if (configuration.getCacheEntryListenerFactory() == null) {
    return;
  }
  EventTypeAwareListener<K, V> listener = new EventTypeAwareListener<>(
      configuration.getCacheEntryListenerFactory().create());

  CacheEntryEventFilter<K, V> filter = event -> true;
  if (configuration.getCacheEntryEventFilterFactory() != null) {
    filter = new EventTypeFilter<>(listener,
        configuration.getCacheEntryEventFilterFactory().create());
  }

  Registration<K, V> registration = new Registration<>(configuration, filter, listener);
  dispatchQueues.putIfAbsent(registration, CompletableFuture.completedFuture(null));
}
 
Example #8
Source File: JCacheExpiryAndMaximumSizeTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
  CacheEntryRemovedListener<Integer, Integer> listener = events -> removed.incrementAndGet();

  CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>();

  configuration.setMaximumSize(OptionalLong.of(MAXIMUM));
  CacheEntryListenerConfiguration<Integer, Integer> listenerConfiguration =
      new MutableCacheEntryListenerConfiguration<>(() -> listener,
          /* filterFactory */ null, /* isOldValueRequired */ false, /* isSynchronous */ true);
  configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
  configuration.setExecutorFactory(MoreExecutors::directExecutor);

  configuration.setExpiryFactory(Optional.of(() -> expiry));
  configuration.setTickerFactory(() -> ticker::read);

  return configuration;
}
 
Example #9
Source File: IgniteCacheEntryListenerAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param lsnrFactory Listener factory.
 * @param key Key.
 * @param create {@code True} if listens for create events.
 * @param update {@code True} if listens for update events.
 * @param rmv {@code True} if listens for remove events.
 * @param expire {@code True} if listens for expire events.
 * @throws Exception If failed.
 */
private void checkEvents(
    final IgniteCache<Object, Object> cache,
    final Factory<CacheEntryListener<Object, Object>> lsnrFactory,
    Integer key,
    boolean create,
    boolean update,
    boolean rmv,
    boolean expire) throws Exception {
    CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(
        lsnrFactory,
        null,
        true,
        false
    );

    cache.registerCacheEntryListener(lsnrCfg);

    try {
        checkEvents(cache, lsnrCfg, key, create, update, rmv, expire, true);
    }
    finally {
        cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
 
Example #10
Source File: JCache.java    From redisson with Apache License 2.0 6 votes vote down vote up
public JCache(JCacheManager cacheManager, Redisson redisson, String name, JCacheConfiguration<K, V> config, boolean hasOwnRedisson) {
    super(redisson.getConfig().getCodec(), redisson.getCommandExecutor(), name);
    
    this.hasOwnRedisson = hasOwnRedisson;
    this.redisson = redisson;
    
    Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory();
    if (cacheLoaderFactory != null) {
        cacheLoader = cacheLoaderFactory.create();
    }
    Factory<CacheWriter<? super K, ? super V>> cacheWriterFactory = config.getCacheWriterFactory();
    if (config.getCacheWriterFactory() != null) {
        cacheWriter = (CacheWriter<K, V>) cacheWriterFactory.create();
    }
    
    this.cacheManager = cacheManager;
    this.config = config;
    
    redisson.getEvictionScheduler().scheduleJCache(getName(), getTimeoutSetName(), getExpiredChannelName());
    
    for (CacheEntryListenerConfiguration<K, V> listenerConfig : config.getCacheEntryListenerConfigurations()) {
        registerCacheEntryListener(listenerConfig, false);
    }
}
 
Example #11
Source File: ConfigurationMerger.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private <K, V> Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> initCacheEventListeners(CompleteConfiguration<K, V> config) {
  Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> listenerResources = new ConcurrentHashMap<>();
  for (CacheEntryListenerConfiguration<K, V> listenerConfig : config.getCacheEntryListenerConfigurations()) {
    listenerResources.put(listenerConfig, ListenerResources.createListenerResources(listenerConfig));
  }
  return listenerResources;
}
 
Example #12
Source File: CacheResources.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
synchronized ListenerResources<K, V> registerCacheEntryListener(CacheEntryListenerConfiguration<K, V> listenerConfig) {
  checkClosed();

  if (listenerResources.containsKey(listenerConfig)) {
    throw new IllegalArgumentException("listener config already registered");
  }

  ListenerResources<K, V> rv = ListenerResources.createListenerResources(listenerConfig);
  listenerResources.put(listenerConfig, rv);
  return rv;
}
 
Example #13
Source File: EventHandlingImpl.java    From cache2k with Apache License 2.0 5 votes vote down vote up
static <T extends Listener<K,V>, K, V> boolean removeCfgMatch(
  final CacheEntryListenerConfiguration<K,V> cfg,
  final List<T> _listenerList) {
  for (final T l : _listenerList) {
    if (l.config.equals(cfg)) {
      _listenerList.remove(l);
      removeCfgMatch(cfg, _listenerList);
      return true;
    }
  }
  return false;
}
 
Example #14
Source File: BlazingCacheCache.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void registerCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
    checkClosed();
    for (BlazingCacheCacheEntryListenerWrapper listenerWrapper : listeners) {
        if (listenerWrapper.configuration.equals(cacheEntryListenerConfiguration)) {
            throw new IllegalArgumentException("configuration " + cacheEntryListenerConfiguration + " already used");
        }
    }
    configureListener(cacheEntryListenerConfiguration);
    this.configuration.addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);

}
 
Example #15
Source File: CacheResources.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
synchronized ListenerResources<K, V> deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> listenerConfig) {
  checkClosed();

  ListenerResources<K, V> resources = listenerResources.remove(listenerConfig);
  if (resources == null) {
    return null;
  }
  try {
    CloseUtil.closeAll(resources);
  } catch (Throwable t) {
    throw new CacheException(t);
  }
  return resources;
}
 
Example #16
Source File: JCacheMaximumSizeTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
  CacheEntryRemovedListener<Integer, Integer> listener = events -> removed.incrementAndGet();
  CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>();
  configuration.setMaximumSize(OptionalLong.of(MAXIMUM));
  CacheEntryListenerConfiguration<Integer, Integer> listenerConfiguration =
      new MutableCacheEntryListenerConfiguration<Integer, Integer>(() -> listener,
          /* filterFactory */ null, /* isOldValueRequired */ false, /* isSynchronous */ true);
  configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
  configuration.setExecutorFactory(MoreExecutors::directExecutor);
  return configuration;
}
 
Example #17
Source File: JCacheMaximumWeightTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
  CacheEntryRemovedListener<Integer, Integer> listener = events ->
      removedWeight.addAndGet(Iterables.getOnlyElement(events).getValue());
  CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>();
  configuration.setMaximumWeight(OptionalLong.of(MAXIMUM));
  configuration.setWeigherFactory(Optional.of(() -> (key, value) -> value));
  CacheEntryListenerConfiguration<Integer, Integer> listenerConfiguration =
      new MutableCacheEntryListenerConfiguration<Integer, Integer>(() -> listener,
          /* filterFactory */ null, /* isOldValueRequired */ true, /* isSynchronous */ true);
  configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
  configuration.setExecutorFactory(MoreExecutors::directExecutor);
  return configuration;
}
 
Example #18
Source File: JCSCache.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Override
public void registerCacheEntryListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
{
    assertNotClosed();
    if (listeners.containsKey(cacheEntryListenerConfiguration))
    {
        throw new IllegalArgumentException(cacheEntryListenerConfiguration + " already registered");
    }
    listeners.put(cacheEntryListenerConfiguration, new JCSListener<>(cacheEntryListenerConfiguration));
    config.addListener(cacheEntryListenerConfiguration);
}
 
Example #19
Source File: TypesafeConfigurationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
static void checkListener(CaffeineConfiguration<?, ?> config) {
  CacheEntryListenerConfiguration<?, ?> listener = Iterables.getOnlyElement(
      config.getCacheEntryListenerConfigurations());
  assertThat(listener.getCacheEntryListenerFactory().create(),
      instanceOf(TestCacheEntryListener.class));
  assertThat(listener.getCacheEntryEventFilterFactory().create(),
      instanceOf(TestCacheEntryEventFilter.class));
  assertThat(listener.isSynchronous(), is(true));
  assertThat(listener.isOldValueRequired(), is(true));
}
 
Example #20
Source File: JCache.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
    Map<Integer, String> listenerIds = listeners.remove(cacheEntryListenerConfiguration);
    if (listenerIds != null) {
        for (Map.Entry<Integer, String> entry : listenerIds.entrySet()) {
            redisson.getTopic(entry.getValue()).removeListener(entry.getKey());
        }
    }
    config.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
}
 
Example #21
Source File: JCacheAdapter.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> cfg) {
  if (cfg == null) {
    throw new NullPointerException();
  }
  eventHandling.deregisterListener(cfg);
}
 
Example #22
Source File: CacheProxy.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(
    CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
  requireNotClosed();
  configuration.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
  dispatcher.deregister(cacheEntryListenerConfiguration);
}
 
Example #23
Source File: Eh107Cache.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public void registerCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
  checkClosed();

  if (cacheEntryListenerConfiguration == null) {
    throw new NullPointerException();
  }

  ListenerResources<K, V> resources = cacheResources.registerCacheEntryListener(cacheEntryListenerConfiguration);
  config.addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);

  registerEhcacheListeners(cacheEntryListenerConfiguration, resources);
}
 
Example #24
Source File: BlazingCacheCacheEntryListenerWrapper.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
BlazingCacheCacheEntryListenerWrapper(boolean synchronous, boolean oldValueRequired, CacheEntryListener<K, V> listener, CacheEntryEventFilter<K, V> filter, CacheEntryListenerConfiguration<K, V> configuration, BlazingCacheCache<K, V> parent) {
    this.synchronous = synchronous;
    this.parent = parent;
    this.oldValueRequired = oldValueRequired;
    this.listener = listener;
    this.filter = filter;
    this.configuration = configuration;
    this.onCreate = listener instanceof CacheEntryCreatedListener;
    this.onUpdate = listener instanceof CacheEntryUpdatedListener;
    this.onRemove = listener instanceof CacheEntryRemovedListener;
    this.needPreviousValue = oldValueRequired || onRemove || onUpdate;
}
 
Example #25
Source File: ClassLoaderAwareCache.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
{
    final Thread thread = Thread.currentThread();
    final ClassLoader loader = before(thread);
    try
    {
        delegate.deregisterCacheEntryListener(cacheEntryListenerConfiguration);
    }
    finally
    {
        thread.setContextClassLoader(loader);
    }
}
 
Example #26
Source File: BlazingCacheCache.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
    checkClosed();
    List<BlazingCacheCacheEntryListenerWrapper<K, V>> newList = new ArrayList<>();
    boolean _needPreviuosValueForListeners = false;
    for (BlazingCacheCacheEntryListenerWrapper<K, V> listenerWrapper : listeners) {
        if (!listenerWrapper.configuration.equals(cacheEntryListenerConfiguration)) {
            newList.add(listenerWrapper);
            _needPreviuosValueForListeners = _needPreviuosValueForListeners | listenerWrapper.needPreviousValue;
        }
    }
    listeners = newList;
    needPreviuosValueForListeners = _needPreviuosValueForListeners || policy != null;
    this.configuration.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
}
 
Example #27
Source File: JCSCache.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Override
public void deregisterCacheEntryListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
{
    assertNotClosed();
    listeners.remove(cacheEntryListenerConfiguration);
    config.removeListener(cacheEntryListenerConfiguration);
}
 
Example #28
Source File: IgniteCacheEntryListenerAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testNoOldValue() throws Exception {
    CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(
        new Factory<CacheEntryListener<Object, Object>>() {
            @Override public CacheEntryListener<Object, Object> create() {
                return new CreateUpdateRemoveExpireListener();
            }
        },
        null,
        false,
        true
    );

    IgniteCache<Object, Object> cache = jcache();

    try {
        for (Integer key : keys()) {
            log.info("Check create/update/remove/expire events, no old value [key=" + key + ']');

            cache.registerCacheEntryListener(lsnrCfg);

            checkEvents(cache, lsnrCfg, key, true, true, true, true, false);
        }
    }
    finally {
        cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
 
Example #29
Source File: IgniteCacheEntryListenerExpiredEventsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void checkExpiredEvents(CacheConfiguration<Object, Object> ccfg) throws Exception {
    IgniteCache<Object, Object> cache = ignite(0).createCache(ccfg);

    try {
        evtCntr = new AtomicInteger();

        CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(
            new ExpiredListenerFactory(),
            null,
            true,
            false
        );

        cache.registerCacheEntryListener(lsnrCfg);

        IgniteCache<Object, Object> expiryCache =
            cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500)));

        expiryCache.put(1, 1);

        for (int i = 0; i < 10; i++)
            cache.get(i);

        boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
            @Override public boolean apply() {
                return evtCntr.get() > 0;
            }
        }, 5000);

        assertTrue(wait);

        U.sleep(100);

        assertEquals(1, evtCntr.get());
    }
    finally {
        ignite(0).destroyCache(cache.getName());
    }
}
 
Example #30
Source File: EventHandlingImpl.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<CacheEntryListenerConfiguration<K,V>> getAllListenerConfigurations() {
  Collection<Listener<K, V>> l = getAllListeners();
  Set<CacheEntryListenerConfiguration<K,V>> _cfgs = new HashSet<CacheEntryListenerConfiguration<K, V>>();
  for (Listener<K,V> li : l) {
    _cfgs.add(li.config);
  }
  return _cfgs;
}