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

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

package io.scalecube.services.routing;

import io.scalecube.services.registry.api.ServiceRegistry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

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

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

  public RouterFactory(ServiceRegistry serviceRegistry) {
    this.serviceRegistry = serviceRegistry;
  }

  /**
   * get router instance by a given router class.
   * 
   * @param routing the type of the Router.
   * @return instance of the Router.
   */
  public Router getRouter(Class routing) {
    return routers.computeIfAbsent(routing, this::create);
  }

  private Router create(Class routing) {
    try {
      return routing.getDeclaredConstructor(ServiceRegistry.class).newInstance(serviceRegistry);
    } catch (Exception ex) {
      LOGGER.error("create router type: {} failed: {}", routing, ex);
      return null;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy