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

org.pipservices3.rpc.services.StatusRestService Maven / Gradle / Ivy

The newest version!
package org.pipservices3.rpc.services;

import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.process.Inflector;
import org.pipservices3.commons.config.ConfigParams;
import org.pipservices3.commons.convert.StringConverter;
import org.pipservices3.commons.data.StringValueMap;
import org.pipservices3.commons.errors.ConfigException;
import org.pipservices3.commons.refer.Descriptor;
import org.pipservices3.commons.refer.IReferences;
import org.pipservices3.commons.refer.ReferenceException;
import org.pipservices3.commons.run.Parameters;
import org.pipservices3.components.info.ContextInfo;

import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Service that returns microservice status information via HTTP/REST protocol.
 * 

* The service responds on /status route (can be changed) with a JSON object: *

* { *

    *
  • "id": unique container id (usually hostname) *
  • "name": container name (from ContextInfo) *
  • "description": container description (from ContextInfo) *
  • "start_time": time when container was started *
  • "current_time": current time in UTC *
  • "uptime": duration since container start time in milliseconds *
  • "properties": additional container properties (from ContextInfo) *
  • "components": descriptors of components registered in the container *
*

* } *

* ### Configuration parameters ### *

    *
  • base_route: base route for remote URI *
  • route: status route (default: "status") *
  • dependencies: *
      *
    • endpoint: override for HTTP Endpoint dependency *
    • controller: override for Controller dependency *
    *
  • connection(s): *
      *
    • discovery_key: (optional) a key to retrieve the connection from IDiscovery *
    • protocol: connection protocol: http or https *
    • host: host name or IP address *
    • port: port number *
    • uri: resource URI or connection string with all parameters in it *
    *
*

* ### References ### *

    *
  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages *
  • *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements *
  • *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection *
  • *:endpoint:http:*:1.0 (optional) {@link HttpEndpoint} reference *
*

* ### Example ### *

 * {@code
 * StatusService service = new StatusService();
 * service.configure(ConfigParams.fromTuples(
 *     "connection.protocol", "http",
 *     "connection.host", "localhost",
 *     "connection.port", 8080
 * ));
 *
 * service.open("123");
 * System.out.println("The Status service is accessible at http://+:8080/status");
 * }
 * 
* * @see RestService */ public class StatusRestService extends RestService { private final ZonedDateTime _startTime = ZonedDateTime.now(); private IReferences _references; private ContextInfo _contextInfo; private String _route = "status"; /** * Creates a new instance of this service. */ public StatusRestService() { _dependencyResolver.put("context-info", new Descriptor("pip-services", "context-info", "default", "*", "1.0")); } /** * Configures component by passing configuration parameters. * * @param config configuration parameters to be set. * @throws ConfigException when configuration is wrong. */ @Override public void configure(ConfigParams config) throws ConfigException { super.configure(config); _route = config.getAsStringWithDefault("route", _route); } /** * Sets references to dependent components. * * @param references references to locate the component dependencies. * @throws ReferenceException when no found references. * @throws ConfigException when configuration is wrong. */ @Override public void setReferences(IReferences references) throws ReferenceException, ConfigException { _references = references; super.setReferences(references); _contextInfo = (ContextInfo) _dependencyResolver.getOneOptional("context-info"); } /** * Registers all service routes in HTTP endpoint. */ @Override public void register() { registerRoute(HttpMethod.GET, _route, new Inflector() { @Override public Response apply(ContainerRequestContext request) { return status(request); } }); } /** * Handles status requests * * @param request an HTTP request * @return res an HTTP response */ private Response status(ContainerRequestContext request) { String id = _contextInfo != null ? _contextInfo.getContextId() : ""; String name = _contextInfo != null ? _contextInfo.getName() : "Unknown"; String description = _contextInfo != null ? _contextInfo.getDescription() : ""; long uptime = Duration.between(_startTime, ZonedDateTime.now()).toMillis(); StringValueMap properties = _contextInfo != null ? _contextInfo.getProperties() : null; List components = new ArrayList<>(); if (_references != null) { for (Object locator : _references.getAllLocators()) components.add(locator.toString()); } Parameters status = Parameters.fromTuples("id", id, "name", name, "description", description, "start_time", StringConverter.toString(_startTime), "current_time", StringConverter.toString(ZonedDateTime.now()), "uptime", uptime, "properties", properties, "components", components); return sendResult(status); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy