Search in sources :

Example 1 with ManagedExecutorService

use of javax.enterprise.concurrent.ManagedExecutorService in project Payara by payara.

the class AsynchronousInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    // Get the configured ManagedExecutorService from the Fault Tolerance Service
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    ManagedExecutorService managedExecutorService = faultToleranceService.getManagedExecutorService();
    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 {
        // Attempt to proceed the InvocationContext with Asynchronous semantics if Fault Tolerance is enabled
        if (faultToleranceService.isFaultToleranceEnabled(faultToleranceService.getApplicationName(invocationManager, invocationContext), config)) {
            Callable callable = () -> {
                return invocationContext.proceed();
            };
            logger.log(Level.FINER, "Proceeding invocation asynchronously");
            proceededInvocationContext = new FutureDelegator(managedExecutorService.submit(callable));
        } else {
            // If fault tolerance isn't enabled, just proceed as normal
            logger.log(Level.FINE, "Fault Tolerance not enabled for {0}, proceeding normally without asynchronous.", faultToleranceService.getApplicationName(invocationManager, invocationContext));
            proceededInvocationContext = invocationContext.proceed();
        }
    } catch (Exception ex) {
        // If an exception was thrown, check if the method is annotated with @Fallback
        // We should only get here if executing synchronously, as the exception wouldn't get thrown in this thread
        Fallback fallback = FaultToleranceCdiUtils.getAnnotation(beanManager, Fallback.class, invocationContext);
        // If the method was annotated with Fallback, attempt it, otherwise just propagate the exception upwards
        if (fallback != null) {
            logger.log(Level.FINE, "Fallback annotation found on method - falling back from Asynchronous");
            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) ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) Config(org.eclipse.microprofile.config.Config) InvocationManager(org.glassfish.api.invocation.InvocationManager) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) Callable(java.util.concurrent.Callable) FaultToleranceException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with ManagedExecutorService

use of javax.enterprise.concurrent.ManagedExecutorService in project wildfly by wildfly.

the class HungTasksTerminationTestCase method testManagedScheduledExecutorServiceHungTasksCancellationOperation.

/**
 * Tests the 'on demand' hung task termination, through a management operation, works on a ManagedScheduledExecutorService.
 * @throws Exception
 */
@Test
public void testManagedScheduledExecutorServiceHungTasksCancellationOperation() throws Exception {
    final PathAddress pathAddress = EE_SUBSYSTEM_PATH_ADDRESS.append(EESubsystemModel.MANAGED_SCHEDULED_EXECUTOR_SERVICE, RESOURCE_NAME);
    // add
    final ModelNode addOperation = Util.createAddOperation(pathAddress);
    final String jndiName = "java:jboss/ee/concurrency/scheduledexecutor/" + RESOURCE_NAME;
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.JNDI_NAME).set(jndiName);
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.HUNG_TASK_THRESHOLD).set(HUNG_TASK_THRESHOLD_TEST_VALUE);
    final ModelNode addResult = managementClient.getControllerClient().execute(addOperation);
    Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
    try {
        // lookup the executor
        final ManagedExecutorService executorService = InitialContext.doLookup(jndiName);
        Assert.assertNotNull(executorService);
        testHungTasksCancellationOperation(pathAddress, executorService);
    } finally {
        // remove
        final ModelNode removeOperation = Util.createRemoveOperation(pathAddress);
        removeOperation.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
        final ModelNode removeResult = managementClient.getControllerClient().execute(removeOperation);
        Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION).isDefined());
    }
}
Also used : ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) PathAddress(org.jboss.as.controller.PathAddress) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 3 with ManagedExecutorService

use of javax.enterprise.concurrent.ManagedExecutorService in project wildfly by wildfly.

the class HungTasksTerminationTestCase method testManagedScheduledExecutorServiceHungTasksCancellationPeriodic.

/**
 * Tests the 'periodic' hung task termination works on a ManagedScheduledExecutorService.
 * @throws Exception
 */
@Test
public void testManagedScheduledExecutorServiceHungTasksCancellationPeriodic() throws Exception {
    final PathAddress pathAddress = EE_SUBSYSTEM_PATH_ADDRESS.append(EESubsystemModel.MANAGED_SCHEDULED_EXECUTOR_SERVICE, RESOURCE_NAME);
    // add
    final ModelNode addOperation = Util.createAddOperation(pathAddress);
    final String jndiName = "java:jboss/ee/concurrency/scheduledexecutor/" + RESOURCE_NAME;
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.JNDI_NAME).set(jndiName);
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.HUNG_TASK_THRESHOLD).set(HUNG_TASK_THRESHOLD_TEST_VALUE);
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.HUNG_TASK_TERMINATION_PERIOD).set(HUNG_TASK_CANCELLATION_PERIOD_TEST_VALUE);
    final ModelNode addResult = managementClient.getControllerClient().execute(addOperation);
    Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
    try {
        // lookup the executor
        final ManagedExecutorService executorService = InitialContext.doLookup(jndiName);
        Assert.assertNotNull(executorService);
        testHungTasksCancellationPeriodic(pathAddress, executorService);
    } finally {
        // remove
        final ModelNode removeOperation = Util.createRemoveOperation(pathAddress);
        removeOperation.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
        final ModelNode removeResult = managementClient.getControllerClient().execute(removeOperation);
        Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION).isDefined());
    }
}
Also used : ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) PathAddress(org.jboss.as.controller.PathAddress) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 4 with ManagedExecutorService

