Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes ISOServer channels map implementation to ConcurrentHashMap #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
40 changes: 18 additions & 22 deletions jpos/src/main/java/org/jpos/iso/ISOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Observer;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
Expand Down Expand Up @@ -95,7 +96,7 @@ private enum PermLogPolicy {
protected Configuration cfg;
private boolean shutdown = false;
private ServerSocket serverSocket;
private Map channels;
private Map<String, WeakReference<ServerChannel>> channels;
protected boolean ignoreISOExceptions;
protected List<ISOServerEventListener> serverListeners = null;

Expand All @@ -118,7 +119,7 @@ public ISOServer(int port, ServerChannel clientSide, ThreadPool pool) {
new ThreadPool (1, DEFAULT_MAX_THREADS) : pool;
listeners = new Vector();
name = "";
channels = new HashMap();
channels = new ConcurrentHashMap<>();
cnt = new int[SIZEOF_CNT];
serverListeners = new ArrayList<ISOServerEventListener>();
}
Expand Down Expand Up @@ -759,26 +760,21 @@ public String getCountersAsString (String isoChannelName) {
return sb.toString();
}
@Override
public void dump (PrintStream p, String indent) {
p.println (indent + getCountersAsString());
Iterator iter = channels.entrySet().iterator();
String inner = indent + " ";
for (int i=0; iter.hasNext(); i++) {
Map.Entry entry = (Map.Entry) iter.next();
WeakReference ref = (WeakReference) entry.getValue();
ISOChannel c = (ISOChannel) ref.get ();
if (c != null && !LAST.equals (entry.getKey()) && c.isConnected() && c instanceof BaseChannel) {
StringBuilder sb = new StringBuilder ();
int[] cc = ((BaseChannel)c).getCounters();
sb.append (inner);
sb.append (entry.getKey());
sb.append (": rx=");
sb.append (Integer.toString (cc[ISOChannel.RX]));
sb.append (", tx=");
sb.append (Integer.toString (cc[ISOChannel.TX]));
sb.append (", last=");
sb.append (Long.toString(lastTxn));
p.println (sb.toString());
public void dump(PrintStream p, String indent) {
String counters = getCountersAsString();
p.println (indent + counters);

for (Map.Entry<String, WeakReference<ServerChannel>> entry : channels.entrySet()) {
String key = entry.getKey();
//Ignore last as it is a special key with the last channel used and would result in duplication.
if (!LAST.equals(key)) {
ISOChannel c = entry.getValue().get();
if (c != null && c.isConnected() && c instanceof BaseChannel) {
int[] cc = ((BaseChannel) c).getCounters();
String line = String.format("%s %s: rx=%d, tx=%d, last=%d",
indent, key, cc[ISOChannel.RX], cc[ISOChannel.TX], lastTxn);
p.println(line);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions jpos/src/test/java/org/jpos/iso/ISOServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
import static org.junit.jupiter.api.Assertions.fail;

import org.jpos.util.NameRegistrar;
import org.jpos.util.ThreadPool;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;

public class ISOServerTest {

@Test
Expand All @@ -52,4 +58,16 @@ public void testGetServerThrowsNotFoundException() throws Throwable {
assertEquals("server.testISOServerName", ex.getMessage(), "ex.getMessage()");
}
}


@Test
public void testDump() throws Throwable {
ISOServer server = new ISOServer(80, new BaseChannel() {}, new ThreadPool());
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);

server.dump(ps, "");

assertEquals("connected=0, rx=0, tx=0, last=0\n", os.toString("UTF8"));
}
}