Skip to content

Commit

Permalink
Implemented closable in the account manager to properly close the okh…
Browse files Browse the repository at this point in the history
…ttp connection.

Calling system exit due to okhttp thread hanging.
  • Loading branch information
AlexandrouR committed Mar 4, 2020
1 parent 64fc1b2 commit 242358c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static void main(String[] args) throws Exception {
Console.exitError(USAGE);
}
}

config.save();
// TODO change this with a no argument method when the update is done in web3j
Console.exitSuccess("");
}
}
19 changes: 13 additions & 6 deletions src/main/java/org/web3j/console/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.web3j.console.account;

import java.io.Closeable;
import java.io.IOException;
import java.util.Scanner;

Expand All @@ -28,7 +29,7 @@

import static org.web3j.codegen.Console.exitError;

public class AccountManager {
public class AccountManager implements Closeable {
private static final String USAGE = "account login|logout|create";
private static final String CLOUD_URL = "https://auth.epirus.io";
private OkHttpClient client;
Expand All @@ -39,20 +40,21 @@ public AccountManager(final CliConfig cliConfig, OkHttpClient client) {
this.config = cliConfig;
}

public static void main(final CliConfig config, final String[] args) {
public static void main(final CliConfig config, final String[] args) throws IOException {

Scanner console = new Scanner(System.in);
if ("create".equals(args[0])) {
System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();
new AccountManager(config, new OkHttpClient()).createAccount(email);

AccountManager accountManager = new AccountManager(config, new OkHttpClient());
accountManager.createAccount(email);
accountManager.close();
} else {
exitError(USAGE);
}
}

public void createAccount(String email) {
public void createAccount(String email) throws IOException {
RequestBody requestBody = createRequestBody(email);
Request newAccountRequest = createRequest(requestBody);

Expand All @@ -79,7 +81,6 @@ public void createAccount(String email) {
} catch (IOException e) {
System.out.println("Could not connect to the server.\nReason:" + e.getMessage());
}
client.connectionPool().evictAll();
}

protected final Response executeClientCall(Request newAccountRequest) throws IOException {
Expand All @@ -98,4 +99,10 @@ protected final Request createRequest(RequestBody accountBody) {
.post(accountBody)
.build();
}

@Override
public void close() throws IOException {
this.client.dispatcher().executorService().shutdown();
this.client.connectionPool().evictAll();
}
}

0 comments on commit 242358c

Please sign in to comment.