3030
3131import java .io .IOException ;
3232import java .io .PrintStream ;
33+ import java .time .Duration ;
34+ import java .time .Instant ;
3335import java .util .*;
3436import java .util .concurrent .ScheduledFuture ;
3537import 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 = null ;
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 = null ;
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 != null ) {
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,12 @@ public int getRXForwarded() {
413415 }
414416
415417 public long getLastTxnTimestampInMillis () {
416- return lastTxn ;
418+ if (lastTxn == null )
419+ return 0L ;
420+ return Instant .now ().minus (Duration .ofNanos (System .nanoTime ()).minus (lastTxn )).toEpochMilli ();
417421 }
418422 public long getIdleTimeInMillis () {
419- return lastTxn > 0L ? System .currentTimeMillis () - lastTxn : -1L ;
423+ return lastTxn != null ? Duration . ofNanos ( System .nanoTime ()). minus ( lastTxn ). toMillis () : -1L ;
420424 }
421425
422426 protected void processUnhandled (ISOMsg m ) {
@@ -529,7 +533,7 @@ public void responseReceived (ISOMsg response) {
529533 synchronized (QMUX .this ) {
530534 rx ++;
531535 rxPending --;
532- lastTxn = System .currentTimeMillis ( );
536+ lastTxn = Duration . ofNanos ( System .nanoTime () );
533537 }
534538 long elapsed = chrono .elapsed ();
535539 metrics .record ("all" , elapsed );
0 commit comments