use of org.glassfish.api.invocation.InvocationManager in project Payara by payara.
the class TimeoutInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
Object proceededInvocationContext = null;
FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
Config config = null;
try {
config = ConfigProvider.getConfig();
} catch (IllegalArgumentException ex) {
logger.log(Level.INFO, "No config could be found", ex);
}
try {
if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
logger.log(Level.FINER, "Proceeding invocation with timeout semantics");
proceededInvocationContext = timeout(invocationContext);
} else {
// If fault tolerance isn't enabled, just proceed as normal
logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without timeout.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
proceededInvocationContext = invocationContext.proceed();
}
} catch (Exception ex) {
Retry retry = FaultToleranceCdiUtils.getAnnotation(beanManager, Retry.class, invocationContext);
if (retry != null) {
logger.log(Level.FINE, "Retry annotation found on method, propagating error upwards.");
throw ex;
} else {
Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
if (fallback != null) {
logger.log(Level.FINE, "Fallback annotation found on method, and no Retry annotation - " + "falling back from Timeout");
FallbackPolicy fallbackPolicy = new FallbackPolicy(fallback, config, invocationContext);
proceededInvocationContext = fallbackPolicy.fallback(invocationContext);
} else {
throw ex;
}
}
}
return proceededInvocationContext;
}
use of org.glassfish.api.invocation.InvocationManager in project Payara by payara.
the class FallbackPolicy method fallback.
/**
* Performs the fallback operation defined by the @Fallback annotation.
* @param invocationContext The failing invocation context
* @return The result of the executed fallback method
* @throws Exception If the fallback method itself fails.
*/
public Object fallback(InvocationContext invocationContext) throws Exception {
Object fallbackInvocationContext = null;
FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
faultToleranceService.startFaultToleranceSpan(new RequestTraceSpan("executeFallbackMethod"), invocationManager, invocationContext);
try {
if (fallbackMethod != null && !fallbackMethod.isEmpty()) {
logger.log(Level.FINE, "Using fallback method: {0}", fallbackMethod);
fallbackInvocationContext = invocationContext.getMethod().getDeclaringClass().getDeclaredMethod(fallbackMethod, invocationContext.getMethod().getParameterTypes()).invoke(invocationContext.getTarget(), invocationContext.getParameters());
} else {
logger.log(Level.FINE, "Using fallback class: {0}", fallbackClass.getName());
ExecutionContext executionContext = new FaultToleranceExecutionContext(invocationContext.getMethod(), invocationContext.getParameters());
fallbackInvocationContext = fallbackClass.getDeclaredMethod(FALLBACK_HANDLER_METHOD_NAME, ExecutionContext.class).invoke(CDI.current().select(fallbackClass).get(), executionContext);
}
} finally {
faultToleranceService.endFaultToleranceSpan();
}
return fallbackInvocationContext;
}
use of org.glassfish.api.invocation.InvocationManager 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() });
}
}
}
use of org.glassfish.api.invocation.InvocationManager in project Payara by payara.
the class MetricsService method getApplicationName.
/**
* Gets the application name from the invocation manager.
*
* @return The application name
*/
public String getApplicationName() {
InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
if (invocationManager.getCurrentInvocation() == null) {
return invocationManager.peekAppEnvironment().getName();
}
String appName = invocationManager.getCurrentInvocation().getAppName();
if (appName == null) {
appName = invocationManager.getCurrentInvocation().getModuleName();
}
if (appName == null) {
appName = invocationManager.getCurrentInvocation().getComponentId();
}
return appName;
}
use of org.glassfish.api.invocation.InvocationManager in project Payara by payara.
the class AppTest method createUtx.
private UserTransaction createUtx() throws javax.naming.NamingException {
UserTransaction utx = new UserTransactionImpl();
InvocationManager im = new org.glassfish.api.invocation.InvocationManagerImpl();
((UserTransactionImpl) utx).setForTesting((JavaEETransactionManager) t, im);
return utx;
}
Aggregations