Skip to content

Commit 99b221c

Browse files
committed
Use monotonic clocks for QMUX.
1 parent 6c05df8 commit 99b221c

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

jpos/src/main/java/org/jpos/q2/iso/QMUX.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import java.io.IOException;
3232
import java.io.PrintStream;
33+
import java.time.Duration;
34+
import java.time.Instant;
3335
import java.util.*;
3436
import java.util.concurrent.ScheduledFuture;
3537
import java.util.concurrent.TimeUnit;
@@ -58,7 +60,7 @@ public class QMUX
5860

5961
List<ISORequestListener> listeners;
6062
private volatile int rx, tx, rxExpired, txExpired, rxPending, rxUnhandled, rxForwarded;
61-
private volatile long lastTxn = 0L;
63+
private volatile Duration lastTxn = Duration.ZERO;
6264
private boolean listenerRegistered;
6365
public QMUX () {
6466
super ();
@@ -161,7 +163,7 @@ public ISOMsg request (ISOMsg m, long timeout) throws ISOException {
161163
if (resp != null)
162164
{
163165
rx++;
164-
lastTxn = System.currentTimeMillis();
166+
lastTxn = Duration.ofNanos(System.nanoTime());
165167
}else {
166168
rxExpired++;
167169
if (m.getDirection() != ISOMsg.OUTGOING)
@@ -351,7 +353,7 @@ public boolean removeISORequestListener(ISORequestListener l) {
351353
}
352354
public synchronized void resetCounters() {
353355
rx = tx = rxExpired = txExpired = rxPending = rxUnhandled = rxForwarded = 0;
354-
lastTxn = 0l;
356+
lastTxn = Duration.ZERO;
355357
}
356358
public String getCountersAsString () {
357359
StringBuffer sb = new StringBuffer();
@@ -366,10 +368,10 @@ public String getCountersAsString () {
366368
sb.append (", connected=");
367369
sb.append (Boolean.toString(isConnected()));
368370
sb.append (", last=");
369-
sb.append (lastTxn);
370-
if (lastTxn > 0) {
371+
sb.append (getLastTxnTimestampInMillis());
372+
if (lastTxn != Duration.ZERO) {
371373
sb.append (", idle=");
372-
sb.append(System.currentTimeMillis() - lastTxn);
374+
sb.append(Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis());
373375
sb.append ("ms");
374376
}
375377
return sb.toString();
@@ -413,10 +415,10 @@ public int getRXForwarded() {
413415
}
414416

415417
public long getLastTxnTimestampInMillis() {
416-
return lastTxn;
418+
return Instant.now().minus(Duration.ofNanos(System.nanoTime()).minus(lastTxn)).toEpochMilli();
417419
}
418420
public long getIdleTimeInMillis() {
419-
return lastTxn > 0L ? System.currentTimeMillis() - lastTxn : -1L;
421+
return lastTxn != Duration.ZERO ? Duration.ofNanos(System.nanoTime()).minus(lastTxn).toMillis() : -1L;
420422
}
421423

422424
protected void processUnhandled (ISOMsg m) {
@@ -529,7 +531,7 @@ public void responseReceived (ISOMsg response) {
529531
synchronized (QMUX.this) {
530532
rx++;
531533
rxPending--;
532-
lastTxn = System.currentTimeMillis();
534+
lastTxn = Duration.ofNanos(System.nanoTime());
533535
}
534536
long elapsed = chrono.elapsed();
535537
metrics.record("all", elapsed);

jpos/src/test/java/org/jpos/q2/iso/QMUXTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static void setUp(@TempDir Path deployDir) throws IOException {
6767
});
6868
q2 = new Q2(deployDir.toString());
6969
q2.start();
70+
q2.ready(5000L);
7071
mux = NameRegistrar.get("mux.mux", 2000L);
7172
assertNotNull(mux);
7273
receivedHandback = null;

0 commit comments

Comments
 (0)