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

org.ehcache.impl.internal.events.AbstractStoreEventDispatcher Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ehcache.impl.internal.events;

import org.ehcache.core.events.StoreEventDispatcher;
import org.ehcache.core.events.StoreEventSink;
import org.ehcache.core.spi.store.events.StoreEventFilter;
import org.ehcache.core.spi.store.events.StoreEventListener;

import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Supplier;

/**
 * AbstractStoreEventDispatcher
 */
abstract class AbstractStoreEventDispatcher implements StoreEventDispatcher {

  protected static final StoreEventSink NO_OP_EVENT_SINK = new CloseableStoreEventSink() {
    @Override
    public void close() {
      // Do nothing
    }

    @Override
    public void closeOnFailure() {
      // Do nothing
    }

    @Override
    public void reset() {
      // Do nothing
    }

    @Override
    public void removed(Object key, Supplier value) {
      // Do nothing
    }

    @Override
    public void updated(Object key, Supplier oldValue, Object newValue) {
      // Do nothing
    }

    @Override
    public void expired(Object key, Supplier value) {
      // Do nothing
    }

    @Override
    public void created(Object key, Object value) {
      // Do nothing
    }

    @Override
    public void evicted(Object key, Supplier value) {
      // Do nothing
    }
  };

  private final Set> filters = new CopyOnWriteArraySet<>();
  private final Set> listeners = new CopyOnWriteArraySet<>();
  private final BlockingQueue>[] orderedQueues;
  private volatile boolean ordered = false;

  protected AbstractStoreEventDispatcher(int dispatcherConcurrency) {
    if (dispatcherConcurrency <= 0) {
      throw new IllegalArgumentException("Dispatcher concurrency must be an integer greater than 0");
    }
    @SuppressWarnings("unchecked")
    LinkedBlockingQueue>[] queues = new LinkedBlockingQueue[dispatcherConcurrency];
    orderedQueues = queues;
    for (int i = 0; i < orderedQueues.length; i++) {
      orderedQueues[i] = new LinkedBlockingQueue<>(10000);
    }
  }

  protected Set> getListeners() {
    return listeners;
  }

  protected Set> getFilters() {
    return filters;
  }

  protected BlockingQueue>[] getOrderedQueues() {
    return orderedQueues;
  }

  @Override
  public void addEventListener(StoreEventListener eventListener) {
    listeners.add(eventListener);
  }

  @Override
  public void removeEventListener(StoreEventListener eventListener) {
    listeners.remove(eventListener);
  }

  @Override
  public void addEventFilter(StoreEventFilter eventFilter) {
    filters.add(eventFilter);
  }

  @Override
  public void setEventOrdering(boolean ordering) {
    this.ordered = ordering;
  }

  @Override
  public boolean isEventOrdering() {
    return ordered;
  }

  @Override
  public void releaseEventSink(StoreEventSink eventSink) {
    ((CloseableStoreEventSink) eventSink).close();
  }

  @Override
  public void releaseEventSinkAfterFailure(StoreEventSink eventSink, Throwable throwable) {
    ((CloseableStoreEventSink) eventSink).closeOnFailure();
  }

  @Override
  public void reset(StoreEventSink eventSink) {
    ((CloseableStoreEventSink) eventSink).reset();
  }
}