Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions jpos/src/main/java/org/jpos/q2/iso/MUXPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jpos.util.NameRegistrar;

import java.io.IOException;
import java.time.Duration;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -70,13 +71,14 @@ public void stopService () {
NameRegistrar.unregister ("mux."+getName ());
}
public ISOMsg request (ISOMsg m, long timeout) throws ISOException {
long maxWait = System.currentTimeMillis() + timeout;
MUX mux = getMUX(m,maxWait);
Duration maxWait = Duration.ofNanos(System.nanoTime()).plus(Duration.ofMillis(timeout));
MUX mux = getMUX(m,timeout);

if (mux != null) {
timeout = maxWait - System.currentTimeMillis();
if (timeout >= 0)
if (timeout == 0)
return mux.request (m, timeout);
else
return mux.request (m, Math.max(maxWait.minus(Duration.ofNanos(System.nanoTime())).toMillis(), 1L));
}
return null;
}
Expand All @@ -96,15 +98,19 @@ public boolean isConnected() {
return false;
}
protected MUX firstAvailableMUX (long maxWait) {
Duration now = Duration.ofNanos(System.nanoTime());
Duration timeout = Duration.ofMillis(maxWait).plus(now);
do {
for (MUX m : mux)
if (isUsable(m))
return m;
ISOUtil.sleep (1000);
} while (System.currentTimeMillis() < maxWait);
} while (maxWait == 0 || Duration.ofNanos(System.nanoTime()).compareTo(timeout) < 0);
return null;
}
protected MUX nextAvailableMUX (int mnumber, long maxWait) {
Duration now = Duration.ofNanos(System.nanoTime());
Duration timeout = Duration.ofMillis(maxWait).plus(now);
do {
for (int i=0; i<mux.length; i++) {
int j = (mnumber+i) % mux.length;
Expand All @@ -113,7 +119,7 @@ protected MUX nextAvailableMUX (int mnumber, long maxWait) {
msgno.incrementAndGet();
}
ISOUtil.sleep (1000);
} while (System.currentTimeMillis() < maxWait);
} while (maxWait == 0 || Duration.ofNanos(System.nanoTime()).compareTo(timeout) < 0);
return null;
}
private String[] toStringArray (String s) {
Expand All @@ -129,20 +135,14 @@ private String[] toStringArray (String s) {
public void request (ISOMsg m, long timeout, final ISOResponseListener r, final Object handBack)
throws ISOException
{
long maxWait = System.currentTimeMillis() + timeout;
MUX mux = getMUX(m,maxWait);
Duration maxWait = Duration.ofNanos(System.nanoTime()).plus(Duration.ofMillis(timeout));
MUX mux = getMUX(m,timeout);

if (mux != null) {
timeout = maxWait - System.currentTimeMillis();
if (timeout >= 0)
if (timeout == 0)
mux.request(m, timeout,r, handBack);
else {
new Thread() {
public void run() {
r.expired (handBack);
}
}.start();
}
else
mux.request(m, Math.max(maxWait.minus(Duration.ofNanos(System.nanoTime())).toMillis(), 1L),r, handBack);
} else
throw new ISOException ("No MUX available");
}
Expand Down
2 changes: 1 addition & 1 deletion jpos/src/test/java/org/jpos/q2/iso/MUXPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testRequestThrowsNullPointerException() throws Throwable {
if (isJavaVersionAtMost(JAVA_14)) {
assertNull(ex.getMessage(), "ex.getMessage()");
} else {
assertEquals("Cannot read the array length because \"<local4>\" is null", ex.getMessage(), "ex.getMessage()");
assertEquals("Cannot read the array length because \"<local6>\" is null", ex.getMessage(), "ex.getMessage()");
}
}
}
Expand Down