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();
 }
Пример #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());
   }
 }
Пример #3
0
 String addClientListener(ClientListener clientListener) {
   final EventRegistration registration =
       nodeEngine
           .getEventService()
           .registerLocalListener(SERVICE_NAME, SERVICE_NAME, clientListener);
   return registration.getId();
 }
Пример #4
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());
   }
 }
  @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;
  }
Пример #6
0
 public String addEventListener(
     EntryListener entryListener, EventFilter eventFilter, String mapName) {
   EventRegistration registration =
       nodeEngine
           .getEventService()
           .registerListener(SERVICE_NAME, mapName, eventFilter, entryListener);
   return registration.getId();
 }
 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();
 }
  @Override
  public void publishEvent(
      Address caller,
      String mapName,
      EntryEventType eventType,
      boolean syntheticEvent,
      final Data dataKey,
      Data dataOldValue,
      Data dataValue,
      Data dataMergingValue) {
    final Collection<EventRegistration> registrations = getRegistrations(mapName);
    if (registrations.isEmpty()) {
      return;
    }

    List<EventRegistration> registrationsWithValue = null;
    List<EventRegistration> registrationsWithoutValue = null;

    for (final EventRegistration candidate : registrations) {
      final EventFilter filter = candidate.getFilter();
      final Result result =
          applyEventFilter(filter, syntheticEvent, dataKey, dataOldValue, dataValue, eventType);

      registrationsWithValue = initRegistrationsWithValue(registrationsWithValue, result);
      registrationsWithoutValue = initRegistrationsWithoutValue(registrationsWithoutValue, result);

      registerCandidate(result, candidate, registrationsWithValue, registrationsWithoutValue);
    }

    final boolean withValueRegistrationExists = isNotEmpty(registrationsWithValue);
    final boolean withoutValueRegistrationExists = isNotEmpty(registrationsWithoutValue);

    if (!withValueRegistrationExists && !withoutValueRegistrationExists) {
      return;
    }

    final EntryEventData eventData =
        createEntryEventData(
            mapName,
            caller,
            dataKey,
            dataValue,
            dataOldValue,
            dataMergingValue,
            eventType.getType());
    final int orderKey = pickOrderKey(dataKey);

    if (withValueRegistrationExists) {
      publishEventInternal(registrationsWithValue, eventData, orderKey);
    }
    if (withoutValueRegistrationExists) {
      publishEventInternal(registrationsWithoutValue, eventData.cloneWithoutValues(), orderKey);
    }
  }
Пример #9
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();
 }
 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());
   }
 }
Пример #11
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());
   }
 }
  @Override
  public void publishMapPartitionLostEvent(Address caller, String mapName, int partitionId) {
    final Collection<EventRegistration> registrations = new LinkedList<EventRegistration>();
    for (EventRegistration registration : getRegistrations(mapName)) {
      if (registration.getFilter() instanceof MapPartitionLostEventFilter) {
        registrations.add(registration);
      }
    }

    if (registrations.isEmpty()) {
      return;
    }

    final String thisNodesAddress = getThisNodesAddress();
    final MapPartitionEventData eventData =
        new MapPartitionEventData(thisNodesAddress, mapName, caller, partitionId);
    publishEventInternal(registrations, eventData, partitionId);
  }
