/
AsyncClient.java
137 lines (119 loc) · 4.5 KB
/
AsyncClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
*******************************************************************************
* Copyright (c) 2017 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.fault.tolerance.tck.asynchronous;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.eclipse.microprofile.fault.tolerance.tck.util.Connection;
import org.eclipse.microprofile.fault.tolerance.tck.util.TCKConfig;
import org.eclipse.microprofile.faulttolerance.Asynchronous;
/**
* A client to demonstrate Asynchronous behaviour
*
* @author <a href="mailto:antoine@sabot-durand.net">Antoine Sabot-Durand</a>
*
*/
@RequestScoped
public class AsyncClient {
/**
* service 1 second in normal operation.
*
* @return the result as a Future
* @throws InterruptedException the interrupted exception
*/
@Asynchronous
public Future<Connection> service() throws InterruptedException {
Connection conn = new Connection() {
{
Thread.sleep(1000);
}
@Override
public String getData() {
return "service DATA";
}
};
return CompletableFuture.completedFuture(conn);
}
/**
* Service an operation until waitCondition is completed or 1 second timeout.
*
* @param waitCondition Execution of this method will delay until the condition is finished
* @param throwException Whether the method should throw an exception (true) or return a stage completed with exception (false)
* @return the result as a CompletionStage. It may be completed with
* InterruptedException if the thread is interrupted
*/
@Asynchronous
public CompletionStage<Connection> serviceCS(Future<?> waitCondition, boolean throwException) {
return serviceCS(waitCondition, false, null);
}
@Asynchronous
public CompletionStage<Connection> serviceCS(Future<?> waitCondition) {
return serviceCS(waitCondition, false);
}
@Asynchronous
public CompletionStage<Connection> serviceCS(Future<?> waitCondition,
CompletionStage<Connection> stageToReturn) {
return serviceCS(waitCondition, false, stageToReturn);
}
private CompletionStage<Connection> serviceCS(Future<?> waitCondition, boolean throwException, CompletionStage<Connection> stageToReturn) {
Throwable exception = null;
try {
waitCondition.get(tckConfig.getTimeoutInMillis(), TimeUnit.SECONDS);
}
catch (ExecutionException e) {
exception = e.getCause();
}
catch (InterruptedException | TimeoutException e) {
exception = e;
}
if (exception != null) {
if (throwException) {
throwAsRuntimeException(exception);
}
else {
return CompletableFutureHelper.failedFuture(exception);
}
}
if (stageToReturn != null) {
return stageToReturn;
}
else {
return CompletableFuture.completedFuture(new Connection() {
@Override
public String getData() {
return "service DATA";
}
});
}
}
private void throwAsRuntimeException(Throwable exception) throws RuntimeException {
if (exception instanceof RuntimeException) {
throw (RuntimeException)exception;
}
else {
throw new RuntimeException(exception);
}
}
}