Search in sources :

Example 1 with CacheEntryListener

use of javax.cache.event.CacheEntryListener in project hazelcast by hazelcast.

the class AbstractCacheService method registerListenerInternal.

protected String registerListenerInternal(String name, CacheEventListener listener, EventFilter eventFilter, boolean isLocal) {
    EventService eventService = getNodeEngine().getEventService();
    EventRegistration reg;
    if (isLocal) {
        if (eventFilter == null) {
            reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, listener);
        } else {
            reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
        }
    } else {
        if (eventFilter == null) {
            reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, listener);
        } else {
            reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
        }
    }
    String id = reg.getId();
    if (listener instanceof Closeable) {
        closeableListeners.put(id, (Closeable) listener);
    } else if (listener instanceof CacheEntryListenerProvider) {
        CacheEntryListener cacheEntryListener = ((CacheEntryListenerProvider) listener).getCacheEntryListener();
        if (cacheEntryListener instanceof Closeable) {
            closeableListeners.put(id, (Closeable) cacheEntryListener);
        }
    }
    return id;
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) CacheEntryListener(javax.cache.event.CacheEntryListener) Closeable(java.io.Closeable) EventService(com.hazelcast.spi.EventService)

Example 2 with CacheEntryListener

use of javax.cache.event.CacheEntryListener in project ignite by apache.

the class IgniteCacheEntryListenerAbstractTest method testSynchronousEvents.

/**
     * @throws Exception If failed.
     */
public void testSynchronousEvents() throws Exception {
    final CacheEntryCreatedListener<Object, Object> lsnr = new CreateUpdateRemoveExpireListener() {

        @Override
        public void onRemoved(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onRemoved(evts);
            awaitLatch();
        }

        @Override
        public void onCreated(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onCreated(evts);
            awaitLatch();
        }

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onUpdated(evts);
            awaitLatch();
        }

        private void awaitLatch() {
            try {
                assertTrue(syncEvtLatch.await(5000, MILLISECONDS));
            } catch (InterruptedException e) {
                fail("Unexpected exception: " + e);
            }
        }
    };
    CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {

        @Override
        public CacheEntryListener<Object, Object> create() {
            return lsnr;
        }
    }, null, true, true);
    IgniteCache<Object, Object> cache = jcache();
    cache.registerCacheEntryListener(lsnrCfg);
    try {
        for (Integer key : keys()) {
            log.info("Check synchronous create event [key=" + key + ']');
            syncEvent(key, 1, cache, 1);
            checkEvent(evts.iterator(), key, CREATED, 1, null);
            log.info("Check synchronous update event [key=" + key + ']');
            syncEvent(key, 2, cache, 1);
            checkEvent(evts.iterator(), key, UPDATED, 2, 1);
            log.info("Check synchronous remove event [key=" + key + ']');
            syncEvent(key, null, cache, 1);
            checkEvent(evts.iterator(), key, REMOVED, null, 2);
            log.info("Check synchronous expire event [key=" + key + ']');
            syncEvent(key, 3, cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 1000))), eagerTtl() ? 2 : 1);
            checkEvent(evts.iterator(), key, CREATED, 3, null);
            if (!eagerTtl()) {
                U.sleep(1100);
                assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
                evtsLatch.await(5000, MILLISECONDS);
                assertEquals(1, evts.size());
            }
            checkEvent(evts.iterator(), key, EXPIRED, null, 3);
            assertEquals(0, evts.size());
        }
    } finally {
        cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListener(javax.cache.event.CacheEntryListener) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 3 with CacheEntryListener

use of javax.cache.event.CacheEntryListener in project ignite by apache.

the class IgniteCacheEntryListenerAbstractTest method testConcurrentRegisterDeregister.

/**
     * @throws Exception If failed.
     */
public void testConcurrentRegisterDeregister() throws Exception {
    final int THREADS = 10;
    final CyclicBarrier barrier = new CyclicBarrier(THREADS);
    final IgniteCache<Object, Object> cache = jcache(0);
    GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            CacheEntryListenerConfiguration<Object, Object> cfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {

                @Override
                public CacheEntryListener<Object, Object> create() {
                    return new CreateUpdateRemoveExpireListener();
                }
            }, null, true, false);
            barrier.await();
            for (int i = 0; i < 100; i++) {
                cache.registerCacheEntryListener(cfg);
                cache.deregisterCacheEntryListener(cfg);
            }
            return null;
        }
    }, THREADS, "register-thread").get();
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListener(javax.cache.event.CacheEntryListener) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 4 with CacheEntryListener

use of javax.cache.event.CacheEntryListener in project ignite by apache.

the class GridCacheReplicatedPreloadSelfTest method testExternalClassesAtConfiguration.

/**
     * @throws Exception If test failed.
     */
public void testExternalClassesAtConfiguration() throws Exception {
    try {
        extClassloadingAtCfg = true;
        useExtClassLoader = true;
        Ignite g1 = startGrid(1);
        // Checks deserialization at node join.
        Ignite g2 = startGrid(2);
        isClient = true;
        Ignite g3 = startGrid(3);
        IgniteCache<Integer, Object> cache1 = g1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache2 = g2.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache3 = g3.cache(DEFAULT_CACHE_NAME);
        final Class<CacheEntryListener> cls1 = (Class<CacheEntryListener>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryListener");
        final Class<CacheEntryEventSerializableFilter> cls2 = (Class<CacheEntryEventSerializableFilter>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryEventSerializableFilter");
        CacheEntryListenerConfiguration<Integer, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Integer, Object>>() {

            @Override
            public CacheEntryListener<Integer, Object> create() {
                try {
                    return cls1.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }, new ClassFilterFactory(cls2), true, true);
        cache1.registerCacheEntryListener(lsnrCfg);
        cache1.put(1, 1);
        assertEquals(1, cache2.get(1));
        assertEquals(1, cache3.get(1));
    } finally {
        extClassloadingAtCfg = false;
        isClient = false;
        useExtClassLoader = false;
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryListener(javax.cache.event.CacheEntryListener) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

CacheEntryListener (javax.cache.event.CacheEntryListener)4 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)3 EventRegistration (com.hazelcast.spi.EventRegistration)1 EventService (com.hazelcast.spi.EventService)1 Closeable (java.io.Closeable)1 Callable (java.util.concurrent.Callable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Duration (javax.cache.expiry.Duration)1 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)1 Ignite (org.apache.ignite.Ignite)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)1