use of javax.enterprise.concurrent.ManagedExecutorService in project wildfly by wildfly.

the class HungTasksTerminationTestCase method testManagedExecutorServiceHungTasksCancellationOperation.

/**
 * Tests the 'on demand' hung task termination, through a management operation, works on a ManagedExecutorService.
 * @throws Exception
 */
@Test
public void testManagedExecutorServiceHungTasksCancellationOperation() throws Exception {
    final PathAddress pathAddress = EE_SUBSYSTEM_PATH_ADDRESS.append(EESubsystemModel.MANAGED_EXECUTOR_SERVICE, RESOURCE_NAME);
    // add
    final ModelNode addOperation = Util.createAddOperation(pathAddress);
    final String jndiName = "java:jboss/ee/concurrency/executor/" + RESOURCE_NAME;
    addOperation.get(ManagedExecutorServiceResourceDefinition.JNDI_NAME).set(jndiName);
    addOperation.get(ManagedExecutorServiceResourceDefinition.HUNG_TASK_THRESHOLD).set(HUNG_TASK_THRESHOLD_TEST_VALUE);
    final ModelNode addResult = managementClient.getControllerClient().execute(addOperation);
    Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
    try {
        final ManagedExecutorService executorService = InitialContext.doLookup(jndiName);
        Assert.assertNotNull(executorService);
        testHungTasksCancellationOperation(pathAddress, executorService);
    } finally {
        // remove
        final ModelNode removeOperation = Util.createRemoveOperation(pathAddress);
        removeOperation.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
        final ModelNode removeResult = managementClient.getControllerClient().execute(removeOperation);
        Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION).isDefined());
    }
}
Also used : ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) PathAddress(org.jboss.as.controller.PathAddress) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 5 with ManagedExecutorService

use of javax.enterprise.concurrent.ManagedExecutorService in project wildfly by wildfly.

the class HungTasksTerminationTestCase method testManagedExecutorServiceHungTasksCancellationPeriodic.

/**
 * Tests the 'periodic' hung task termination works on a ManagedExecutorService.
 * @throws Exception
 */
@Test
public void testManagedExecutorServiceHungTasksCancellationPeriodic() throws Exception {
    final PathAddress pathAddress = EE_SUBSYSTEM_PATH_ADDRESS.append(EESubsystemModel.MANAGED_EXECUTOR_SERVICE, RESOURCE_NAME);
    // add
    final ModelNode addOperation = Util.createAddOperation(pathAddress);
    final String jndiName = "java:jboss/ee/concurrency/executor/" + RESOURCE_NAME;
    addOperation.get(ManagedExecutorServiceResourceDefinition.JNDI_NAME).set(jndiName);
    addOperation.get(ManagedExecutorServiceResourceDefinition.HUNG_TASK_THRESHOLD).set(HUNG_TASK_THRESHOLD_TEST_VALUE);
    addOperation.get(ManagedScheduledExecutorServiceResourceDefinition.HUNG_TASK_TERMINATION_PERIOD).set(HUNG_TASK_CANCELLATION_PERIOD_TEST_VALUE);
    final ModelNode addResult = managementClient.getControllerClient().execute(addOperation);
    Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
    try {
        final ManagedExecutorService executorService = InitialContext.doLookup(jndiName);
        Assert.assertNotNull(executorService);
        testHungTasksCancellationPeriodic(pathAddress, executorService);
    } finally {
        // remove
        final ModelNode removeOperation = Util.createRemoveOperation(pathAddress);
        removeOperation.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
        final ModelNode removeResult = managementClient.getControllerClient().execute(removeOperation);
        Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION).isDefined());
    }
}
Also used : ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) PathAddress(org.jboss.as.controller.PathAddress) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Aggregations

ManagedExecutorService (javax.enterprise.concurrent.ManagedExecutorService)10 PathAddress (org.jboss.as.controller.PathAddress)8 ModelNode (org.jboss.dmr.ModelNode)8 Test (org.junit.Test)8 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)1 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AroundInvoke (javax.interceptor.AroundInvoke)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 Config (org.eclipse.microprofile.config.Config)1 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)1 FaultToleranceException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1