Search in sources :

Example 6 with Activity

use of io.narayana.lra.participant.model.Activity 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)

Example 7 with Activity

use of io.narayana.lra.participant.model.Activity in project narayana by jbosstm.

the class ActivityController method multiLevelNestedActivity.

@PUT
@Path("/multiLevelNestedActivity")
@LRA(LRA.Type.MANDATORY)
public Response multiLevelNestedActivity(@HeaderParam(LRA_HTTP_RECOVERY_HEADER) String rcvId, @HeaderParam(LRA_HTTP_HEADER) String nestedLRAId, @QueryParam("nestedCnt") @DefaultValue("1") Integer nestedCnt) {
    assert nestedLRAId != null;
    Activity activity = addWork(nestedLRAId, rcvId);
    if (activity == null)
        return Response.status(Response.Status.EXPECTATION_FAILED).entity("Missing lra data").build();
    // invoke resources that enlist nested LRAs
    String[] lras = new String[nestedCnt + 1];
    lras[0] = nestedLRAId;
    IntStream.range(1, lras.length).forEach(i -> lras[i] = restPutInvocation("nestedActivity", ""));
    return Response.ok(String.join(",", lras)).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) LRA(io.narayana.lra.annotation.LRA) NestedLRA(io.narayana.lra.annotation.NestedLRA) PUT(javax.ws.rs.PUT)

Example 8 with Activity

use of io.narayana.lra.participant.model.Activity in project narayana by jbosstm.

the class ActivityController method compensateWork.

@PUT
@Path("/compensate")
@Produces(MediaType.APPLICATION_JSON)
@Compensate
public Response compensateWork(@HeaderParam(LRA_HTTP_HEADER) String lraId, String userData) throws NotFoundException {
    compensatedCount.incrementAndGet();
    assert lraId != null;
    String txId = NarayanaLRAClient.getLRAId(lraId);
    Activity activity = activityService.getActivity(txId);
    activity.setEndData(userData);
    if (activity.getAndDecrementAcceptCount() > 0) {
        activity.status = CompensatorStatus.Compensating;
        activity.statusUrl = String.format("%s/%s/%s/status", context.getBaseUri(), ACTIVITIES_PATH, txId);
        return Response.accepted().location(URI.create(activity.statusUrl)).build();
    }
    activity.status = CompensatorStatus.Compensated;
    activity.statusUrl = String.format("%s/%s/activity/compensated", context.getBaseUri(), txId);
    System.out.printf("ActivityController compensating %s%n", txId);
    return Response.ok(activity.statusUrl).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Compensate(io.narayana.lra.annotation.Compensate) PUT(javax.ws.rs.PUT)

Example 9 with Activity

use of io.narayana.lra.participant.model.Activity in project narayana by jbosstm.

the class ActivityController method forget.

@PUT
@Path("/{TxId}/forget")
public void forget(@PathParam("TxId") String txId) throws NotFoundException {
    Activity activity = activityService.getActivity(txId);
    activityService.remove(activity.id);
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 10 with Activity

use of io.narayana.lra.participant.model.Activity in project narayana by jbosstm.

the class ActivityController method compensate.

/**
 * Performing a PUT on <participant URL>/compensate will cause the participant to compensate
 * the work that was done within the scope of the transaction.
 *
 * The participant will either return a 200 OK code and a <status URL> which indicates the outcome and which can be probed (via GET)
 * and will simply return the same (implicit) information:
 *
 * <URL>/cannot-compensate
 * <URL>/cannot-complete
 */
@PUT
@Path("/{TxId}/compensate")
@Produces(MediaType.APPLICATION_JSON)
public Response compensate(@PathParam("TxId") String txId) throws NotFoundException {
    Activity activity = activityService.getActivity(txId);
    activity.status = CompensatorStatus.Compensated;
    activity.statusUrl = String.format("%s/%s/activity/compensated", context.getBaseUri(), txId);
    return Response.ok(activity.statusUrl).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Aggregations

Activity (io.narayana.lra.participant.model.Activity)11 Path (javax.ws.rs.Path)10 PUT (javax.ws.rs.PUT)7 Produces (javax.ws.rs.Produces)7 LRA (io.narayana.lra.annotation.LRA)4 NestedLRA (io.narayana.lra.annotation.NestedLRA)4 GET (javax.ws.rs.GET)2 Compensate (io.narayana.lra.annotation.Compensate)1 CompensatorStatus (io.narayana.lra.annotation.CompensatorStatus)1 Complete (io.narayana.lra.annotation.Complete)1 Forget (io.narayana.lra.annotation.Forget)1 Status (io.narayana.lra.annotation.Status)1 TimeLimit (io.narayana.lra.annotation.TimeLimit)1 IllegalLRAStateException (io.narayana.lra.client.IllegalLRAStateException)1 DELETE (javax.ws.rs.DELETE)1 NotFoundException (javax.ws.rs.NotFoundException)1