use of org.web3j.protocol.websocket.WebSocketClient in project besu by hyperledger.
the class BesuNode method checkIfWebSocketEndpointIsAvailable.
private void checkIfWebSocketEndpointIsAvailable(final String url) {
final WebSocketClient webSocketClient = new WebSocketClient(URI.create(url));
// Web3j implementation always invoke the listener (even when one hasn't been set). We are using
// this stub implementation to avoid a NullPointerException.
webSocketClient.setListener(new WebSocketListener() {
@Override
public void onMessage(final String message) {
// DO NOTHING
}
@Override
public void onError(final Exception e) {
// DO NOTHING
}
@Override
public void onClose() {
// DO NOTHING
}
});
// Because we can't trust the connection timeout of the WebSocket client implementation, we are
// using this approach to verify if the endpoint is enabled.
webSocketClient.connect();
try {
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(webSocketClient::isOpen);
} catch (final ConditionTimeoutException e) {
throw new WebsocketNotConnectedException();
} finally {
webSocketClient.close();
}
}
use of org.web3j.protocol.websocket.WebSocketClient in project besu by hyperledger.
the class BesuNode method nodeRequests.
private NodeRequests nodeRequests() {
Optional<WebSocketService> websocketService = Optional.empty();
if (nodeRequests == null) {
final Web3jService web3jService;
if (useWsForJsonRpc) {
final String url = wsRpcBaseUrl().orElse("ws://" + LOCALHOST + ":" + 8546);
final Map<String, String> headers = new HashMap<>();
if (token != null) {
headers.put("Authorization", "Bearer " + token);
}
final WebSocketClient wsClient = new WebSocketClient(URI.create(url), headers);
web3jService = new WebSocketService(wsClient, false);
try {
((WebSocketService) web3jService).connect();
} catch (final ConnectException e) {
throw new RuntimeException(e);
}
websocketService = Optional.of((WebSocketService) web3jService);
} else {
final String url = jsonRpcBaseUrl().orElse(HTTP + LOCALHOST + ":" + 8545);
web3jService = new HttpService(url);
if (token != null) {
((HttpService) web3jService).addHeader("Authorization", "Bearer " + token);
}
}
final ConsensusType bftType = getGenesisConfig().map(gc -> gc.toLowerCase().contains("ibft") ? ConsensusType.IBFT2 : ConsensusType.QBFT).orElse(ConsensusType.IBFT2);
nodeRequests = new NodeRequests(new JsonRpc2_0Web3j(web3jService, 2000, Async.defaultExecutorService()), new CliqueRequestFactory(web3jService), new BftRequestFactory(web3jService, bftType), new PermissioningJsonRpcRequestFactory(web3jService), new AdminRequestFactory(web3jService), new PrivacyRequestFactory(web3jService), new CustomRequestFactory(web3jService), new MinerRequestFactory(web3jService), new TxPoolRequestFactory(web3jService), websocketService, loginRequestFactory());
}
return nodeRequests;
}
use of org.web3j.protocol.websocket.WebSocketClient in project wuhanchain by BSN-DDC.
the class EventListenerByBlock method getBlockEventByListener.
/**
* get block event
*
* @param startBlockNum blockNumber
* @return ddc official contract event data
* @throws Exception
*/
public void getBlockEventByListener(BigInteger startBlockNum, String wsUrl, Map<String, String> httpHeaders) {
try {
// webSocketClient
WebSocketClient webSocketClient = new WebSocketClient(new URI(wsUrl), httpHeaders);
WebSocketService webSocketService = new WebSocketService(webSocketClient, false);
webSocketService.connect();
web3j = Web3j.build(webSocketService);
// ThreadPool
executorService = new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100000), new ThreadPoolExecutor.CallerRunsPolicy());
// startBlock, endBlock, fullTransactionObjects
subscribe = web3j.replayPastAndFutureBlocksFlowable(DefaultBlockParameter.valueOf(startBlockNum), true).doOnError(e -> logger.error("doOnError:" + e.getMessage())).subscribe(this::executeBlock, ex -> logger.error("subscribe:" + ex.getMessage()), () -> onCompleted());
while (true) {
if (subscriptions) {
break;
}
}
} catch (Exception e) {
logger.error("analysisResult:" + e);
}
}
Aggregations