All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.notifications.cachelistener.cluster.ClusterEventCallable Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.notifications.cachelistener.cluster;

import org.infinispan.Cache;
import org.infinispan.commons.marshall.AbstractExternalizer;
import org.infinispan.commons.util.Util;
import org.infinispan.distexec.DistributedCallable;
import org.infinispan.marshall.core.Ids;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;

/**
 * This DistributedCallable is used to invoke a raised notification on the cluster listener that registered to listen
 * for this event.
 *
 * @author wburns
 * @since 7.0
 */
public class ClusterEventCallable implements DistributedCallable {

   private static final Log log = LogFactory.getLog(ClusterEventCallable.class);
   private static final boolean trace = log.isTraceEnabled();

   private transient ClusterCacheNotifier clusterCacheNotifier;

   private final UUID identifier;
   private final Collection> events;

   public ClusterEventCallable(UUID identifier, ClusterEvent event) {
      this(identifier, Collections.singleton(event));
   }

   public ClusterEventCallable(UUID identifier, Collection> events) {
      this.identifier = identifier;
      this.events = events;
   }

   @Override
   public Void call() throws Exception {
      if (trace) {
         log.tracef("Received cluster event(s) %s, notifying cluster listener with id %s", events, identifier);
      }
      clusterCacheNotifier.notifyClusterListeners(events, identifier);
      return null;
   }

   @Override
   public void setEnvironment(Cache cache, Set inputKeys) {
      this.clusterCacheNotifier = cache.getAdvancedCache().getComponentRegistry().getComponent(ClusterCacheNotifier.class);
      for (ClusterEvent event : events) {
         event.cache = cache;
      }
   }

   public static class Externalizer extends AbstractExternalizer {
      @Override
      public Set> getTypeClasses() {
         return Util.>asSet(ClusterEventCallable.class);
      }

      @Override
      public void writeObject(ObjectOutput output, ClusterEventCallable object) throws IOException {
         output.writeObject(object.identifier);
         output.writeObject(object.events);
      }

      @Override
      public ClusterEventCallable readObject(ObjectInput input) throws IOException, ClassNotFoundException {
         return new ClusterEventCallable((UUID)input.readObject(), (Collection)input.readObject());
      }

      @Override
      public Integer getId() {
         return Ids.CLUSTER_EVENT_CALLABLE;
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy