Search in sources :

Example 1 with Activity

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

the class ActivityController method acceptWork.

@PUT
@Path(ACCEPT_WORK)
@LRA(LRA.Type.REQUIRED)
public Response acceptWork(@HeaderParam(LRA_HTTP_RECOVERY_HEADER) String rcvId, @HeaderParam(LRA_HTTP_HEADER) String lraId) {
    assert lraId != null;
    Activity activity = addWork(lraId, rcvId);
    if (activity == null)
        return Response.status(Response.Status.EXPECTATION_FAILED).entity("Missing lra data").build();
    // tests that it is possible to asynchronously complete
    activity.setAcceptedCount(1);
    return Response.ok(lraId).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 2 with Activity

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

the class ActivityController method forgetWork.

@DELETE
@Path("/forget")
@Produces(MediaType.APPLICATION_JSON)
@Forget
public Response forgetWork(@HeaderParam(LRA_HTTP_HEADER) String lraId) {
    // throws NotFoundException {
    completedCount.incrementAndGet();
    assert lraId != null;
    String txId = NarayanaLRAClient.getLRAId(lraId);
    Activity activity = activityService.getActivity(txId);
    activityService.remove(activity.id);
    activity.status = CompensatorStatus.Completed;
    activity.statusUrl = String.format("%s/%s/activity/completed", context.getBaseUri(), txId);
    System.out.printf("ActivityController forgetting %s%n", txId);
    return Response.ok(activity.statusUrl).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Forget(io.narayana.lra.annotation.Forget)

Example 3 with Activity

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

the class ActivityController method completeWork.

@PUT
@Path("/complete")
@Produces(MediaType.APPLICATION_JSON)
@Complete
public Response completeWork(@HeaderParam(LRA_HTTP_HEADER) String lraId, String userData) throws NotFoundException {
    completedCount.incrementAndGet();
    assert lraId != null;
    String txId = NarayanaLRAClient.getLRAId(lraId);
    Activity activity = activityService.getActivity(txId);
    activity.setEndData(userData);
    if (activity.getAndDecrementAcceptCount() > 0) {
        activity.status = CompensatorStatus.Completing;
        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.Completed;
    activity.statusUrl = String.format("%s/%s/activity/completed", context.getBaseUri(), txId);
    System.out.printf("ActivityController completing %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) Complete(io.narayana.lra.annotation.Complete) PUT(javax.ws.rs.PUT)

Example 4 with Activity

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

the class ActivityController method addWork.

private Activity addWork(String lraId, String rcvId) {
    assert lraId != null;
    String txId = NarayanaLRAClient.getLRAId(lraId);
    System.out.printf("ActivityController: work id %s and rcvId %s %n", txId, rcvId);
    if (txId == null)
        return null;
    try {
        return activityService.getActivity(txId);
    } catch (NotFoundException e) {
        Activity activity = new Activity(txId);
        activity.rcvUrl = rcvId;
        activity.status = null;
        activityService.add(activity);
        return activity;
    }
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Activity(io.narayana.lra.participant.model.Activity)

Example 5 with Activity

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

the class ActivityController method status.

/**
 *     Performing a GET on the participant URL will return the current status of the participant {@link CompensatorStatus}, or 404 if the participant is no longer present.
 */
@GET
@Path("/status")
@Produces(MediaType.APPLICATION_JSON)
@Status
@LRA(LRA.Type.NOT_SUPPORTED)
public Response status(@PathParam("LraUrl") String lraUrl, @HeaderParam(LRA_HTTP_HEADER) String lraId) throws NotFoundException {
    String txId = NarayanaLRAClient.getLRAId(lraId);
    Activity activity = activityService.getActivity(txId);
    if (activity.status == null)
        throw new IllegalLRAStateException(lraId, "LRA is not active", "getStatus");
    if (activity.getAndDecrementAcceptCount() <= 0) {
        if (activity.status == CompensatorStatus.Completing)
            activity.status = CompensatorStatus.Completed;
        else if (activity.status == CompensatorStatus.Compensating)
            activity.status = CompensatorStatus.Compensated;
    }
    return Response.ok(activity.status.name()).build();
}
Also used : Activity(io.narayana.lra.participant.model.Activity) IllegalLRAStateException(io.narayana.lra.client.IllegalLRAStateException) Path(javax.ws.rs.Path) Status(io.narayana.lra.annotation.Status) CompensatorStatus(io.narayana.lra.annotation.CompensatorStatus) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LRA(io.narayana.lra.annotation.LRA) NestedLRA(io.narayana.lra.annotation.NestedLRA)

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