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

io.scalecube.services.routing.Routers Maven / Gradle / Ivy

package io.scalecube.services.routing;

import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.Exceptions;

public final class Routers {
  private static final Logger LOGGER = LoggerFactory.getLogger(Routers.class);

  private static final ConcurrentHashMap, Router> routers =
      new ConcurrentHashMap<>();

  private Routers() {}

  /**
   * Get router instance by a given router class. The class should have a default constructor.
   * Otherwise no router can be created
   *
   * @param routerType the type of the Router.
   * @return instance of the Router.
   */
  public static Router getRouter(Class routerType) {
    return routers.computeIfAbsent(routerType, Routers::create);
  }

  private static Router create(Class routerType) {
    try {
      return routerType.newInstance();
    } catch (Exception ex) {
      LOGGER.error("Create router type: {} failed: {}", routerType, ex);
      throw Exceptions.propagate(ex);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy