Search in sources :

Example 1 with EventService

use of com.hazelcast.spi.EventService in project hazelcast by hazelcast.

the class QueueService method addItemListener.

public String addItemListener(String name, ItemListener listener, boolean includeValue, boolean isLocal) {
    EventService eventService = nodeEngine.getEventService();
    QueueEventFilter filter = new QueueEventFilter(includeValue);
    EventRegistration registration;
    if (isLocal) {
        registration = eventService.registerLocalListener(QueueService.SERVICE_NAME, name, filter, listener);
    } else {
        registration = eventService.registerListener(QueueService.SERVICE_NAME, name, filter, listener);
    }
    return registration.getId();
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) EventService(com.hazelcast.spi.EventService)

Example 2 with EventService

use of com.hazelcast.spi.EventService in project hazelcast by hazelcast.

the class ClientMapIssueTest method testListenerRegistrations.

@Test
public void testListenerRegistrations() throws Exception {
    Config config = getConfig();
    HazelcastInstance instance1 = hazelcastFactory.newHazelcastInstance(config);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final String mapName = randomMapName();
    IMap<Object, Object> map = client.getMap(mapName);
    map.addEntryListener(new EntryAdapter<Object, Object>(), true);
    HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance(config);
    instance1.getLifecycleService().terminate();
    instance1 = hazelcastFactory.newHazelcastInstance(config);
    final EventService eventService1 = TestUtil.getNode(instance1).nodeEngine.getEventService();
    final EventService eventService2 = TestUtil.getNode(instance2).nodeEngine.getEventService();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            Collection<EventRegistration> regs1 = eventService1.getRegistrations(MapService.SERVICE_NAME, mapName);
            Collection<EventRegistration> regs2 = eventService2.getRegistrations(MapService.SERVICE_NAME, mapName);
            assertEquals("there should be only one registration", 1, regs1.size());
            assertEquals("there should be only one registration", 1, regs2.size());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) AssertTask(com.hazelcast.test.AssertTask) Collection(java.util.Collection) EventService(com.hazelcast.spi.EventService) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with EventService

use of com.hazelcast.spi.EventService 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 4 with EventService

use of com.hazelcast.spi.EventService in project hazelcast by hazelcast.

the class AbstractCacheService method addInvalidationListener.

/**
     * Registers and {@link com.hazelcast.cache.impl.CacheEventListener} for specified <code>cacheName</code>.
     *
     * @param name      the name of the cache that {@link com.hazelcast.cache.impl.CacheEventListener} will be registered for
     * @param listener  the {@link com.hazelcast.cache.impl.CacheEventListener} to be registered for specified <code>cache</code>
     * @param localOnly true if only events originated from this member wants be listened, false if all invalidation events in the
     *                  cluster wants to be listened
     * @return the id which is unique for current registration
     */
@Override
public String addInvalidationListener(String name, CacheEventListener listener, boolean localOnly) {
    EventService eventService = nodeEngine.getEventService();
    EventRegistration registration;
    if (localOnly) {
        registration = eventService.registerLocalListener(SERVICE_NAME, name, listener);
    } else {
        registration = eventService.registerListener(SERVICE_NAME, name, listener);
    }
    return registration.getId();
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) EventService(com.hazelcast.spi.EventService)

Example 5 with EventService

use of com.hazelcast.spi.EventService in project hazelcast by hazelcast.

the class CacheEventHandler method publishEvent.

void publishEvent(CacheEventContext cacheEventContext) {
    final EventService eventService = nodeEngine.getEventService();
    final String cacheName = cacheEventContext.getCacheName();
    final Collection<EventRegistration> candidates = eventService.getRegistrations(SERVICE_NAME, cacheName);
    if (candidates.isEmpty()) {
        return;
    }
    final Object eventData;
    final CacheEventType eventType = cacheEventContext.getEventType();
    switch(eventType) {
        case CREATED:
        case UPDATED:
        case REMOVED:
        case EXPIRED:
            final CacheEventData cacheEventData = new CacheEventDataImpl(cacheName, eventType, cacheEventContext.getDataKey(), cacheEventContext.getDataValue(), cacheEventContext.getDataOldValue(), cacheEventContext.isOldValueAvailable());
            CacheEventSet eventSet = new CacheEventSet(eventType, cacheEventContext.getCompletionId());
            eventSet.addEventData(cacheEventData);
            eventData = eventSet;
            break;
        case EVICTED:
        case INVALIDATED:
            eventData = new CacheEventDataImpl(cacheName, eventType, cacheEventContext.getDataKey(), null, null, false);
            break;
        case COMPLETED:
            CacheEventData completedEventData = new CacheEventDataImpl(cacheName, eventType, cacheEventContext.getDataKey(), cacheEventContext.getDataValue(), null, false);
            eventSet = new CacheEventSet(eventType, cacheEventContext.getCompletionId());
            eventSet.addEventData(completedEventData);
            eventData = eventSet;
            break;
        default:
            throw new IllegalArgumentException("Event Type not defined to create an eventData during publish : " + eventType.name());
    }
    eventService.publishEvent(SERVICE_NAME, candidates, eventData, cacheEventContext.getOrderKey());
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) EventService(com.hazelcast.spi.EventService)

Aggregations

EventService (com.hazelcast.spi.EventService)49 EventRegistration (com.hazelcast.spi.EventRegistration)37 Address (com.hazelcast.nio.Address)5 NodeEngine (com.hazelcast.spi.NodeEngine)5 QueryCacheEventService (com.hazelcast.map.impl.querycache.QueryCacheEventService)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 ClientEndpoint (com.hazelcast.client.ClientEndpoint)3 CollectionEventFilter (com.hazelcast.collection.impl.collection.CollectionEventFilter)3 Config (com.hazelcast.config.Config)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 MapEventData (com.hazelcast.map.impl.event.MapEventData)3 Data (com.hazelcast.nio.serialization.Data)3 CachePartitionLostEventFilter (com.hazelcast.cache.impl.event.CachePartitionLostEventFilter)2 CachePartitionLostListener (com.hazelcast.cache.impl.event.CachePartitionLostListener)2 InitialMembershipEvent (com.hazelcast.core.InitialMembershipEvent)2 ItemListener (com.hazelcast.core.ItemListener)2 Member (com.hazelcast.core.Member)2