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

org.infinispan.client.hotrod.event.impl.EventDispatcher Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.client.hotrod.event.impl;

import java.net.SocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;

public abstract class EventDispatcher {
   static final Log log = LogFactory.getLog(EventDispatcher.class);
   private static final AtomicReferenceFieldUpdater statusUpdater
         = AtomicReferenceFieldUpdater.newUpdater(EventDispatcher.class, DispatcherStatus.class, "status");

   final Object listener;
   final byte[] listenerId;
   final SocketAddress address;
   final String cacheName;
   final Runnable cleanup;
   private volatile DispatcherStatus status = DispatcherStatus.STOPPED;

   EventDispatcher(String cacheName, Object listener, byte[] listenerId, SocketAddress address, Runnable cleanup) {
      this.listener = listener;
      this.listenerId = listenerId;
      this.address = address;
      this.cacheName = cacheName;
      this.cleanup = cleanup;
   }

   public boolean start() {
      return statusUpdater.compareAndSet(this, EventDispatcher.DispatcherStatus.STOPPED, EventDispatcher.DispatcherStatus.RUNNING);
   }

   public boolean stop() {
      boolean stopped = statusUpdater.compareAndSet(this, DispatcherStatus.RUNNING, DispatcherStatus.STOPPED);
      if (stopped && cleanup != null) {
         cleanup.run();
      }
      return stopped;
   }

   public boolean isRunning() {
      return status == DispatcherStatus.RUNNING;
   }

   public abstract CompletableFuture executeFailover();

   protected abstract void invokeEvent(T event);

   protected void invokeFailoverEvent() {}

   public SocketAddress address() {
      return address;
   }

   enum DispatcherStatus {
      STOPPED,
      RUNNING
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy