Browse files

Add close() method to PaxosInterface

  • Loading branch information...
1 parent b4685bc commit 71dd22927980349dbf34713cf04ae591d6fc7008 @dgomezferro committed Sep 28, 2012
View
30 src/main/java/com/yahoo/pasc/paxos/client/PaxosClientHandler.java
@@ -22,8 +22,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Random;
+import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
@@ -159,6 +161,21 @@ public synchronized void submitNewRequest(byte[] request) {
}
}
+ public void close() {
+ if (timer != null)
+ timer.cancel();
+ if (resubmit != null)
+ resubmit.cancel();
+ for (Channel c : serverChannels) {
+ if (c != null) {
+ c.close();
+ }
+ }
+ if (connectionThread != null) {
+ connectionThread.interrupt();
+ }
+ }
+
@Override
public synchronized void submitControlMessage(byte[] controlMessage) {
ControlMessage cm = new ControlMessage(clientId, controlMessage);
@@ -167,9 +184,11 @@ public synchronized void submitControlMessage(byte[] controlMessage) {
}
private static final String ELECTION_PATH = "/pasc_election";
+ private Thread connectionThread;
public void start() throws KeeperException, InterruptedException {
- new Thread(new ConnectionThread()).start();
+ connectionThread = new Thread(new ConnectionThread());
+ connectionThread.start();
}
@Override
@@ -337,6 +356,7 @@ private void tryConnect(final int i) {
private volatile boolean exit = false;
private int attempts = 0;
private int timeout = 1000;
+ private Set<ClientBootstrap> bootstraps = new HashSet<ClientBootstrap>();
@Override
public void run() {
@@ -352,6 +372,7 @@ public void run() {
final int id = toConnect.take();
ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
+ bootstraps.add(bootstrap);
// Set up the pipeline factory.
bootstrap.setPipelineFactory(channelPipelineFactory);
@@ -377,13 +398,16 @@ public void operationComplete(ChannelFuture future) throws Exception {
});
}
} catch (InterruptedException e) {
- // ignore
+ close();
}
}
@Override
- public void close() throws IOException {
+ public void close() {
exit = true;
+ for (ClientBootstrap cb : bootstraps) {
+ cb.releaseExternalResources();
+ }
}
}
View
2 src/main/java/com/yahoo/pasc/paxos/client/PaxosInterface.java
@@ -20,4 +20,6 @@
public void submitNewRequest(byte[] request);
public void submitControlMessage(byte[] controlMessage);
+
+ public void close();
}

0 comments on commit 71dd229

Please sign in to comment.