Class DefaultServiceRegistry

All Implemented Interfaces:
MetricConstants, MoleculerLifecycle

public class DefaultServiceRegistry extends ServiceRegistry implements MetricConstants
Default implementation of the Service Registry.
  • Field Details

    • middlewares

      protected final LinkedHashSet<Middleware> middlewares
    • services

      protected final LinkedHashMap<String,Service> services
    • strategies

      protected final HashMap<String, Strategy<ActionEndpoint>> strategies
    • names

      protected final HashSet<String> names
    • promises

      protected final ConcurrentHashMap<String, PendingPromise> promises
    • requestStreams

      protected final HashMap<String, IncomingStream> requestStreams
    • responseStreams

      protected final HashMap<String, IncomingStream> responseStreams
    • readLock

      protected final ReentrantReadWriteLock.ReadLock readLock
    • writeLock

      protected final ReentrantReadWriteLock.WriteLock writeLock
    • requestStreamReadLock

      protected final ReentrantReadWriteLock.ReadLock requestStreamReadLock
    • requestStreamWriteLock

      protected final ReentrantReadWriteLock.WriteLock requestStreamWriteLock
    • responseStreamReadLock

      protected final ReentrantReadWriteLock.ReadLock responseStreamReadLock
    • responseStreamWriteLock

      protected final ReentrantReadWriteLock.WriteLock responseStreamWriteLock
    • asyncLocalInvocation

      protected boolean asyncLocalInvocation
      Invoke all local services via Thread pool (true) or directly (false)
    • checkVersion

      protected boolean checkVersion
      Check protocol version
    • protocolVersion

      protected String protocolVersion
      ServiceBroker's protocol version. Placeholder only: overwritten in started(broker) from broker.getProtocolVersion(). The authoritative default lives in ServiceBrokerConfig.DEFAULT_PROTOCOL_VERSION ("5").
    • writeErrorsToLog

      protected boolean writeErrorsToLog
      Write exceptions into the log file
    • writeRegistrations

      protected boolean writeRegistrations
      Write Service and Action registrations into the log file
    • streamTimeout

      protected long streamTimeout
      Stream inactivity/read timeout in MILLISECONDS (0 = no timeout). It may be useful if you want to remove the wrong packages from the memory.
    • nodeID

      protected String nodeID
    • executor

      protected ExecutorService executor
    • scheduler

      protected ScheduledExecutorService scheduler
    • strategyFactory

      protected StrategyFactory strategyFactory
    • transporter

      protected Transporter transporter
    • eventbus

      protected Eventbus eventbus
    • uidGenerator

      protected UidGenerator uidGenerator
    • serviceInvoker

      protected ServiceInvoker serviceInvoker
    • metrics

      protected Metrics metrics
    • callTimeoutTimer

      protected final AtomicReference<ScheduledFuture<?>> callTimeoutTimer
      Cancelable timer for handling timeouts of action calls
    • prevTimeoutAt

      protected final AtomicLong prevTimeoutAt
      Next scheduled time to check timeouts
    • servicesOnlineTimer

      protected ScheduledFuture<?> servicesOnlineTimer
      Cancelable timer for handling "wait for service" calls
    • serviceListeners

      protected final LinkedList<ServiceListener> serviceListeners
      Promises of the "waitingForServices" calls
    • timestamp

      protected AtomicLong timestamp
      Timestamp of the service descriptor of this Moleculer Node (~= "generated at" timestamp)
    • cachedDescriptor

      protected volatile FastBuildTree cachedDescriptor
    • gaugeRequestsActive

      protected MetricGauge gaugeRequestsActive
    • gaugeStreamsReceiveActive

      protected MetricGauge gaugeStreamsReceiveActive
    • counterOrphanResponseTotal

      protected MetricCounter counterOrphanResponseTotal
  • Constructor Details

    • DefaultServiceRegistry

      public DefaultServiceRegistry()
    • DefaultServiceRegistry

      public DefaultServiceRegistry(boolean asyncLocalInvocation)
  • Method Details

    • started

      public void started(ServiceBroker broker) throws Exception
      Initializes ServiceRegistry instance.
      Specified by:
      started in interface MoleculerLifecycle
      Overrides:
      started in class MoleculerComponent
      Parameters:
      broker - parent ServiceBroker
      Throws:
      Exception
    • stopped

      public void stopped()
      Specified by:
      stopped in interface MoleculerLifecycle
      Overrides:
      stopped in class MoleculerComponent
    • checkTimeouts

      protected void checkTimeouts()
    • reschedule

      protected void reschedule(long minTimeoutAt)
      Recalculates the next timeout checking time.
      Parameters:
      minTimeoutAt - next / closest timestamp
    • register

      protected void register(String id, io.datatree.Promise promise, long timeoutAt, String nodeID, String action, io.datatree.Tree req)
    • deregister

      protected void deregister(String id)
    • receiveRequest

      public void receiveRequest(io.datatree.Tree message)
      Specified by:
      receiveRequest in class ServiceRegistry
    • logUnableToInvokeAction

      protected void logUnableToInvokeAction(String action, Throwable cause)
    • receivePong

      public void receivePong(io.datatree.Tree message)
      Specified by:
      receivePong in class ServiceRegistry
    • receiveResponse

      public void receiveResponse(io.datatree.Tree message)
      Specified by:
      receiveResponse in class ServiceRegistry
    • use

      public void use(Collection<Middleware> middlewares)
      Specified by:
      use in class ServiceRegistry
    • addActions

      public io.datatree.Promise addActions(String serviceName, Service service)
      Specified by:
      addActions in class ServiceRegistry
    • addOnlineActions

      protected void addOnlineActions(String serviceName, Service service) throws Exception
      Throws:
      Exception
    • broadcastServicesChanged

      protected void broadcastServicesChanged(boolean local)
    • addActions

      public void addActions(String nodeID, io.datatree.Tree config)
      Specified by:
      addActions in class ServiceRegistry
    • removeActions

      public void removeActions(String nodeID)
      Specified by:
      removeActions in class ServiceRegistry
    • stopAllLocalServices

      protected void stopAllLocalServices()
    • getService

      public Service getService(String name)
      Specified by:
      getService in class ServiceRegistry
    • getAction

      public Action getAction(String name, String nodeID)
      Specified by:
      getAction in class ServiceRegistry
    • waitForServices

      public io.datatree.Promise waitForServices(long timeoutMillis, Collection<String> services)
      Specified by:
      waitForServices in class ServiceRegistry
    • checkServicesOnline

      protected void checkServicesOnline()
    • isServicesOnline

      protected boolean isServicesOnline(Collection<String> requiredServices)
    • countOnlineServices

      protected int countOnlineServices(Collection<String> requiredServices)
    • ping

      public io.datatree.Promise ping(long timeoutMillis, String nodeID)
      Specified by:
      ping in class ServiceRegistry
    • getTimestamp

      public long getTimestamp()
      Specified by:
      getTimestamp in class ServiceRegistry
    • getDescriptor

      public io.datatree.Tree getDescriptor()
      Specified by:
      getDescriptor in class ServiceRegistry
    • clearDescriptorCache

      protected void clearDescriptorCache()
    • currentDescriptor

      protected io.datatree.Tree currentDescriptor()
    • isCheckVersion

      public boolean isCheckVersion()
    • setCheckVersion

      public void setCheckVersion(boolean checkVersion)
    • isAsyncLocalInvocation

      public boolean isAsyncLocalInvocation()
    • setAsyncLocalInvocation

      public void setAsyncLocalInvocation(boolean asyncLocalInvocation)
    • isWriteErrorsToLog

      public boolean isWriteErrorsToLog()
    • setWriteErrorsToLog

      public void setWriteErrorsToLog(boolean writeErrorsToLog)
    • getStreamTimeout

      public long getStreamTimeout()
    • setStreamTimeout

      public void setStreamTimeout(long streamTimeout)
    • isWriteRegistrations

      public boolean isWriteRegistrations()
    • setWriteRegistrations

      public void setWriteRegistrations(boolean writeRegistrations)