From 99b221c3a6887805d6c1986e48125cf94545f159 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Wed, 26 May 2021 23:54:48 -0600 Subject: [PATCH] Use monotonic clocks for QMUX. --- jpos/src/main/java/org/jpos/q2/iso/QMUX.java | 20 ++++++++++--------- .../java/org/jpos/q2/iso/QMUXTestCase.java | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/jpos/src/main/java/org/jpos/q2/iso/QMUX.java b/jpos/src/main/java/org/jpos/q2/iso/QMUX.java index fcb6651281..f49b07bf52 100644 --- a/jpos/src/main/java/org/jpos/q2/iso/QMUX.java +++ b/jpos/src/main/java/org/jpos/q2/iso/QMUX.java @@ -30,6 +30,8 @@ import java.io.IOException; import java.io.PrintStream; +import java.time.Duration; +import java.time.Instant; import java.util.*; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -58,7 +60,7 @@ public class QMUX List listeners; private volatile int rx, tx, rxExpired, txExpired, rxPending, rxUnhandled, rxForwarded; - private volatile long lastTxn = 0L; + private volatile Duration lastTxn = Duration.ZERO; private boolean listenerRegistered; public QMUX () { super (); @@ -161,7 +163,7 @@ public ISOMsg request (ISOMsg m, long timeout) throws ISOException { if (resp != null) { rx++; - lastTxn = System.currentTimeMillis(); + lastTxn = Duration.ofNanos(System.nanoTime()); }else { rxExpired++; if (m.getDirection() != ISOMsg.OUTGOING) @@ -351,7 +353,7 @@ public boolean removeISORequestListener(ISORequestListener l) { } public synchronized void resetCounters() { rx = tx = rxExpired = txExpired = rxPending = rxUnhandled = rxForwarded = 0; - lastTxn = 0l; + lastTxn = Duration.ZERO; } public String getCountersAsString () { StringBuffer sb = new StringBuffer(); @@ -366,10 +368,10 @@ public String getCountersAsString () { sb.append (", connected="); sb.append (Boolean.toString(isConnected())); sb.append (", last="); - sb.append (lastTxn); - if (lastTxn > 0) { + sb.append (getLastTxnTimestampInMillis()); + if (lastTxn != Duration.ZERO) { sb.append (", idle="); - sb.append(System.currentTimeMillis() - lastTxn); + sb.append(Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis()); sb.append ("ms"); } return sb.toString(); @@ -413,10 +415,10 @@ public int getRXForwarded() { } public long getLastTxnTimestampInMillis() { - return lastTxn; + return Instant.now().minus(Duration.ofNanos(System.nanoTime()).minus(lastTxn)).toEpochMilli(); } public long getIdleTimeInMillis() { - return lastTxn > 0L ? System.currentTimeMillis() - lastTxn : -1L; + return lastTxn != Duration.ZERO ? Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis() : -1L; } protected void processUnhandled (ISOMsg m) { @@ -529,7 +531,7 @@ public void responseReceived (ISOMsg response) { synchronized (QMUX.this) { rx++; rxPending--; - lastTxn = System.currentTimeMillis(); + lastTxn = Duration.ofNanos(System.nanoTime()); } long elapsed = chrono.elapsed(); metrics.record("all", elapsed); diff --git a/jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java b/jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java index cc656ba048..714501b4ed 100644 --- a/jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java +++ b/jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java @@ -67,6 +67,7 @@ public static void setUp(@TempDir Path deployDir) throws IOException { }); q2 = new Q2(deployDir.toString()); q2.start(); + q2.ready(5000L); mux = NameRegistrar.get("mux.mux", 2000L); assertNotNull(mux); receivedHandback = null;