public String addItemListener(ItemListener<E> listener, boolean includeValue) {
   final EventService eventService = getNodeEngine().getEventService();
   final EventRegistration registration =
       eventService.registerListener(
           getServiceName(), name, new CollectionEventFilter(includeValue), listener);
   return registration.getId();
 }
Ejemplo n.º 2
0
 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());
   }
 }
Ejemplo n.º 3
0
 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());
 }
  @Override
  public Object call() throws Exception {
    final ClientEndpoint endpoint = getEndpoint();
    final ClientEngine clientEngine = getClientEngine();

    ItemListener listener =
        new ItemListener() {
          @Override
          public void itemAdded(ItemEvent item) {
            send(item);
          }

          @Override
          public void itemRemoved(ItemEvent item) {
            send(item);
          }

          private void send(ItemEvent event) {
            if (endpoint.live()) {
              Data item = serializationService.toData(event.getItem());
              final ItemEventType eventType = event.getEventType();
              final String uuid = event.getMember().getUuid();
              PortableItemEvent portableItemEvent = new PortableItemEvent(item, eventType, uuid);
              endpoint.sendEvent(portableItemEvent, getCallId());
            }
          }
        };
    final EventService eventService = clientEngine.getEventService();
    final CollectionEventFilter filter = new CollectionEventFilter(includeValue);
    final EventRegistration registration =
        eventService.registerListener(getServiceName(), name, filter, listener);
    final String registrationId = registration.getId();
    endpoint.setListenerRegistration(getServiceName(), name, registrationId);
    return registrationId;
  }
  /**
   * - Sends eviction event. - Invalidates near cache.
   *
   * @param key the key to be processed.
   * @param value the value to be processed.
   */
  @Override
  public void doPostEvictionOperations(Data key, Object value, boolean isExpired) {
    MapEventPublisher mapEventPublisher = mapServiceContext.getMapEventPublisher();
    NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
    EventService eventService = nodeEngine.getEventService();

    if (!eventService.hasEventRegistration(SERVICE_NAME, name)) {
      return;
    }

    Address thisAddress = nodeEngine.getThisAddress();

    // Fire EVICTED event also in case of expiration because historically eviction-listener
    // listens all kind of eviction and expiration events and by firing EVICTED event we are
    // preserving
    // this behavior.
    mapEventPublisher.publishEvent(thisAddress, name, EVICTED, key, value, null);

    if (isExpired) {
      // We will be in this if in two cases:
      // 1. In case of TTL or max-idle-seconds expiration.
      // 2. When evicting due to the size-based eviction, we are also firing an EXPIRED event
      //    because there is a possibility that evicted entry may be also an expired one. Trying to
      // catch
      //    as much as possible expired entries.
      mapEventPublisher.publishEvent(thisAddress, name, EXPIRED, key, value, null);
    }
  }
Ejemplo n.º 6
0
 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());
   }
 }
Ejemplo n.º 7
0
 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);
  }
Ejemplo n.º 9
0
 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 publishEventInternal(
      Collection<EventRegistration> registrations, Object eventData, int orderKey) {
    final MapServiceContext mapServiceContext = this.mapServiceContext;
    final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
    final EventService eventService = nodeEngine.getEventService();

    eventService.publishEvent(SERVICE_NAME, registrations, eventData, orderKey);
  }
Ejemplo n.º 11
0
 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);
 }
 @Override
 public boolean deregisterListener(String name, String registrationId) {
   final EventService eventService = getNodeEngine().getEventService();
   boolean result = eventService.deregisterListener(SERVICE_NAME, name, registrationId);
   Closeable listener = closeableListeners.remove(registrationId);
   if (listener != null) {
     IOUtil.closeResource(listener);
   }
   return result;
 }
Ejemplo n.º 13
0
  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());
  }
Ejemplo n.º 14
0
 public String addListener(
     String name, EventListener listener, Data key, boolean includeValue, boolean local) {
   EventService eventService = nodeEngine.getEventService();
   EventRegistration registration;
   final MultiMapEventFilter filter = new MultiMapEventFilter(includeValue, key);
   if (local) {
     registration = eventService.registerLocalListener(SERVICE_NAME, name, filter, listener);
   } else {
     registration = eventService.registerListener(SERVICE_NAME, name, filter, listener);
   }
   return registration.getId();
 }
Ejemplo n.º 15
0
 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());
   }
 }
Ejemplo n.º 16
0
 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());
   }
 }
 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 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 String addEventListener(
     EventListener entryListener, EventFilter eventFilter, String mapName) {
   if (config.isLiteMember()) {
     throw new ReplicatedMapCantBeCreatedOnLiteMemberException(nodeEngine.getThisAddress());
   }
   EventRegistration registration =
       eventService.registerLocalListener(SERVICE_NAME, mapName, eventFilter, entryListener);
   return registration.getId();
 }
 public boolean removeEventListener(String mapName, String registrationId) {
   if (config.isLiteMember()) {
     throw new ReplicatedMapCantBeCreatedOnLiteMemberException(nodeEngine.getThisAddress());
   }
   if (registrationId == null) {
     throw new IllegalArgumentException("registrationId cannot be null");
   }
   return eventService.deregisterListener(SERVICE_NAME, mapName, registrationId);
 }
Ejemplo n.º 21
0
  public String addMembershipListener(MembershipListener listener) {
    checkNotNull(listener, "listener cannot be null");

    EventService eventService = nodeEngine.getEventService();
    EventRegistration registration;
    if (listener instanceof InitialMembershipListener) {
      lock.lock();
      try {
        ((InitialMembershipListener) listener).init(new InitialMembershipEvent(this, getMembers()));
        registration = eventService.registerLocalListener(SERVICE_NAME, SERVICE_NAME, listener);
      } finally {
        lock.unlock();
      }
    } else {
      registration = eventService.registerLocalListener(SERVICE_NAME, SERVICE_NAME, listener);
    }

    return registration.getId();
  }
 @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();
   }
 }
Ejemplo n.º 23
0
  @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());
  }
Ejemplo n.º 24
0
 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);
   }
 }
 protected String registerListenerInternal(
     String name, CacheEventListener listener, EventFilter eventFilter) {
   final EventService eventService = getNodeEngine().getEventService();
   final EventRegistration registration;
   if (eventFilter == null) {
     registration =
         eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, listener);
   } else {
     registration =
         eventService.registerListener(
             AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
   }
   final String id = registration.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;
 }
 protected boolean hasRegisteredListenerForThisMap() {
   final EventService eventService = getNodeEngine().getEventService();
   return eventService.hasEventRegistration(SERVICE_NAME, name);
 }
 public SerializableEventServiceBean(EventService es) {
   this.eventThreadCount = es.getEventThreadCount();
   this.eventQueueCapacity = es.getEventQueueCapacity();
   this.eventQueueSize = es.getEventQueueSize();
 }
Ejemplo n.º 28
0
 public boolean removeListener(String name, String registrationId) {
   EventService eventService = nodeEngine.getEventService();
   return eventService.deregisterListener(SERVICE_NAME, name, registrationId);
 }
Ejemplo n.º 29
0
  public boolean removeMembershipListener(String registrationId) {
    checkNotNull(registrationId, "registrationId cannot be null");

    EventService eventService = nodeEngine.getEventService();
    return eventService.deregisterListener(SERVICE_NAME, SERVICE_NAME, registrationId);
  }
 public boolean removeItemListener(String registrationId) {
   EventService eventService = getNodeEngine().getEventService();
   return eventService.deregisterListener(getServiceName(), name, registrationId);
 }