2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.fe.impl;
23 import org.openecomp.sdc.common.api.Constants;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
25 import org.openecomp.sdc.fe.config.Configuration;
26 import org.openecomp.sdc.fe.config.ConfigurationManager;
28 import javax.servlet.ServletContext;
29 import javax.ws.rs.core.Response;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.ScheduledExecutorService;
32 import java.util.concurrent.TimeUnit;
35 public class HealthCheckService {
37 private static final Logger healthLogger = Logger.getLogger("asdc.fe.healthcheck");
39 * This executor will execute the health check task.
41 private ScheduledExecutorService healthCheckExecutor =
42 Executors.newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "FE-Health-Check-Thread"));
44 private final HealthCheckScheduledTask task ;
47 public HealthCheckService(ServletContext context) {
48 this.context = context;
49 this.task = new HealthCheckScheduledTask(this);
52 public Configuration getConfig(){
53 return ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
57 void setLastHealthStatus(HealthStatus lastHealthStatus) {
58 this.lastHealthStatus = lastHealthStatus;
61 private HealthStatus lastHealthStatus = new HealthStatus(500, "{}");
62 private ServletContext context;
64 public void start(int interval) {
65 this.healthCheckExecutor.scheduleAtFixedRate( getTask() , 0, interval, TimeUnit.SECONDS);
69 * To be used by the HealthCheckServlet
73 public Response getFeHealth() {
74 return this.buildResponse(lastHealthStatus);
77 private Response buildResponse(HealthStatus healthStatus) {
78 healthLogger.trace("FE and BE health check status: {}", healthStatus.getBody());
79 return Response.status(healthStatus.getStatusCode()).entity(healthStatus.getBody()).build();
82 public HealthStatus getLastHealthStatus() {
83 return lastHealthStatus;
85 public HealthCheckScheduledTask getTask() {
90 static class HealthStatus {
92 public void setBody(String body) {
96 public void setStatusCode(int statusCode) {
97 this.statusCode = statusCode;
101 private int statusCode;
103 public HealthStatus(int code, String body) {
105 this.statusCode = code;
108 public int getStatusCode() {
112 public String getBody() {