public class RpcQueryNode extends Object
This class represents a connection to a specified Nano node endpoint, with the main purpose of sending and queuing RPC requests.
To use this class, set the endpoint address, port and authorization token (if configured) in the constructor,
and then pass request arguments to one of the processRequest()
methods. Asynchronous requests can also be
accomplished using one of the processRequestAsync
methods, which can take a callback, as well as returning a
future object representing the response.
Below is an example of a synchronous query which creates a new account from a provided wallet ID:
try { // Configure a connection to localhost:7076 RpcQueryNode node = new RpcQueryNode(); // Construct the request (and pass query arguments) RequestAccountCreate request = new RequestAccountCreate( "B4ECF585D887B590907949C41F73BB11AA0BD4FD98563CC5D810EF26FAAD948E"); // Wallet ID // Send request to the node synchronously and retrieve response ResponseAccount response = node.processRequest(request); // Output new account System.out.println("New account: " + response.getAccountAddress()); } catch (RpcException | IOException e) { e.printStackTrace(); }
Modifier and Type | Field | Description |
---|---|---|
protected static ExecutorService |
EXECUTOR_SERVICE |
Constructor | Description |
---|---|
RpcQueryNode() |
Constructs a new query node with the address
127.0.0.1:7076 . |
RpcQueryNode(String authToken) |
Constructs a new query node with the address
127.0.0.1:7076 and specified authorization token. |
RpcQueryNode(String address,
int port) |
Constructs a new query node with the provided address and port.
|
RpcQueryNode(String address,
int port,
String authToken) |
Constructs a new query node with the given address, port and authorization token.
|
RpcQueryNode(URL address) |
Constructs a new query node with the given address (as a URL).
|
RpcQueryNode(URL address,
String authToken) |
Constructs a new query node with the given address (as a URL) and authorization token.
|
Modifier and Type | Method | Description |
---|---|---|
protected <R extends RpcResponse> |
deserializeResponseFromJSON(String responseJson,
Class<R> responseClass) |
Converts a pure JSON string into a response instance.
|
URL |
getAddress() |
|
String |
getAuthToken() |
|
com.google.gson.Gson |
getGsonInstance() |
Deprecated, for removal: This API element is subject to removal in a future version.
moved to use internal static utility
|
protected RpcException |
parseException(String message) |
Parses a returned error string into the appropriate RpcException type.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequest(Q request) |
Sends a query request to the node via RPC.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequest(Q request,
Integer timeout) |
Sends a query request to the node via RPC.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request) |
Sends a query request to the node via RPC.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
Integer timeout) |
Sends a query request to the node via RPC.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
Integer timeout,
QueryCallback<Q,R> callback) |
Sends a query request to the node via RPC.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
QueryCallback<Q,R> callback) |
Sends a query request to the node via RPC.
|
String |
processRequestRaw(String jsonRequest,
Integer timeout) |
Sends a raw JSON query to the RPC server, and then returns the raw JSON response.
|
<R extends RpcResponse> |
processRequestRaw(String jsonRequest,
Integer timeout,
Class<R> responseClass) |
Sends a raw JSON query to the RPC server, and then returns an object in the specified class containing the
deserialized response data.
|
String |
serializeRequestToJSON(RpcRequest<?> req) |
Converts a request instance into a pure JSON string.
|
void |
setAuthToken(String authToken) |
Sets the authorization token to be used with future requests.
|
protected static final ExecutorService EXECUTOR_SERVICE
public RpcQueryNode() throws MalformedURLException
127.0.0.1:7076
.MalformedURLException
- if the address cannot be parsedpublic RpcQueryNode(String authToken) throws MalformedURLException
127.0.0.1:7076
and specified authorization token.authToken
- the authorization token to be sent with queriesMalformedURLException
- if the address cannot be parsedpublic RpcQueryNode(String address, int port) throws MalformedURLException
address
- the address of the nodeport
- the port which the node is listening onMalformedURLException
- if the address cannot be parsedpublic RpcQueryNode(String address, int port, String authToken) throws MalformedURLException
address
- the address of the nodeport
- the port which the node is listening onauthToken
- the authorization token to be sent with queriesMalformedURLException
- if the address cannot be parsedpublic RpcQueryNode(URL address)
address
- the HTTP URL (address and port) which the node is listening onpublic final URL getAddress()
public final String getAuthToken()
public final void setAuthToken(String authToken)
authToken
- the new token to be used for queries, or null to removepublic <Q extends RpcRequest<R>,R extends RpcResponse> R processRequest(Q request) throws IOException, RpcException
Q
- the request typeR
- the response typerequest
- the query request to send to the nodeIOException
- if an error occurs with the connection to the nodeRpcException
- if the node returns a non-successful responsepublic <Q extends RpcRequest<R>,R extends RpcResponse> R processRequest(Q request, Integer timeout) throws IOException, RpcException
Q
- the request typeR
- the response typerequest
- the query request to send to the nodetimeout
- the timeout for the request in milliseconds, or null for noneIOException
- if an error occurs with the connection to the nodeRpcException
- if the node returns a non-successful responsepublic <Q extends RpcRequest<R>,R extends RpcResponse> Future<R> processRequestAsync(Q request)
Q
- the request typeR
- the response typerequest
- the query request to send to the nodepublic <Q extends RpcRequest<R>,R extends RpcResponse> Future<R> processRequestAsync(Q request, Integer timeout)
Q
- the request typeR
- the response typerequest
- the query request to send to the nodetimeout
- the timeout for the request in milliseconds, or null for nonepublic <Q extends RpcRequest<R>,R extends RpcResponse> Future<R> processRequestAsync(Q request, QueryCallback<Q,R> callback)
Q
- the request typeR
- the response typerequest
- the query request to send to the nodecallback
- the callback to execute after the request has completed (or null for no callback)public <Q extends RpcRequest<R>,R extends RpcResponse> Future<R> processRequestAsync(Q request, Integer timeout, QueryCallback<Q,R> callback)
Q
- the request typeR
- the response typerequest
- the query request to send to the nodetimeout
- the timeout for the request in milliseconds, or null for nonecallback
- the callback to execute after the request has completed (or null for no callback)public <R extends RpcResponse> R processRequestRaw(String jsonRequest, Integer timeout, Class<R> responseClass) throws IOException, RpcException
R
- the response typejsonRequest
- the JSON query to send to the nodetimeout
- the connection timeout in milliseconds, or null to disable timeoutsresponseClass
- the class to deserialize the response data intoIOException
- if an error occurs with the connection to the nodeRpcException
- if the node returns a non-successful responsepublic String processRequestRaw(String jsonRequest, Integer timeout) throws IOException
Sends a raw JSON query to the RPC server, and then returns the raw JSON response.
Note that this method will not deserialize the resulting JSON, or parse it for errors reported by the node.
You will need to implement this functionality yourself, or use the alternate processRequestRaw(String,
Integer, Class)
method.
jsonRequest
- the JSON query to send to the nodetimeout
- the connection timeout in milliseconds, or null to disable timeoutsIOException
- if an error occurs with the connection to the nodeprotected <R extends RpcResponse> R deserializeResponseFromJSON(String responseJson, Class<R> responseClass) throws RpcException
R
- the response typeresponseJson
- the JSON to deserializeresponseClass
- the response class to deserialize intoRpcException
- if the node returns a non-successful responseprotected RpcException parseException(String message)
message
- the returned error message from the nodepublic String serializeRequestToJSON(RpcRequest<?> req)
req
- the request to serialize@Deprecated(forRemoval=true) public final com.google.gson.Gson getGsonInstance()
Copyright © 2020. All rights reserved.