Search in sources :

Example 1 with HealthCheckService

use of fish.payara.microprofile.healthcheck.HealthCheckService in project Payara by payara.

the class HealthCheckServlet method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HealthCheckService healthCheckService = Globals.getDefaultBaseServiceLocator().getService(HealthCheckService.class);
    // If we couldn't find the HealthCheckService, throw an exception
    if (healthCheckService == null) {
        throw new ServletException("Could not find Health Check Service");
    }
    healthCheckService.performHealthChecks(response);
}
Also used : ServletException(javax.servlet.ServletException) HealthCheckService(fish.payara.microprofile.healthcheck.HealthCheckService)

Example 2 with HealthCheckService

use of fish.payara.microprofile.healthcheck.HealthCheckService in project Payara by payara.

the class HealthCheckServletContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
    // Check if this context is the root one ("/")
    if (ctx.getContextPath().isEmpty()) {
        // Check if there is already a servlet for healthcheck
        Map<String, ? extends ServletRegistration> registrations = ctx.getServletRegistrations();
        for (ServletRegistration reg : registrations.values()) {
            if (reg.getClass().equals(HealthCheckServlet.class)) {
                return;
            }
        }
        // Register servlet
        ServletRegistration.Dynamic reg = ctx.addServlet("microprofile-healthcheck-servlet", HealthCheckServlet.class);
        reg.addMapping("/health");
    }
    // Get the BeanManager
    BeanManager beanManager = null;
    try {
        beanManager = CDI.current().getBeanManager();
    } catch (Exception ex) {
        Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.FINE, "Exception getting BeanManager; this probably isn't a CDI application. " + "No HealthChecks will be registered", ex);
    }
    // Check for any Beans annotated with @Health
    if (beanManager != null) {
        HealthCheckService healthCheckService = Globals.getDefaultBaseServiceLocator().getService(HealthCheckService.class);
        InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
        // For each bean annotated with @Health and implementing the HealthCheck interface,
        // register it to the HealthCheckService along with the application name
        Set<Bean<?>> beans = beanManager.getBeans(HealthCheck.class, new AnnotationLiteral<Health>() {
        });
        for (Bean<?> bean : beans) {
            HealthCheck healthCheck = (HealthCheck) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
            healthCheckService.registerHealthCheck(invocationManager.getCurrentInvocation().getAppName(), healthCheck);
            healthCheckService.registerClassLoader(invocationManager.getCurrentInvocation().getAppName(), healthCheck.getClass().getClassLoader());
            Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.INFO, "Registered {0} as a HealthCheck for app: {1}", new Object[] { bean.getBeanClass().getCanonicalName(), invocationManager.getCurrentInvocation().getAppName() });
        }
    }
}
Also used : HealthCheckService(fish.payara.microprofile.healthcheck.HealthCheckService) Health(org.eclipse.microprofile.health.Health) InvocationManager(org.glassfish.api.invocation.InvocationManager) HealthCheck(org.eclipse.microprofile.health.HealthCheck) ServletException(javax.servlet.ServletException) Bean(javax.enterprise.inject.spi.Bean) ServletRegistration(javax.servlet.ServletRegistration) BeanManager(javax.enterprise.inject.spi.BeanManager)

Aggregations

HealthCheckService (fish.payara.microprofile.healthcheck.HealthCheckService)2 ServletException (javax.servlet.ServletException)2 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 ServletRegistration (javax.servlet.ServletRegistration)1 Health (org.eclipse.microprofile.health.Health)1 HealthCheck (org.eclipse.microprofile.health.HealthCheck)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1