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() });
}
}
}
Aggregations