Skip to content

Commit

Permalink
Merge pull request #242 from barspi/master
Browse files Browse the repository at this point in the history
Q2Info handles /q2/mux/{muxname}/connected returning HTTP 503 if not connected
  • Loading branch information
ar authored Mar 3, 2022
2 parents 576356f + 2e89c78 commit 664c69e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/src/asciidoc/book.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jPOS Extended Edition
:author: jPOS Software SRL
:email: support@jpos.org
:revdate: {localdate}
:revnumber: 2.2.7-SNAPSHOT
:revnumber: 2.2.9-SNAPSHOT
:source-highlighter: rouge
:listing-caption: Listing
:pdf-page-size: A4
Expand Down Expand Up @@ -80,7 +80,7 @@ include::module_client_simulator.adoc[]
include::module_sshd.adoc[]
== Contributed modules
include::module_fsdmsgX.adoc[]
//------------------------------------------------------------
Expand Down
26 changes: 24 additions & 2 deletions modules/qrest/src/main/java/org/jpos/qrest/participant/Q2Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import static org.jpos.qrest.Constants.RESPONSE;

public class Q2Info implements TransactionParticipant, Configurable {
// special temp key in response map to indicate a specific HttpResponseStatus value (should be removed from actual response)
private final static String HTTP_STATUS_KEY = "__http_status__";
private TransactionManager txnmgr;
private Q2 q2;
private List<Route<Map<String,Object>>> routes = new ArrayList<>();
Expand Down Expand Up @@ -73,8 +75,14 @@ public int prepare(long id, Serializable context) {
}
status = HttpResponseStatus.OK;
}
if (response != null && response.containsKey("error")) {
status = HttpResponseStatus.NOT_ACCEPTABLE;

if (response != null) {
if (response.containsKey("error"))
status = HttpResponseStatus.NOT_ACCEPTABLE;
else if (response.containsKey(HTTP_STATUS_KEY)) {
status = (HttpResponseStatus) response.get(HTTP_STATUS_KEY);
response.remove(HTTP_STATUS_KEY);
}
}
ctx.put(RESPONSE,
response != null ? new Response(status, response) :
Expand Down Expand Up @@ -136,6 +144,19 @@ private Map<String,Object> muxInfo (Route r, String path) {
}
}

private Map<String,Object> connected (Route r, String path) {
try {
QMUX qmux = (QMUX)QMUX.getMUX((String) r.parameters(path).get("muxname"));
Map<String,Object> m = muxInfo(qmux);
m.put(HTTP_STATUS_KEY, qmux.isConnected() ?
HttpResponseStatus.OK :
HttpResponseStatus.SERVICE_UNAVAILABLE);
return m;
} catch (NameRegistrar.NotFoundException e) {
return mapOf("error", e.getMessage() + " not found");
}
}

private Map<String, Object> txnmgr () {
List<Object> txnmgr = NameRegistrar.getAsMap().entrySet()
.stream().filter(e -> e.getValue() instanceof TransactionManager)
Expand Down Expand Up @@ -170,6 +191,7 @@ private void initInternalRoutes() {
routes.add(new Route<>(prefix + "/q2/uptime**", "GET", (t,s) -> mapOf("uptime", q2.getUptime())));
routes.add(new Route<>(prefix + "/q2/started**", "GET", (t,s) -> mapOf("started", new Date(System.currentTimeMillis() - q2.getUptime()))));
routes.add(new Route<>(prefix + "/q2/diskspace**", "GET", (t,s) -> diskspace()));
routes.add(new Route<>(prefix + "/q2/mux/{muxname}/connected", "GET", (t,s) -> connected(t,s))); // like below, but returns HTTP code 503 if mux not connected
routes.add(new Route<>(prefix + "/q2/mux/{muxname}**", "GET", (t,s) -> muxInfo(t,s)));
routes.add(new Route<>(prefix + "/q2/mux**", "GET", (t,s) -> muxes()));
routes.add(new Route<>(prefix + "/q2/txnmgr/name", "GET", (t,s) -> mapOf("name", txnmgr.getName())));
Expand Down

0 comments on commit 664c69e

Please # to comment.