From 7ec488d69045b7a5721cf0922b0a22a9e44ef3b8 Mon Sep 17 00:00:00 2001 From: Ivan Mushketyk Date: Mon, 19 Mar 2018 23:09:31 +0000 Subject: [PATCH 1/2] Adds web3j shutdown method --- .../main/java/org/web3j/protocol/Web3j.java | 5 ++++ .../web3j/protocol/core/JsonRpc2_0Web3j.java | 7 +++++ .../org/web3j/protocol/http/HttpService.java | 2 +- .../protocol/core/JsonRpc2_0Web3jTest.java | 27 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/org/web3j/protocol/core/JsonRpc2_0Web3jTest.java diff --git a/core/src/main/java/org/web3j/protocol/Web3j.java b/core/src/main/java/org/web3j/protocol/Web3j.java index bdbf07a4c..c5a62f5a9 100644 --- a/core/src/main/java/org/web3j/protocol/Web3j.java +++ b/core/src/main/java/org/web3j/protocol/Web3j.java @@ -36,4 +36,9 @@ static Web3j build( ScheduledExecutorService scheduledExecutorService) { return new JsonRpc2_0Web3j(web3jService, pollingInterval, scheduledExecutorService); } + + /** + * Shutdowns a Web3j instance and cleans up opened resources. + */ + void shutdown(); } diff --git a/core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java b/core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java index ab0671db5..23c0487c8 100644 --- a/core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java +++ b/core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java @@ -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()); @@ -86,6 +87,7 @@ public JsonRpc2_0Web3j( this.web3jService = web3jService; this.web3jRx = new JsonRpc2_0Rx(this, scheduledExecutorService); this.blockTime = pollingInterval; + this.scheduledExecutorService = scheduledExecutorService; } @Override @@ -773,4 +775,9 @@ public Observable catchUpToLatestAndSubscribeToNewBlocksObservable( return web3jRx.catchUpToLatestAndSubscribeToNewTransactionsObservable( startBlock, blockTime); } + + @Override + public void shutdown() { + scheduledExecutorService.shutdown(); + } } diff --git a/core/src/main/java/org/web3j/protocol/http/HttpService.java b/core/src/main/java/org/web3j/protocol/http/HttpService.java index b8a4df2aa..a321fe5e2 100644 --- a/core/src/main/java/org/web3j/protocol/http/HttpService.java +++ b/core/src/main/java/org/web3j/protocol/http/HttpService.java @@ -67,7 +67,7 @@ public HttpService(OkHttpClient httpClient) { this(DEFAULT_URL, httpClient); } - public HttpService(boolean includeRawResponse) { + public HttpService(boolean includeRawResponse) { this(DEFAULT_URL, includeRawResponse); } diff --git a/core/src/test/java/org/web3j/protocol/core/JsonRpc2_0Web3jTest.java b/core/src/test/java/org/web3j/protocol/core/JsonRpc2_0Web3jTest.java new file mode 100644 index 000000000..4cd379908 --- /dev/null +++ b/core/src/test/java/org/web3j/protocol/core/JsonRpc2_0Web3jTest.java @@ -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(); + } +} \ No newline at end of file From ee152f4b2244fb0da05c3f83dd3538b378b96e60 Mon Sep 17 00:00:00 2001 From: Ivan Mushketyk Date: Tue, 20 Mar 2018 09:07:17 +0000 Subject: [PATCH 2/2] Adds documentation for the Web3j.shutdown method --- core/src/main/java/org/web3j/protocol/Web3j.java | 2 +- docs/source/getting_started.rst | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/web3j/protocol/Web3j.java b/core/src/main/java/org/web3j/protocol/Web3j.java index c5a62f5a9..f67b106e4 100644 --- a/core/src/main/java/org/web3j/protocol/Web3j.java +++ b/core/src/main/java/org/web3j/protocol/Web3j.java @@ -38,7 +38,7 @@ static Web3j build( } /** - * Shutdowns a Web3j instance and cleans up opened resources. + * Shutdowns a Web3j instance and closes opened resources. */ void shutdown(); } diff --git a/docs/source/getting_started.rst b/docs/source/getting_started.rst index 603d7f9ae..083af6e0f 100644 --- a/docs/source/getting_started.rst +++ b/docs/source/getting_started.rst @@ -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 `. +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 ----------------------