Skip to content

Commit 5f53128

Browse files
authored
Added health check option against subscriptions (#6)
1 parent eeeff91 commit 5f53128

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

java-client/src/main/java/energy/trolie/client/RequestSubscription.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public interface RequestSubscription {
1717
* @return true if the subscription is running and has not been stopped
1818
*/
1919
boolean isSubscribed();
20+
21+
/**
22+
*
23+
* @return true if the subscription is currently healthy, meaning that the last poll
24+
* occurred successfully without error.
25+
*/
26+
boolean isHealthy();
2027
}

java-client/src/main/java/energy/trolie/client/impl/request/AbstractStreamingGet.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ public abstract class AbstractStreamingGet<T extends StreamingResponseReceiver>
4545
int bufferSize;
4646
ThreadPoolExecutor threadPoolExecutor;
4747
Map<String, String> httpHeaders;
48+
protected boolean lastRequestFailed = false;
4849

4950
protected JsonFactory jsonFactory;
5051
protected T receiver;
51-
52+
53+
protected boolean didLastRequestFail() {
54+
return lastRequestFailed;
55+
}
56+
5257
protected abstract String getPath();
5358
protected abstract String getContentType();
5459

@@ -100,9 +105,11 @@ protected boolean handleResponse(ClassicHttpResponse response) {
100105
try {
101106
return threadPoolExecutor.submit(new HandlerExecutor(response.getEntity().getContent())).get();
102107
} catch (IOException e) {
108+
lastRequestFailed = true;
103109
logger.error("I/O error initiating request",e);
104110
receiver.error(new StreamingGetConnectionException(e));
105111
} catch (Exception e) {
112+
lastRequestFailed = true;
106113
logger.error("Internal error handling response",e);
107114
receiver.error(new SubscriberInternalException(e));
108115
}
@@ -111,6 +118,7 @@ protected boolean handleResponse(ClassicHttpResponse response) {
111118
return true;
112119
} else {
113120
String s = "Server responded with status code " + response.getCode();
121+
lastRequestFailed = true;
114122
logger.error(s);
115123
receiver.error(new StreamingGetResponseException(s, response.getCode()));
116124
}
@@ -131,14 +139,16 @@ protected HttpGet createRequest() throws URISyntaxException {
131139

132140
public void executeRequest() {
133141
try {
134-
142+
lastRequestFailed = false;
135143
HttpGet get = createRequest();
136144
httpClient.execute(host.getHost(), get, createResponseHandler());
137145

138146
} catch (IOException e) {
147+
lastRequestFailed = true;
139148
logger.error("I/O error initiating request",e);
140149
receiver.error(new StreamingGetConnectionException(e));
141150
} catch (Exception e) {
151+
lastRequestFailed = true;
142152
receiver.error(new SubscriberInternalException(e));
143153
}
144154
}

java-client/src/main/java/energy/trolie/client/impl/request/AbstractStreamingSubscribedGet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public boolean isActive() {
8383
return active.get();
8484
}
8585

86+
@Override
87+
public boolean isHealthy() {
88+
return !didLastRequestFail();
89+
}
90+
8691
@Override
8792
protected boolean handleResponse(ClassicHttpResponse response) {
8893
boolean success = super.handleResponse(response);
@@ -91,6 +96,7 @@ protected boolean handleResponse(ClassicHttpResponse response) {
9196
try {
9297
eTagStore.putETag(getPath(), response.getHeader(HttpHeaders.ETAG).getValue());
9398
} catch (ProtocolException e) {
99+
lastRequestFailed = true;
94100
logger.error("Error handling server response",e);
95101
receiver.error(new StreamingGetHandlingException(e));
96102
return false;

java-client/src/test/java/energy/trolie/client/TrolieClientIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878

7979
import static org.junit.jupiter.api.Assertions.assertEquals;
8080
import static org.junit.jupiter.api.Assertions.assertNotNull;
81+
import static org.junit.jupiter.api.Assertions.assertTrue;
8182

8283
@Slf4j
8384
@SuppressWarnings("unchecked")
@@ -543,6 +544,7 @@ public void setSubscription(RequestSubscription subscription) {
543544

544545
while (subscription.isSubscribed()) {
545546
Thread.sleep(100);
547+
assertTrue(subscription.isHealthy());
546548
}
547549

548550
//we should have received 2 snapshots, 1 304 code and 1 500 code

0 commit comments

Comments
 (0)