Search in sources :

Example 1 with TimeLimit

use of io.narayana.lra.annotation.TimeLimit in project narayana by jbosstm.

the class NarayanaLRAClient method getTerminationUris.

/**
 * For particular compensator class it returns termination uris based on the provided base uri.
 * You get map of string and url.
 *
 * @param compensatorClass  compensator class to examine
 * @param baseUri  base url used on creation of the termination map.
 * @return map of urls
 */
public static Map<String, String> getTerminationUris(Class<?> compensatorClass, URI baseUri) {
    Map<String, String> paths = new HashMap<>();
    final boolean[] asyncTermination = { false };
    Annotation resourcePathAnnotation = compensatorClass.getAnnotation(Path.class);
    String resourcePath = resourcePathAnnotation == null ? "" : ((Path) resourcePathAnnotation).value().replaceAll("^/+", "");
    final String uriPrefix = String.format("%s:%s%s", baseUri.getScheme(), baseUri.getSchemeSpecificPart(), resourcePath).replaceAll("/$", "");
    Arrays.stream(compensatorClass.getMethods()).forEach(method -> {
        Annotation pathAnnotation = method.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            if (checkMethod(paths, COMPENSATE, (Path) pathAnnotation, method.getAnnotation(Compensate.class), uriPrefix) != 0) {
                TimeLimit timeLimit = method.getAnnotation(TimeLimit.class);
                if (timeLimit != null)
                    paths.put(TIMELIMIT_PARAM_NAME, Long.toString(timeLimit.unit().toMillis(timeLimit.limit())));
                if (isAsyncCompletion(method))
                    asyncTermination[0] = true;
            }
            if (checkMethod(paths, COMPLETE, (Path) pathAnnotation, method.getAnnotation(Complete.class), uriPrefix) != 0) {
                if (isAsyncCompletion(method))
                    asyncTermination[0] = true;
            }
            checkMethod(paths, STATUS, (Path) pathAnnotation, method.getAnnotation(Status.class), uriPrefix);
            checkMethod(paths, FORGET, (Path) pathAnnotation, method.getAnnotation(Forget.class), uriPrefix);
            checkMethod(paths, LEAVE, (Path) pathAnnotation, method.getAnnotation(Leave.class), uriPrefix);
        }
    });
    if (asyncTermination[0] && !paths.containsKey(STATUS) && !paths.containsKey(FORGET)) {
        LRALogger.i18NLogger.error_asyncTerminationBeanMissStatusAndForget(compensatorClass);
        throw new GenericLRAException(Response.Status.BAD_REQUEST.getStatusCode(), "LRA participant class with asynchronous temination but no @Status or @Forget annotations");
    }
    StringBuilder linkHeaderValue = new StringBuilder();
    if (paths.size() != 0) {
        paths.forEach((k, v) -> makeLink(linkHeaderValue, null, k, v));
        paths.put("Link", linkHeaderValue.toString());
    }
    return paths;
}
Also used : Path(javax.ws.rs.Path) Status(io.narayana.lra.annotation.Status) CompensatorStatus(io.narayana.lra.annotation.CompensatorStatus) HashMap(java.util.HashMap) Forget(io.narayana.lra.annotation.Forget) Leave(io.narayana.lra.annotation.Leave) Annotation(java.lang.annotation.Annotation) TimeLimit(io.narayana.lra.annotation.TimeLimit)

Example 2 with TimeLimit

use of io.narayana.lra.annotation.TimeLimit in project narayana by jbosstm.

the class ServerLRAFilter method getTimeOut.

private long getTimeOut(Method method) {
    Annotation timeLimit = method.getDeclaredAnnotation(TimeLimit.class);
    if (timeLimit == null)
        timeLimit = method.getDeclaringClass().getDeclaredAnnotation(TimeLimit.class);
    if (timeLimit == null)
        return NarayanaLRAClient.DEFAULT_TIMEOUT_MILLIS;
    TimeLimit tl = (TimeLimit) timeLimit;
    return tl.unit().toMillis(tl.limit());
}
Also used : Annotation(java.lang.annotation.Annotation) TimeLimit(io.narayana.lra.annotation.TimeLimit)

Example 3 with TimeLimit

use of io.narayana.lra.annotation.TimeLimit in project narayana by jbosstm.

the class ActivityController method extendTimeLimit.

@GET
@Path("/renewTimeLimit")
@Produces(MediaType.APPLICATION_JSON)
@TimeLimit(limit = 100, unit = TimeUnit.MILLISECONDS)
@LRA(value = LRA.Type.REQUIRED)
public Response extendTimeLimit(@HeaderParam(LRA_HTTP_HEADER) String lraId) {
    activityService.add(new Activity(NarayanaLRAClient.getLRAId(lraId)));
    try {
        /*
             * the incomming LRA was created with a timeLimit of 100 ms via the @TimeLimit annotation
             * update the timeLimit to 300
             * sleep for 200
             * return from the method so the LRA will have been running for 200 ms so it should not be cancelled
             */
        lraClient.renewTimeLimit(NarayanaLRAClient.lraToURL(lraId), 300, TimeUnit.MILLISECONDS);
        // sleep for 200000 micro seconds (should be longer than specified in the @TimeLimit annotation)
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return Response.status(Response.Status.OK).entity(Entity.text("Simulate buisiness logic timeoout")).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LRA(io.narayana.lra.annotation.LRA) NestedLRA(io.narayana.lra.annotation.NestedLRA) TimeLimit(io.narayana.lra.annotation.TimeLimit)

Aggregations

TimeLimit (io.narayana.lra.annotation.TimeLimit)3 Annotation (java.lang.annotation.Annotation)2 Path (javax.ws.rs.Path)2 CompensatorStatus (io.narayana.lra.annotation.CompensatorStatus)1 Forget (io.narayana.lra.annotation.Forget)1 LRA (io.narayana.lra.annotation.LRA)1 Leave (io.narayana.lra.annotation.Leave)1 NestedLRA (io.narayana.lra.annotation.NestedLRA)1 Status (io.narayana.lra.annotation.Status)1 Activity (io.narayana.lra.participant.model.Activity)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1