Skip to content

Commit

Permalink
use synchronizedMap to prevent concurrent modificatrion on channels
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Jul 31, 2024
1 parent 584da0f commit d1f202f
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions jpos/src/main/java/org/jpos/iso/ISOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,7 @@
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.Vector;
import java.util.*;

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

Expand All @@ -118,7 +108,7 @@ public ISOServer(int port, ServerChannel clientSide, ThreadPool pool) {
new ThreadPool (1, DEFAULT_MAX_THREADS) : pool;
listeners = new Vector();
name = "";
channels = new HashMap();
channels = Collections.synchronizedMap(new HashMap<>());
cnt = new int[SIZEOF_CNT];
serverListeners = new ArrayList<ISOServerEventListener>();
}
Expand Down Expand Up @@ -266,18 +256,13 @@ private void shutdownChannels () {
}
}
private void purgeChannels () {
Iterator iter = channels.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
WeakReference ref = (WeakReference) entry.getValue();
ISOChannel c = (ISOChannel) ref.get ();
if (c == null || !c.isConnected()) {
iter.remove ();
}
}
channels.entrySet().removeIf(entry -> {
WeakReference<ISOChannel> ref = entry.getValue();
ISOChannel c = ref.get();
return c == null || !c.isConnected();
});
}


@Override
public ServerSocket createServerSocket(int port) throws IOException {
ServerSocket ss = new ServerSocket();
Expand Down

0 comments on commit d1f202f

Please sign in to comment.