Search in sources :

Example 1 with Health

use of org.eclipse.microprofile.health.Health 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)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 ServletException (javax.servlet.ServletException)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