Keep references to threads and shut them down more properly
This commit is contained in:
@ -18,7 +18,7 @@ abstract public class ActiveGossipThread implements Runnable {
|
|||||||
|
|
||||||
private GossipManager _gossipManager;
|
private GossipManager _gossipManager;
|
||||||
|
|
||||||
private AtomicBoolean _keepRunning;
|
private final AtomicBoolean _keepRunning;
|
||||||
|
|
||||||
public ActiveGossipThread(GossipManager gossipManager) {
|
public ActiveGossipThread(GossipManager gossipManager) {
|
||||||
_gossipManager = gossipManager;
|
_gossipManager = gossipManager;
|
||||||
@ -36,10 +36,12 @@ abstract public class ActiveGossipThread implements Runnable {
|
|||||||
_keepRunning.set(false);
|
_keepRunning.set(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
shutdown();
|
||||||
_keepRunning = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shutdown(){
|
||||||
|
_keepRunning.set(false);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Performs the sending of the membership list, after we have
|
* Performs the sending of the membership list, after we have
|
||||||
* incremented our own heartbeat.
|
* incremented our own heartbeat.
|
||||||
|
@ -38,8 +38,10 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
|||||||
private ExecutorService _gossipThreadExecutor;
|
private ExecutorService _gossipThreadExecutor;
|
||||||
|
|
||||||
private Class<? extends PassiveGossipThread> _passiveGossipThreadClass;
|
private Class<? extends PassiveGossipThread> _passiveGossipThreadClass;
|
||||||
|
private PassiveGossipThread passiveGossipThread;
|
||||||
|
|
||||||
private Class<? extends ActiveGossipThread> _activeGossipThreadClass;
|
private Class<? extends ActiveGossipThread> _activeGossipThreadClass;
|
||||||
|
private ActiveGossipThread activeGossipThread;
|
||||||
|
|
||||||
public GossipManager(Class<? extends PassiveGossipThread> passiveGossipThreadClass,
|
public GossipManager(Class<? extends PassiveGossipThread> passiveGossipThreadClass,
|
||||||
Class<? extends ActiveGossipThread> activeGossipThreadClass, String address, int port,
|
Class<? extends ActiveGossipThread> activeGossipThreadClass, String address, int port,
|
||||||
@ -118,10 +120,10 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
|||||||
}
|
}
|
||||||
_gossipThreadExecutor = Executors.newCachedThreadPool();
|
_gossipThreadExecutor = Executors.newCachedThreadPool();
|
||||||
try {
|
try {
|
||||||
_gossipThreadExecutor.execute(_passiveGossipThreadClass.getConstructor(GossipManager.class)
|
passiveGossipThread = _passiveGossipThreadClass.getConstructor(GossipManager.class).newInstance(this);
|
||||||
.newInstance(this));
|
_gossipThreadExecutor.execute(passiveGossipThread);
|
||||||
_gossipThreadExecutor.execute(_activeGossipThreadClass.getConstructor(GossipManager.class)
|
activeGossipThread = _activeGossipThreadClass.getConstructor(GossipManager.class).newInstance(this);
|
||||||
.newInstance(this));
|
_gossipThreadExecutor.execute(activeGossipThread);
|
||||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||||
| InvocationTargetException | NoSuchMethodException | SecurityException e1) {
|
| InvocationTargetException | NoSuchMethodException | SecurityException e1) {
|
||||||
throw new RuntimeException(e1);
|
throw new RuntimeException(e1);
|
||||||
@ -142,6 +144,14 @@ public abstract class GossipManager extends Thread implements NotificationListen
|
|||||||
*/
|
*/
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
_gossipThreadExecutor.shutdown();
|
_gossipThreadExecutor.shutdown();
|
||||||
|
passiveGossipThread.shutdown();
|
||||||
|
activeGossipThread.shutdown();
|
||||||
|
try {
|
||||||
|
boolean result = _gossipThreadExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
|
||||||
|
System.err.println("Terminate retuned " + result);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
_gossipServiceRunning.set(false);
|
_gossipServiceRunning.set(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,13 @@ abstract public class PassiveGossipThread implements Runnable {
|
|||||||
_keepRunning.set(false);
|
_keepRunning.set(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shutdown(){
|
||||||
|
_server.close();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract method for merging the local and remote list.
|
* Abstract method for merging the local and remote list.
|
||||||
* @param gossipManager The GossipManager for retrieving the local members and dead members list.
|
* @param gossipManager The GossipManager for retrieving the local members and dead members list.
|
||||||
|
@ -18,6 +18,15 @@ public class TenNodeThreeSeedTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws UnknownHostException, InterruptedException{
|
public void test() throws UnknownHostException, InterruptedException{
|
||||||
|
abc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAgain() throws UnknownHostException, InterruptedException{
|
||||||
|
abc();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void abc() throws InterruptedException, UnknownHostException{
|
||||||
GossipSettings settings = new GossipSettings();
|
GossipSettings settings = new GossipSettings();
|
||||||
int seedNodes = 3;
|
int seedNodes = 3;
|
||||||
ArrayList<GossipMember> startupMembers = new ArrayList<GossipMember>();
|
ArrayList<GossipMember> startupMembers = new ArrayList<GossipMember>();
|
||||||
@ -39,6 +48,6 @@ public class TenNodeThreeSeedTest {
|
|||||||
}
|
}
|
||||||
for (int i = 0; i < clusterMembers; ++i) {
|
for (int i = 0; i < clusterMembers; ++i) {
|
||||||
clients.get(i).shutdown();
|
clients.get(i).shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user