Search in sources :

Example 1 with FaultTolerancePolicy

use of fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy in project Payara by payara.

the class TestUtils method assertAnnotationInvalid.

public static void assertAnnotationInvalid(String expectedErrorMessage) {
    try {
        Method annotatedMethod = getAnnotatedMethod();
        FaultTolerancePolicy policy = FaultTolerancePolicy.asAnnotated(annotatedMethod.getDeclaringClass(), annotatedMethod);
        fail("Annotation should be invalid for " + annotatedMethod + " but got: " + policy);
    } catch (FaultToleranceDefinitionException ex) {
        String message = ex.getMessage();
        int endGeneralPart = message.indexOf(" annotated with ");
        assertTrue(endGeneralPart > 0);
        assertEquals(expectedErrorMessage, message.substring(endGeneralPart + 16));
    }
}
Also used : Method(java.lang.reflect.Method) FaultToleranceDefinitionException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy)

Example 2 with FaultTolerancePolicy

use of fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy in project Payara by payara.

the class FaultToleranceInterceptor method intercept.

public Object intercept(InvocationContext context) throws Exception {
    if (!shouldIntercept(context)) {
        return context.proceed();
    }
    context.getContextData().put(PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED, Boolean.TRUE);
    try {
        initialize();
        AtomicReference<FaultToleranceConfig> lazyConfig = new AtomicReference<>();
        Supplier<FaultToleranceConfig> configSupplier = () -> lazyConfig.updateAndGet(value -> value != null ? value : faultToleranceService.getConfig(context, this));
        FaultTolerancePolicy policy = FaultTolerancePolicy.get(context, configSupplier);
        if (policy.isPresent) {
            return policy.proceed(context, () -> faultToleranceService.getMethodContext(context, policy, getRequestContextController()));
        }
    } catch (FaultToleranceDefinitionException e) {
        logger.log(Level.SEVERE, "Effective FT policy contains illegal values, fault tolerance cannot be applied," + " falling back to plain method invocation.", e);
    // fall-through to normal proceed
    }
    return context.proceed();
}
Also used : FaultToleranceConfig(fish.payara.microprofile.faulttolerance.FaultToleranceConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) FaultToleranceDefinitionException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy)

Example 3 with FaultTolerancePolicy

use of fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy in project Payara by payara.

the class ConfigAlternativeAsynchronousAnnotationsTest method userDefinedAsynchronousAnnotation.

/**
 * Tests with two alternative annotations, one of which is a user defined one {@link OurAsynchronous}.
 */
@Test
public void userDefinedAsynchronousAnnotation() {
    config.override(ALTERNATIVE_ASYNCHRONOUS_ANNNOTATIONS_PROPERTY, javax.ejb.Asynchronous.class.getName() + "," + OurAsynchronous.class.getName());
    FaultTolerancePolicy policy = getPolicy();
    assertNotNull(policy.asynchronous);
    assertFalse("Should be COMPLETION_STAGE", policy.asynchronous.isSuccessWhenCompletedExceptionally());
}
Also used : Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy) Test(org.junit.Test)

Example 4 with FaultTolerancePolicy

use of fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy in project Payara by payara.

the class ConfigAlternativeAsynchronousAnnotationsTest method javaxEjbAsynchronous.

/**
 * Test with a single alternative annotation of type {@link javax.ejb.Asynchronous} set.
 */
@Test
public void javaxEjbAsynchronous() {
    config.override(ALTERNATIVE_ASYNCHRONOUS_ANNNOTATIONS_PROPERTY, javax.ejb.Asynchronous.class.getName());
    FaultTolerancePolicy policy = getPolicy();
    assertNotNull(policy.asynchronous);
    assertTrue("Should be FUTURE", policy.asynchronous.isSuccessWhenCompletedExceptionally());
}
Also used : Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy) Test(org.junit.Test)

Example 5 with FaultTolerancePolicy

use of fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy in project Payara by payara.

the class ConfigAlternativeAsynchronousAnnotationsTest method asynchronousStillRecognisedWhenSettingAlternativeAnnotations.

/**
 * Again two alternative annotations are set but the FT {@link Asynchronous} annotation is used and should still
 * work (even when not in the list configured).
 */
@Test
public void asynchronousStillRecognisedWhenSettingAlternativeAnnotations() {
    config.override(ALTERNATIVE_ASYNCHRONOUS_ANNNOTATIONS_PROPERTY, javax.ejb.Asynchronous.class.getName() + "," + OurAsynchronous.class.getName());
    FaultTolerancePolicy policy = getPolicy();
    assertNotNull(policy.asynchronous);
    assertTrue("Should be FUTURE", policy.asynchronous.isSuccessWhenCompletedExceptionally());
}
Also used : Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) FaultTolerancePolicy(fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy) Test(org.junit.Test)

Aggregations

FaultTolerancePolicy (fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy)5 Asynchronous (org.eclipse.microprofile.faulttolerance.Asynchronous)3 Test (org.junit.Test)3 FaultToleranceDefinitionException (org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)2 FaultToleranceConfig (fish.payara.microprofile.faulttolerance.FaultToleranceConfig)1 Method (java.lang.reflect.Method)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1