Class ApiGateway

java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.service.Service
services.moleculer.web.ApiGateway
All Implemented Interfaces:
services.moleculer.service.MoleculerLifecycle, RequestProcessor

public class ApiGateway extends services.moleculer.service.Service implements RequestProcessor
  • Field Details

    • routes

      protected Route[] routes
    • lastRoute

      protected Route lastRoute
      Last route (for the last middleware)
    • lastMiddleware

      protected HttpMiddleware lastMiddleware
      Last middleware (custom error pages, HTTP-redirector, etc.)
    • debug

      protected boolean debug
      Print more debug messages.
    • cachedRoutes

      protected int cachedRoutes
      Maximum number of cached routes.
    • staticMappings

      protected LinkedHashMap<String,Mapping> staticMappings
      Static mappings.
    • dynamicMappings

      protected final LinkedList<Mapping> dynamicMappings
      Dynamic mappings.
    • globalMiddlewares

      protected Set<HttpMiddleware> globalMiddlewares
      Global middlewares.
    • checkedNames

      protected Set<String> checkedNames
      Checked services with @HttpAlias annotations.
    • templateEngine

      protected AbstractTemplateEngine templateEngine
      HTML template engine.
    • webSocketRegistry

      protected WebSocketRegistry webSocketRegistry
      WebSocket registry (Netty or J2EE)
    • webSocketFilter

      protected WebSocketFilter webSocketFilter
      WebSocket filter (access control)
    • beforeCall

      protected CallProcessor beforeCall
      Custom message pre-processor.
    • afterCall

      protected CallProcessor afterCall
      Custom message post-processor.
    • executor

      protected ExecutorService executor
      Custom Action Executor (null = use the shared ExecutorService of the MessageBroker).
    • readLock

      protected final ReentrantReadWriteLock.ReadLock readLock
    • writeLock

      protected final ReentrantReadWriteLock.WriteLock writeLock
    • webSocketListener

      public services.moleculer.eventbus.Listener webSocketListener
      Send WebSocket via broadcasted Moleculer Event.
  • Constructor Details

    • ApiGateway

      public ApiGateway()
    • ApiGateway

      public ApiGateway(String... whiteListEntries)
  • Method Details

    • started

      public void started(services.moleculer.ServiceBroker broker) throws Exception
      Initializes gateway instance.
      Specified by:
      started in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      started in class services.moleculer.service.MoleculerComponent
      Parameters:
      broker - parent ServiceBroker
      Throws:
      Exception
    • logRoute

      protected void logRoute(Route route)
    • logAlias

      protected void logAlias(StringBuilder msg, Route route, Alias alias)
    • stopped

      public void stopped()
      Closes gateway.
      Specified by:
      stopped in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      stopped in class services.moleculer.service.MoleculerComponent
    • clearMappings

      protected void clearMappings()
    • service

      public void service(WebRequest req, WebResponse rsp) throws Exception
      Handles request of the HTTP client.
      Specified by:
      service in interface RequestProcessor
      Parameters:
      req - WebRequest object that contains the request the client made of the ApiGateway
      rsp - WebResponse object that contains the response the ApiGateway returns to the client
      Throws:
      Exception - if an input or output error occurs while the ApiGateway is handling the HTTP request
    • use

      public void use(HttpMiddleware... middlewares)
    • use

      public void use(Collection<HttpMiddleware> middlewares)
    • addRoute

      public Route addRoute()
    • addRoute

      public Route addRoute(String path, String serviceList, HttpMiddleware... middlewares)
      Define a route for a list of Service (eg. in the "service" the service's "action" action will available on "http://host:port/path/service/action").
      Parameters:
      path - root path to services (can be null or empty)
      serviceList - list of services and/or actions (eg. "service1, service2.action, service3, service4.action")
      middlewares - optional middlewares (eg. CorsHeaders)
      Returns:
      route the new route
    • addRoute

      public Route addRoute(Route route)
      Adds a route to the list of routes.
      Parameters:
      route - the new route
      Returns:
      route the new route
    • getRoutes

      public Route[] getRoutes()
    • setRoutes

      public void setRoutes(Route[] routes)
    • getCachedRoutes

      public int getCachedRoutes()
    • setCachedRoutes

      public void setCachedRoutes(int cacheSize)
    • getLastMiddleware

      public HttpMiddleware getLastMiddleware()
    • setLastMiddleware

      public void setLastMiddleware(HttpMiddleware lastMiddleware)
    • isDebug

      public boolean isDebug()
    • setDebug

      public void setDebug(boolean debug)
    • getTemplateEngine

      public AbstractTemplateEngine getTemplateEngine()
    • setTemplateEngine

      public void setTemplateEngine(AbstractTemplateEngine templateEngine)
    • getBeforeCall

      public CallProcessor getBeforeCall()
    • setBeforeCall

      public void setBeforeCall(CallProcessor beforeCall)
    • getAfterCall

      public CallProcessor getAfterCall()
    • setAfterCall

      public void setAfterCall(CallProcessor afterCall)
    • getWebSocketRegistry

      public WebSocketRegistry getWebSocketRegistry()
    • setWebSocketRegistry

      public void setWebSocketRegistry(WebSocketRegistry webSocketRegistry)
    • getWebSocketFilter

      public WebSocketFilter getWebSocketFilter()
    • setWebSocketFilter

      public void setWebSocketFilter(WebSocketFilter webSocketFilter)
    • getExecutor

      public ExecutorService getExecutor()
    • setExecutor

      public void setExecutor(ExecutorService executor)
    • getParent

      public RequestProcessor getParent()
      Description copied from interface: RequestProcessor
      Returns the next RequestProcessor in the invocation chain.
      Specified by:
      getParent in interface RequestProcessor
      Returns:
      parent processor