Search in sources :

Example 1 with CompensatorStatus

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

the class ProxyService method getStatus.

CompensatorStatus getStatus(URL lraId, String participantId) throws InvalidLRAStateException {
    ParticipantProxy proxy = getProxy(lraId, participantId);
    if (proxy == null)
        throw new NotFoundException();
    Optional<CompensatorStatus> status = proxy.getStatus();
    // null status implies that the participant is still active
    return status.orElseThrow(InvalidLRAStateException::new);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) CompensatorStatus(io.narayana.lra.annotation.CompensatorStatus)

Example 2 with CompensatorStatus

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

the class NarayanaLRAClient method getStatus.

@Override
public Optional<CompensatorStatus> getStatus(URL lraId) throws GenericLRAException {
    Response response = null;
    try {
        aquireConnection();
        response = getTarget().path(getLRAId(lraId.toString())).request().accept(MediaType.TEXT_PLAIN_TYPE).get();
    } catch (Exception e) {
        releaseConnection(null);
        LRALogger.i18NLogger.error_cannotAccesCorrdinatorWhenGettingStatus(base, lraId, e);
        throw new GenericLRAException(lraId, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Could not access the LRA coordinator: " + e.getMessage(), e);
    }
    try {
        if (response.getStatus() == Response.Status.NO_CONTENT.getStatusCode())
            return Optional.empty();
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            LRALogger.i18NLogger.error_invalidStatusCode(base, response.getStatus(), lraId);
            throw new GenericLRAException(lraId, response.getStatus(), "LRA coordinator returned an invalid status code", null);
        }
        if (!response.hasEntity()) {
            LRALogger.i18NLogger.error_noContentOnGetStatus(base, lraId);
            throw new GenericLRAException(lraId, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "LRA coordinator#getStatus returned 200 OK but no content");
        }
        // convert the returned String into a status
        Optional<CompensatorStatus> status;
        try {
            return fromString(response.readEntity(String.class));
        } catch (IllegalArgumentException e) {
            LRALogger.i18NLogger.error_invalidArgumentOnStatusFromCoordinator(base, lraId, e);
            throw new GenericLRAException(lraId, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "LRA coordinator returned an invalid status", e);
        }
    } finally {
        releaseConnection(response);
    }
}
Also used : Response(javax.ws.rs.core.Response) URISyntaxException(java.net.URISyntaxException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CompensatorStatus(io.narayana.lra.annotation.CompensatorStatus)

Example 3 with CompensatorStatus

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

the class Coordinator method getNestedLRAStatus.

@GET
@Path("{NestedLraId}/status")
public Response getNestedLRAStatus(@PathParam("NestedLraId") String nestedLraId) {
    if (!lraService.hasTransaction(nestedLraId)) {
        // it must have compensated TODO maybe it's better to keep nested LRAs in separate collection
        return Response.ok(CompensatorStatus.Compensated.name()).build();
    }
    Transaction lra = lraService.getTransaction(toURL(nestedLraId));
    CompensatorStatus status = lra.getLRAStatus();
    if (status == null || lra.getLRAStatus() == null) {
        LRALogger.i18NLogger.error_cannotGetStatusOfNestedLra(nestedLraId, lra.getId());
        throw new IllegalLRAStateException(nestedLraId, "The LRA is still active", "getNestedLRAStatus");
    }
    return Response.ok(lra.getLRAStatus().name()).build();
}
Also used : Transaction(io.narayana.lra.coordinator.domain.model.Transaction) IllegalLRAStateException(io.narayana.lra.client.IllegalLRAStateException) CompensatorStatus(io.narayana.lra.annotation.CompensatorStatus) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with CompensatorStatus

use of io.narayana.lra.annotation.CompensatorStatus 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

CompensatorStatus (io.narayana.lra.annotation.CompensatorStatus)4 IllegalLRAStateException (io.narayana.lra.client.IllegalLRAStateException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 LRA (io.narayana.lra.annotation.LRA)1 NestedLRA (io.narayana.lra.annotation.NestedLRA)1 Status (io.narayana.lra.annotation.Status)1 Transaction (io.narayana.lra.coordinator.domain.model.Transaction)1 Activity (io.narayana.lra.participant.model.Activity)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 NotFoundException (javax.ws.rs.NotFoundException)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1