Skip to content

Commit

Permalink
Web3j implements AutoClosable Interface (#2102)
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Börner <[email protected]>
  • Loading branch information
steffenboe authored Sep 25, 2024
1 parent 650e6d6 commit 8fa31dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/web3j/protocol/Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.web3j.protocol.rx.Web3jRx;

/** JSON-RPC Request object building factory. */
public interface Web3j extends Ethereum, Web3jRx, Batcher, BlobFee {
public interface Web3j extends Ethereum, Web3jRx, Batcher, BlobFee, AutoCloseable {

/**
* Construct a new Web3j instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,9 @@ private static BigInteger fakeExponential(BigInteger numerator) {
}
return output.divide(BLOB_BASE_FEE_UPDATE_FRACTION);
}

@Override
public void close() throws Exception {
this.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public class JsonRpc2_0Web3jTest {
public void testStopExecutorOnShutdown() throws Exception {
web3j.shutdown();

verify(scheduledExecutorService).shutdown();
verify(service).close();
verifyServicesClosed();
}

@Test
Expand All @@ -52,4 +51,15 @@ public void testExceptionOnServiceClosure() throws Exception {
web3j.shutdown();
});
}

@Test
public void shouldShutdownOnAutoClose() throws Exception {
try (Web3j web3j = Web3j.build(service, 10, scheduledExecutorService)) {}
verifyServicesClosed();
}

private void verifyServicesClosed() throws IOException {
verify(scheduledExecutorService).shutdown();
verify(service).close();
}
}

0 comments on commit 8fa31dd

Please sign in to comment.