-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue35 Add non-JAX-RS CompletionStage TCK
Signed-off-by: xstefank <[email protected]>
- Loading branch information
Showing
2 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...c/main/java/org/eclipse/microprofile/lra/tck/participant/nonjaxrs/ValidCSParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.lra.tck.participant.nonjaxrs; | ||
|
||
import org.eclipse.microprofile.lra.annotation.Compensate; | ||
import org.eclipse.microprofile.lra.annotation.ParticipantStatus; | ||
import org.eclipse.microprofile.lra.annotation.ws.rs.LRA; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.net.URI; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@ApplicationScoped | ||
@Path(ValidCSParticipant.ROOT_PATH) | ||
public class ValidCSParticipant { | ||
|
||
public static final String ROOT_PATH = "valid-cs-participant"; | ||
public static final String ENLIST_WITH_COMPENSATE = "enlist"; | ||
|
||
private final AtomicInteger compensatedCount = new AtomicInteger(0); | ||
|
||
@GET | ||
@Path(ENLIST_WITH_COMPENSATE) | ||
@LRA(value = LRA.Type.REQUIRED, cancelOn = Response.Status.INTERNAL_SERVER_ERROR) | ||
public Response enlist(@HeaderParam(LRA.LRA_HTTP_CONTEXT_HEADER) String lraId) { | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
|
||
@Compensate | ||
public CompletionStage<ParticipantStatus> compensate(URI lraId) { | ||
assert lraId != null; | ||
|
||
compensatedCount.incrementAndGet(); | ||
|
||
return CompletableFuture.runAsync(() -> { | ||
simulateLongRunningCompensation(); | ||
|
||
|
||
}).handle((s, e) -> e != null ? ParticipantStatus.Compensated : ParticipantStatus.FailedToCompensate); | ||
} | ||
|
||
private void simulateLongRunningCompensation() { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path(ValidLRAParticipant.COMPENSATED_COUNT_PATH) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response compensated() { | ||
return Response.ok(compensatedCount.toString()).build(); | ||
} | ||
|
||
|
||
|
||
} |