Skip to content

Commit

Permalink
Merge pull request hyperledger-web3j#433 from mushketyk/web3j-shutdown
Browse files Browse the repository at this point in the history
Adds web3j shutdown method
  • Loading branch information
conor10 authored Mar 23, 2018
2 parents a711aa6 + ee152f4 commit ec465ac
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/src/main/java/org/web3j/protocol/Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ static Web3j build(
ScheduledExecutorService scheduledExecutorService) {
return new JsonRpc2_0Web3j(web3jService, pollingInterval, scheduledExecutorService);
}

/**
* Shutdowns a Web3j instance and closes opened resources.
*/
void shutdown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class JsonRpc2_0Web3j implements Web3j {
protected final Web3jService web3jService;
private final JsonRpc2_0Rx web3jRx;
private final long blockTime;
private final ScheduledExecutorService scheduledExecutorService;

public JsonRpc2_0Web3j(Web3jService web3jService) {
this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService());
Expand All @@ -86,6 +87,7 @@ public JsonRpc2_0Web3j(
this.web3jService = web3jService;
this.web3jRx = new JsonRpc2_0Rx(this, scheduledExecutorService);
this.blockTime = pollingInterval;
this.scheduledExecutorService = scheduledExecutorService;
}

@Override
Expand Down Expand Up @@ -773,4 +775,9 @@ public Observable<EthBlock> catchUpToLatestAndSubscribeToNewBlocksObservable(
return web3jRx.catchUpToLatestAndSubscribeToNewTransactionsObservable(
startBlock, blockTime);
}

@Override
public void shutdown() {
scheduledExecutorService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public HttpService(OkHttpClient httpClient) {
this(DEFAULT_URL, httpClient);
}

public HttpService(boolean includeRawResponse) {
public HttpService(boolean includeRawResponse) {
this(DEFAULT_URL, includeRawResponse);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.web3j.protocol.core;

import java.util.concurrent.ScheduledExecutorService;

import org.junit.Test;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class JsonRpc2_0Web3jTest {

@Test
public void testStopExecutorOnShutdown() {
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);

Web3j web3j = Web3j.build(
mock(Web3jService.class), 10, scheduledExecutorService
);

web3j.shutdown();

verify(scheduledExecutorService).shutdown();
}
}
6 changes: 6 additions & 0 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ For further information refer to :doc:`infura`.
Instructions on obtaining Ether to transact on the network can be found in the
:ref:`testnet section of the docs <ethereum-testnets>`.

When you no longer need a `Web3j` instance you need to call the `shutdown` method to close resources used by it.

.. code-block:: java
web3.shutdown()
Start sending requests
----------------------
Expand Down

0 comments on commit ec465ac

Please sign in to comment.