Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'idea'
apply plugin: 'io.codearte.nexus-staging'

group 'org.web3j'
version '1.6.0'
version '2.0.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand All @@ -33,7 +33,7 @@ ext {
ossrhPassword = project.hasProperty('ossrhPassword') ? project.property('ossrhPassword') : ''

web3jVersion = '3.3.1'
springBootVersion = '1.5.10.RELEASE'
springBootVersion = '2.0.4.RELEASE'

}

Expand All @@ -44,6 +44,7 @@ repositories {
dependencies {
compile "org.springframework.boot:spring-boot-autoconfigure:$springBootVersion",
"org.springframework.boot:spring-boot-actuator:$springBootVersion",
"org.springframework.boot:spring-boot-actuator-autoconfigure:$springBootVersion",
"org.springframework.boot:spring-boot-configuration-processor:$springBootVersion",
"org.web3j:core:$web3jVersion"
compile("org.springframework.boot:spring-boot-starter:$springBootVersion") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.web3j.spring.actuate;

import java.util.concurrent.CompletableFuture;

import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.util.Assert;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthProtocolVersion;
import org.web3j.protocol.core.methods.response.NetPeerCount;
import org.web3j.protocol.core.methods.response.NetVersion;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;

/**
* Health endpoint extension for Web3j
*/

@EndpointWebExtension(endpoint = HealthEndpoint.class)
public class Web3jHealthEndpointWebExtension {

private Web3j web3j;

public Web3jHealthEndpointWebExtension(Web3j web3j) {
Assert.notNull(web3j, "Web3j must not be null");
this.web3j = web3j;
}

@ReadOperation
public Health health() {
Builder builder = Health.up();

try {
boolean listening = web3j.netListening().send().isListening();
if (!listening) {
builder = builder.down();
} else {
CompletableFuture<NetVersion> netVersion = web3j.netVersion().sendAsync();
CompletableFuture<Web3ClientVersion> clientVersion = web3j.web3ClientVersion().sendAsync();
CompletableFuture<EthBlockNumber> ethBlockNumber = web3j.ethBlockNumber().sendAsync();
CompletableFuture<EthProtocolVersion> ethProtocolVersion = web3j.ethProtocolVersion().sendAsync();
CompletableFuture<NetPeerCount> netPeerCount = web3j.netPeerCount().sendAsync();
CompletableFuture.allOf(netVersion, clientVersion, ethBlockNumber, ethProtocolVersion, netPeerCount)
.join();

builder.withDetail("netVersion", netVersion.get().getNetVersion());
builder.withDetail("clientVersion", clientVersion.get().getWeb3ClientVersion());
builder.withDetail("blockNumber", ethBlockNumber.get().getBlockNumber());
builder.withDetail("protocolVersion", ethProtocolVersion.get().getProtocolVersion());
builder.withDetail("netPeerCount", netPeerCount.get().getQuantity());
}
} catch (Exception e) {
builder = builder.down(e);
}

return builder.build();
}
}
66 changes: 0 additions & 66 deletions src/main/java/org/web3j/spring/actuate/Web3jHealthIndicator.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.ipc.UnixIpcService;
import org.web3j.protocol.ipc.WindowsIpcService;
import org.web3j.spring.actuate.Web3jHealthIndicator;
import org.web3j.spring.actuate.Web3jHealthEndpointWebExtension;

import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -93,11 +93,11 @@ private static void configureLogging(OkHttpClient.Builder builder) {
builder.addInterceptor(logging);
}
}



@Bean
@ConditionalOnBean(Web3j.class)
Comment thread
SnuK87 marked this conversation as resolved.
Web3jHealthIndicator web3jHealthIndicator(Web3j web3j) {
return new Web3jHealthIndicator(web3j);
Web3jHealthEndpointWebExtension healthEndpointWebExtension(Web3j web3j) {
return new Web3jHealthEndpointWebExtension(web3j);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.web3j.spring.autoconfigure;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
Expand All @@ -9,24 +15,17 @@
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

import org.web3j.protocol.Service;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.core.JsonRpc2_0Web3j;
import org.web3j.protocol.http.HttpService;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.web3j.spring.actuate.Web3jHealthEndpointWebExtension;

public class Web3jAutoConfigurationTest {

Expand Down Expand Up @@ -98,8 +97,8 @@ public void testNoAdminClient() {
public void testHealthCheckIndicatorDown() {
load(EmptyConfiguration.class, "web3j.client-address=");

HealthIndicator web3jHealthIndicator = this.context.getBean(HealthIndicator.class);
Health health = web3jHealthIndicator.health();
Web3jHealthEndpointWebExtension healthEndpoint = this.context.getBean(Web3jHealthEndpointWebExtension.class);
Health health = healthEndpoint.health();
assertThat(health.getStatus(), equalTo(Status.DOWN));
assertThat(health.getDetails().get("error").toString(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails, the error being returned is "org.web3j.exceptions.MessageDecodingException: Value must be in format 0x[1-9]+[0-9]* or 0x0".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me all tests are green. Could you post the full stacktrace of the exception?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests seem to work for me, could it be a conflict in your env?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests work fine, I had an instance of Test-RPC running on port 8545.

startsWith("java.net.ConnectException: Failed to connect to localhost/"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package org.web3j.spring.autoconfigure;

import static java.util.concurrent.CompletableFuture.supplyAsync;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

import java.math.BigInteger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthProtocolVersion;
import org.web3j.protocol.core.methods.response.NetPeerCount;
import org.web3j.protocol.core.methods.response.NetVersion;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.spring.actuate.Web3jHealthEndpointWebExtension;
import org.web3j.spring.autoconfigure.context.SpringApplicationTest;
import org.web3j.utils.Numeric;

import java.math.BigInteger;

import static java.util.concurrent.CompletableFuture.supplyAsync;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplicationTest.class)
public class Web3jHealthIndicatorTest {


@Autowired
HealthIndicator web3jHealthIndicator;
Web3jHealthEndpointWebExtension web3jHealthIndicator;

@Autowired
Web3j web3j;
Expand Down