Search in sources :

Example 1 with CacheEventType

use of com.hazelcast.cache.CacheEventType in project hazelcast by hazelcast.

the class CacheEventListenerAdaptor method handleEvent.

private void handleEvent(int type, Collection<CacheEventData> keys) {
    final Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvent = createCacheEntryEvent(keys);
    CacheEventType eventType = CacheEventType.getByType(type);
    switch(eventType) {
        case CREATED:
            if (this.cacheEntryCreatedListener != null) {
                this.cacheEntryCreatedListener.onCreated(cacheEntryEvent);
            }
            break;
        case UPDATED:
            if (this.cacheEntryUpdatedListener != null) {
                this.cacheEntryUpdatedListener.onUpdated(cacheEntryEvent);
            }
            break;
        case REMOVED:
            if (this.cacheEntryRemovedListener != null) {
                this.cacheEntryRemovedListener.onRemoved(cacheEntryEvent);
            }
            break;
        case EXPIRED:
            if (this.cacheEntryExpiredListener != null) {
                this.cacheEntryExpiredListener.onExpired(cacheEntryEvent);
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid event type: " + eventType.name());
    }
}
Also used : CacheEventType(com.hazelcast.cache.CacheEventType) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 2 with CacheEventType

use of com.hazelcast.cache.CacheEventType 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.impl.eventservice.EventRegistration) EventService(com.hazelcast.spi.impl.eventservice.EventService) CacheEventType(com.hazelcast.cache.CacheEventType)

Aggregations

CacheEventType (com.hazelcast.cache.CacheEventType)2 EventRegistration (com.hazelcast.spi.impl.eventservice.EventRegistration)1 EventService (com.hazelcast.spi.impl.eventservice.EventService)1 CacheEntryEvent (javax.cache.event.CacheEntryEvent)1