Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
Merged
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
24 changes: 20 additions & 4 deletions src/main/java/com/bandwidth/sdk/BandwidthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Closeable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
Expand All @@ -64,7 +65,7 @@
*
* Created by sbarstow on 10/3/14.
*/
public class BandwidthClient implements Client{
public class BandwidthClient implements Client, Closeable {

private final static Logger LOG = LoggerFactory.getLogger(BandwidthClient.class);
public static final int MONITOR_TIMER = 5000;
Expand Down Expand Up @@ -144,6 +145,12 @@ public synchronized static BandwidthClient getInstance() {
return INSTANCE;
}

public synchronized static void shutdown() {
if (INSTANCE != null) {
INSTANCE.close();
INSTANCE = null;
}
}

/**
* Constructor. Instances are created through getInstance() method.
Expand Down Expand Up @@ -770,11 +777,20 @@ public void shutdown() {
protected void finalize() throws Throwable
{
try {
this.idleConnectionMonitorRunnable.shutdown();
} catch(Throwable t){
throw t;
close();
} finally {
super.finalize();
}
}

@Override
public void close() {
if (!this.idleConnectionMonitorRunnable.shutdown) {
this.idleConnectionMonitorRunnable.shutdown();
}
if (!this.executorService.isShutdown()) {
this.executorService.shutdown();
}
}

}
8 changes: 1 addition & 7 deletions src/test/java/com/bandwidth/sdk/MockClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
import java.util.Map;

public class MockClient extends BandwidthClient {
private String userId;
private String token;
private String secret;
private String endpoint;
private String version;

public MockClient() {
public MockClient() {
super(TestsHelper.TEST_USER_ID, "", "", "", "", 200, 20);
}

Expand All @@ -33,11 +30,8 @@ public MockClient(final String userId,
final int maxConnections,
final int defaultMaxPerRoute){
super(userId, token, secret, endpoint, version, maxConnections, defaultMaxPerRoute);
this.userId = userId;
this.token = token;
this.secret = secret;
this.endpoint = endpoint;
this.version = version;
}

public final List<RestRequest> requests = new ArrayList<RestRequest>();
Expand Down