Skip to content

Commit

Permalink
Merge pull request #79 from qsoftdevelopment/feature/AddForceDestroy
Browse files Browse the repository at this point in the history
Add forceDestroy method
  • Loading branch information
MaxPresman authored Oct 18, 2017
2 parents 8c16877 + 5fa6852 commit f4f0de7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/main/java/com/pubnub/api/PubNub.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public String decrypt(String inputString, String cipherKey) throws PubNubExcepti
* @param inputString String to be encrypted
* @return String containing the encryption of inputString using cipherKey
*/
public String encrypt(String inputString) throws PubNubException {
public String encrypt(String inputString) throws PubNubException {
if (inputString == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).build();
}
Expand Down Expand Up @@ -250,14 +250,14 @@ public int getTimestamp() {
* @return instance uuid.
*/
public String getInstanceId() {
return instanceId;
return instanceId;
}

/**
* @return request uuid.
*/
public String getRequestId() {
return UUID.randomUUID().toString();
return UUID.randomUUID().toString();
}

/**
Expand All @@ -276,12 +276,24 @@ public void stop() {
}

/**
* Destroy the SDK to evict the connection pools.
* Destroy the SDK to cancel all ongoing requests and stop heartbeat timer.
*/
public void destroy() {
try {
subscriptionManager.destroy();
retrofitManager.destroy();
subscriptionManager.destroy(false);
retrofitManager.destroy(false);
} catch (Exception error) {
//
}
}

/**
* Force destroy the SDK to evict the connection pools and close executors.
*/
public void forceDestroy() {
try {
subscriptionManager.destroy(true);
retrofitManager.destroy(true);
} catch (Exception error) {
//
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/pubnub/api/managers/RetrofitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import retrofit2.Retrofit;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class RetrofitManager {
Expand Down Expand Up @@ -144,17 +145,21 @@ private Retrofit createRetrofit(OkHttpClient client) {
}



public void destroy() {
public void destroy(boolean force) {
if (this.transactionClientInstance != null) {
closeExecutor(this.transactionClientInstance);
closeExecutor(this.transactionClientInstance, force);
}
if (this.subscriptionClientInstance != null) {
closeExecutor(this.subscriptionClientInstance);
closeExecutor(this.subscriptionClientInstance, force);
}
}

private void closeExecutor(OkHttpClient client) {
private void closeExecutor(OkHttpClient client, boolean force) {
client.dispatcher().cancelAll();
if (force) {
client.connectionPool().evictAll();
ExecutorService executorService = client.dispatcher().executorService();
executorService.shutdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ public synchronized void stop() {
consumerThread.interrupt();
}

public synchronized void destroy() {
public synchronized void destroy(boolean forceDestroy) {
this.disconnect();
if (forceDestroy) {
consumerThread.interrupt();
}
}

public synchronized void adaptStateBuilder(StateOperation stateOperation) {
Expand Down

0 comments on commit f4f0de7

Please sign in to comment.