private void sendMemberAttributeEvent( MemberImpl member, MemberAttributeOperationType operationType, String key, Object value) { final MemberAttributeServiceEvent event = new MemberAttributeServiceEvent(this, member, operationType, key, value); MemberAttributeEvent attributeEvent = new MemberAttributeEvent(this, member, operationType, key, value); Collection<MembershipAwareService> membershipAwareServices = nodeEngine.getServices(MembershipAwareService.class); if (membershipAwareServices != null && !membershipAwareServices.isEmpty()) { for (final MembershipAwareService service : membershipAwareServices) { // service events should not block each other nodeEngine .getExecutionService() .execute( ExecutionService.SYSTEM_EXECUTOR, new Runnable() { public void run() { service.memberAttributeChanged(event); } }); } } EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME); for (EventRegistration reg : registrations) { eventService.publishEvent(SERVICE_NAME, reg, attributeEvent, reg.getId().hashCode()); } }
void sendClientEvent(ClientEvent event) { final EventService eventService = nodeEngine.getEventService(); final Collection<EventRegistration> regs = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME); String uuid = event.getUuid(); eventService.publishEvent(SERVICE_NAME, regs, event, uuid.hashCode()); }
private void sendMembershipEventNotifications( MemberImpl member, Set<Member> members, final boolean added) { int eventType = added ? MembershipEvent.MEMBER_ADDED : MembershipEvent.MEMBER_REMOVED; MembershipEvent membershipEvent = new MembershipEvent(this, member, eventType, members); Collection<MembershipAwareService> membershipAwareServices = nodeEngine.getServices(MembershipAwareService.class); if (membershipAwareServices != null && !membershipAwareServices.isEmpty()) { final MembershipServiceEvent event = new MembershipServiceEvent(membershipEvent); for (final MembershipAwareService service : membershipAwareServices) { nodeEngine .getExecutionService() .execute( MEMBERSHIP_EVENT_EXECUTOR_NAME, new Runnable() { public void run() { if (added) { service.memberAdded(event); } else { service.memberRemoved(event); } } }); } } EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME); for (EventRegistration reg : registrations) { eventService.publishEvent(SERVICE_NAME, reg, membershipEvent, reg.getId().hashCode()); } }
private void flushInvalidationMessages( String cacheName, InvalidationEventQueue invalidationMessageQueue) { // If still in progress, no need to another attempt. So just ignore. if (invalidationMessageQueue.tryAcquire()) { try { CacheBatchInvalidationMessage batchInvalidationMessage = new CacheBatchInvalidationMessage(cacheName, invalidationMessageQueue.size()); CacheSingleInvalidationMessage invalidationMessage; final int size = invalidationMessageQueue.size(); // At most, poll from the invalidation queue as the current size of the queue before start // to polling. // So skip new invalidation queue items offered while the polling in progress in this round. for (int i = 0; i < size; i++) { invalidationMessage = invalidationMessageQueue.poll(); if (invalidationMessage == null) { break; } batchInvalidationMessage.addInvalidationMessage(invalidationMessage); } EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(ICacheService.SERVICE_NAME, cacheName); if (!registrations.isEmpty()) { eventService.publishEvent( ICacheService.SERVICE_NAME, registrations, batchInvalidationMessage, cacheName.hashCode()); } } finally { invalidationMessageQueue.release(); } } }
Collection<EventRegistration> getRegistrations(String mapName) { final MapServiceContext mapServiceContext = this.mapServiceContext; final NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); final EventService eventService = nodeEngine.getEventService(); return eventService.getRegistrations(SERVICE_NAME, mapName); }
private void sendClientEvent(ClientEndpoint endpoint) { if (endpoint.isFirstConnection()) { final EventService eventService = nodeEngine.getEventService(); final Collection<EventRegistration> regs = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME); eventService.publishEvent(SERVICE_NAME, regs, endpoint, endpoint.getUuid().hashCode()); } }
void publishEvent(String cacheName, CacheEventSet eventSet, int orderKey) { final EventService eventService = nodeEngine.getEventService(); final Collection<EventRegistration> candidates = eventService.getRegistrations(ICacheService.SERVICE_NAME, cacheName); if (candidates.isEmpty()) { return; } eventService.publishEvent(ICacheService.SERVICE_NAME, candidates, eventSet, orderKey); }
void publishEvent(CacheEventContext cacheEventContext) { final EventService eventService = nodeEngine.getEventService(); final String cacheName = cacheEventContext.getCacheName(); final Collection<EventRegistration> candidates = eventService.getRegistrations(ICacheService.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( ICacheService.SERVICE_NAME, candidates, eventData, cacheEventContext.getOrderKey()); }
private void sendSingleInvalidationEvent(String name, Data key, String sourceUuid) { EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(ICacheService.SERVICE_NAME, name); if (!registrations.isEmpty()) { eventService.publishEvent( ICacheService.SERVICE_NAME, registrations, new CacheSingleInvalidationMessage(name, key, sourceUuid), name.hashCode()); } }
public void fireEntryListenerEvent( Data key, Data oldValue, Data value, EntryEventType eventType, String name, Address caller) { Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, name); if (registrations.isEmpty()) { return; } EntryEventData eventData = new EntryEventData(name, name, caller, key, value, oldValue, eventType.getType()); for (EventRegistration registration : registrations) { if (!shouldPublish(key, oldValue, value, eventType, registration.getFilter())) { continue; } eventService.publishEvent(SERVICE_NAME, registration, eventData, key.hashCode()); } }
public void fireMapClearedEvent(int deletedEntrySize, String name) { EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, name); if (registrations.isEmpty()) { return; } MapEventData mapEventData = new MapEventData( name, name, nodeEngine.getThisAddress(), EntryEventType.CLEAR_ALL.getType(), deletedEntrySize); eventService.publishEvent(SERVICE_NAME, registrations, mapEventData, name.hashCode()); }
public void publishEvent(ItemEventType eventType, Data data) { EventService eventService = getNodeEngine().getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(getServiceName(), name); for (EventRegistration registration : registrations) { QueueEventFilter filter = (QueueEventFilter) registration.getFilter(); QueueEvent event = new QueueEvent( name, filter.isIncludeValue() ? data : null, eventType, getNodeEngine().getThisAddress()); eventService.publishEvent(getServiceName(), registration, event, name.hashCode()); } }
@Override public void deregisterAllListener(String name) { final EventService eventService = getNodeEngine().getEventService(); final Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, name); if (registrations != null) { for (EventRegistration registration : registrations) { Closeable listener = closeableListeners.remove(registration.getId()); if (listener != null) { IOUtil.closeResource(listener); } } } eventService.deregisterAllListeners(AbstractCacheService.SERVICE_NAME, name); CacheContext cacheContext = cacheContexts.get(name); if (cacheContext != null) { cacheContext.resetCacheEntryListenerCount(); cacheContext.resetInvalidationListenerCount(); } }
@Test public void testListenerRegistrations() throws Exception { final String mapName = "testListener"; HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(); final HazelcastInstance client1 = HazelcastClient.newHazelcastClient(); final IMap<Object, Object> map = client1.getMap(mapName); map.addEntryListener( new EntryListener<Object, Object>() { @Override public void entryAdded(EntryEvent<Object, Object> event) {} @Override public void entryRemoved(EntryEvent<Object, Object> event) {} @Override public void entryUpdated(EntryEvent<Object, Object> event) {} @Override public void entryEvicted(EntryEvent<Object, Object> event) {} @Override public void mapEvicted(MapEvent event) {} @Override public void mapCleared(MapEvent event) {} }, true); HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(); instance1.getLifecycleService().terminate(); instance1 = Hazelcast.newHazelcastInstance(); final Field original = HazelcastInstanceProxy.class.getDeclaredField("original"); original.setAccessible(true); final HazelcastInstanceImpl impl = (HazelcastInstanceImpl) original.get(instance1); final EventService eventService = impl.node.nodeEngine.getEventService(); final Collection<EventRegistration> regs = eventService.getRegistrations(MapService.SERVICE_NAME, mapName); assertEquals("there should be only one registrations", 1, regs.size()); }
private void sendBatchInvalidationEvent(String name, Data key, String sourceUuid) { EventService eventService = nodeEngine.getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(ICacheService.SERVICE_NAME, name); if (registrations.isEmpty()) { return; } InvalidationEventQueue invalidationMessageQueue = invalidationMessageMap.get(name); if (invalidationMessageQueue == null) { InvalidationEventQueue newInvalidationMessageQueue = new InvalidationEventQueue(); invalidationMessageQueue = invalidationMessageMap.putIfAbsent(name, newInvalidationMessageQueue); if (invalidationMessageQueue == null) { invalidationMessageQueue = newInvalidationMessageQueue; } } CacheSingleInvalidationMessage invalidationMessage = new CacheSingleInvalidationMessage(name, key, sourceUuid); invalidationMessageQueue.offer(invalidationMessage); if (invalidationMessageQueue.size() >= invalidationMessageBatchSize) { flushInvalidationMessages(name, invalidationMessageQueue); } }
public boolean hasListener() { EventService eventService = getNodeEngine().getEventService(); Collection<EventRegistration> registrations = eventService.getRegistrations(getServiceName(), name); return registrations.size() > 0; }