Search in sources :

Example 31 with InvocationManager

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;
}
Also used : FallbackPolicy(fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) NamingException(javax.naming.NamingException) TimeoutException(org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 32 with InvocationManager

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;
}
Also used : ExecutionContext(org.eclipse.microprofile.faulttolerance.ExecutionContext) InvocationManager(org.glassfish.api.invocation.InvocationManager) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService)

Example 33 with InvocationManager

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

Example 34 with InvocationManager

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;
}
Also used : InvocationManager(org.glassfish.api.invocation.InvocationManager)

Example 35 with InvocationManager

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;
}
Also used : InvocationManager(org.glassfish.api.invocation.InvocationManager)

Aggregations

InvocationManager (org.glassfish.api.invocation.InvocationManager)40 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)13 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)9 NamingException (javax.naming.NamingException)9 Config (org.eclipse.microprofile.config.Config)8 InvocationException (org.glassfish.api.invocation.InvocationException)7 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 AroundInvoke (javax.interceptor.AroundInvoke)5 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)5 Retry (org.eclipse.microprofile.faulttolerance.Retry)5 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)5 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)4 ArrayList (java.util.ArrayList)4 NoSuchElementException (java.util.NoSuchElementException)4 EJBInvocation (org.glassfish.ejb.api.EJBInvocation)4 JavaEETransactionManager (com.sun.enterprise.transaction.api.JavaEETransactionManager)3 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)3 Method (java.lang.reflect.Method)3 Context (javax.naming.Context)3 InitialContext (javax.naming.InitialContext)3