Пример #13
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 publishMapEvent(
      Address caller, String mapName, EntryEventType eventType, int numberOfEntriesAffected) {
    final Collection<EventRegistration> registrations = new LinkedList<EventRegistration>();
    for (EventRegistration registration : getRegistrations(mapName)) {
      if (!(registration.getFilter() instanceof MapPartitionLostEventFilter)) {
        registrations.add(registration);
      }
    }

    if (registrations.isEmpty()) {
      return;
    }

    final String source = getThisNodesAddress();
    final MapEventData mapEventData =
        new MapEventData(source, mapName, caller, eventType.getType(), numberOfEntriesAffected);

    publishEventInternal(registrations, mapEventData, mapName.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();
   }
 }
 public void publishEvent(
     Address caller,
     String mapName,
     EntryEventType eventType,
     final Data dataKey,
     Data dataOldValue,
     Data dataValue) {
   final Collection<EventRegistration> candidates = getCandidates(mapName);
   if (candidates.isEmpty()) {
     return;
   }
   final Set<EventRegistration> registrationsWithValue = new HashSet<EventRegistration>();
   final Set<EventRegistration> registrationsWithoutValue = new HashSet<EventRegistration>();
   // iterate on candidates.
   for (final EventRegistration candidate : candidates) {
     Result result = Result.NONE;
     final EventFilter filter = candidate.getFilter();
     if (emptyFilter(filter)) {
       result = processEmptyFilter();
     } else if (queryEventFilter(filter)) {
       result = processQueryEventFilter(filter, eventType, dataKey, dataOldValue, dataValue);
     } else if (filter.eval(dataKey)) {
       result = processEntryEventFilter(filter);
     }
     registerCandidate(result, candidate, registrationsWithValue, registrationsWithoutValue);
   }
   if (registrationsWithValue.isEmpty() && registrationsWithoutValue.isEmpty()) {
     return;
   }
   final EntryEventData eventData =
       createEntryEventData(
           mapName, caller, dataKey, dataValue, dataOldValue, eventType.getType());
   final int orderKey = pickOrderKey(dataKey);
   publishWithValue(registrationsWithValue, eventData, orderKey);
   publishWithoutValue(registrationsWithoutValue, eventData, orderKey);
 }
 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;
 }
Пример #18
0
 public void publishEvent(
     Address caller,
     String mapName,
     EntryEventType eventType,
     Data dataKey,
     Data dataOldValue,
     Data dataValue) {
   Collection<EventRegistration> candidates =
       nodeEngine.getEventService().getRegistrations(SERVICE_NAME, mapName);
   Set<EventRegistration> registrationsWithValue = new HashSet<EventRegistration>();
   Set<EventRegistration> registrationsWithoutValue = new HashSet<EventRegistration>();
   if (candidates.isEmpty()) return;
   Object key = null;
   Object value = null;
   Object oldValue = null;
   for (EventRegistration candidate : candidates) {
     EventFilter filter = candidate.getFilter();
     if (filter instanceof EventServiceImpl.EmptyFilter) {
       registrationsWithValue.add(candidate);
     } else if (filter instanceof QueryEventFilter) {
       Object testValue;
       if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) {
         oldValue = oldValue != null ? oldValue : toObject(dataOldValue);
         testValue = oldValue;
       } else {
         value = value != null ? value : toObject(dataValue);
         testValue = value;
       }
       key = key != null ? key : toObject(dataKey);
       QueryEventFilter queryEventFilter = (QueryEventFilter) filter;
       QueryEntry entry = new QueryEntry(getSerializationService(), dataKey, key, testValue);
       if (queryEventFilter.eval(entry)) {
         if (queryEventFilter.isIncludeValue()) {
           registrationsWithValue.add(candidate);
         } else {
           registrationsWithoutValue.add(candidate);
         }
       }
     } else if (filter.eval(dataKey)) {
       EntryEventFilter eventFilter = (EntryEventFilter) filter;
       if (eventFilter.isIncludeValue()) {
         registrationsWithValue.add(candidate);
       } else {
         registrationsWithoutValue.add(candidate);
       }
     }
   }
   if (registrationsWithValue.isEmpty() && registrationsWithoutValue.isEmpty()) return;
   String source = nodeEngine.getThisAddress().toString();
   if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) {
     dataValue = dataValue != null ? dataValue : dataOldValue;
   }
   EventData event =
       new EventData(
           source, mapName, caller, dataKey, dataValue, dataOldValue, eventType.getType());
   int orderKey = dataKey.hashCode();
   nodeEngine
       .getEventService()
       .publishEvent(SERVICE_NAME, registrationsWithValue, event, orderKey);
   nodeEngine
       .getEventService()
       .publishEvent(
           SERVICE_NAME, registrationsWithoutValue, event.cloneWithoutValues(), orderKey);
 }