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

io.vertx.core.eventbus.impl.HandlerHolder Maven / Gradle / Ivy

There is a newer version: 4.5.10
Show newest version
package io.vertx.core.eventbus.impl;

import io.vertx.core.Context;
import io.vertx.core.spi.metrics.EventBusMetrics;

/**
 * @author Tim Fox
 */
public class HandlerHolder {

  private final EventBusMetrics metrics;
  private final Context context;
  private final HandlerRegistration handler;
  private final boolean replyHandler;
  private final boolean localOnly;
  private boolean removed;

  public HandlerHolder(EventBusMetrics metrics, HandlerRegistration handler, boolean replyHandler, boolean localOnly,
                       Context context) {
    this.metrics = metrics;
    this.context = context;
    this.handler = handler;
    this.replyHandler = replyHandler;
    this.localOnly = localOnly;
  }

  // We use a synchronized block to protect removed as it can be unregistered from a different thread
  public void setRemoved() {
    boolean unregisterMetric = false;
    synchronized (this) {
      if (!removed) {
        removed = true;
        unregisterMetric = true;
      }
    }
    if (unregisterMetric) {
      metrics.handlerUnregistered(handler.getMetric());
    }
  }

  // Because of biased locks the overhead of the synchronized lock should be very low as it's almost always
  // called by the same event loop
  public synchronized boolean isRemoved() {
    return removed;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    HandlerHolder that = (HandlerHolder) o;
    if (handler != null ? !handler.equals(that.handler) : that.handler != null) return false;
    return true;
  }

  @Override
  public int hashCode() {
    return handler != null ? handler.hashCode() : 0;
  }

  public Context getContext() {
    return context;
  }

  public HandlerRegistration getHandler() {
    return handler;
  }

  public boolean isReplyHandler() {
    return replyHandler;
  }

  public boolean isLocalOnly() {
    return localOnly;
  }
;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy