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 and port 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(); }
Constructor | Description |
---|---|
RpcQueryNode() |
Constructs a new query node with the local loopback address with port 7076 (
[::1]:7076 ). |
RpcQueryNode(int port) |
Constructs a new query node with the local loopback address
[::1] and the specified port. |
RpcQueryNode(String address,
int port) |
Constructs a new query node with the given address and port number.
|
RpcQueryNode(URL address) |
Constructs a new query node with the given address (as a URL).
|
Modifier and Type | Method | Description |
---|---|---|
URL |
getAddress() |
|
int |
getDefaultTimeout() |
|
ExecutorService |
getExecutorService() |
|
RpcRequestSerializer |
getRequestSerializer() |
|
RpcRequestSubmitter |
getRequestSubmitter() |
|
RpcResponseDeserializer |
getResponseDeserializer() |
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequest(Q request) |
Sends a query request to the node via RPC with the default timeout.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequest(Q request,
int timeout) |
Sends a query request to the node via RPC with the specified timeout.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request) |
Sends an asynchronous query request to the node via RPC with the default timeout.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
int timeout) |
Sends an asynchronous query request to the node via RPC with the specified timeout.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
int timeout,
QueryCallback<Q,R> callback) |
Sends an asynchronous query request to the node via RPC with the specified timeout.
|
<Q extends RpcRequest<R>,R extends RpcResponse> |
processRequestAsync(Q request,
QueryCallback<Q,R> callback) |
Sends an asynchronous query request to the node via RPC with the default timeout.
|
String |
processRequestRaw(String jsonRequest,
int timeout) |
Sends a raw JSON query to the RPC server, and then returns the raw JSON response.
|
<R extends RpcResponse> |
processRequestRaw(String jsonRequest,
int 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.
|
RpcQueryNode |
setDefaultTimeout(int defaultTimeout) |
Sets the default timeout period when unspecified for requests.
|
RpcQueryNode |
setExecutorService(ExecutorService executorService) |
Sets the executor service used to process asynchronous queries.
|
RpcQueryNode |
setRequestSerializer(RpcRequestSerializer requestSerializer) |
Sets the serializer object used to convert request instances into string form, which is then sent to the node
for processing.
|
RpcQueryNode |
setRequestSubmitter(RpcRequestSubmitter requestSubmitter) |
Sets the submitter object used to submit requests to the remote node.
|
RpcQueryNode |
setResponseDeserializer(RpcResponseDeserializer responseDeserializer) |
Sets the deserializer object used to convert the string response into a response class.
|
public RpcQueryNode() throws MalformedURLException
[::1]:7076
).MalformedURLException
- if the address cannot be parsedpublic RpcQueryNode(int port) throws MalformedURLException
[::1]
and the specified port.port
- the port which the node is listening onMalformedURLException
- 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(URL address)
address
- the HTTP URL (address and port) which the node is listening onpublic final URL getAddress()
public final int getDefaultTimeout()
0
if timeouts are disabledpublic final RpcQueryNode setDefaultTimeout(int defaultTimeout)
defaultTimeout
- the timeout period in milliseconds, or 0
to disable timeoutspublic final RpcRequestSerializer getRequestSerializer()
public final RpcQueryNode setRequestSerializer(RpcRequestSerializer requestSerializer)
requestSerializer
- the serializer objectpublic final RpcResponseDeserializer getResponseDeserializer()
public final RpcQueryNode setResponseDeserializer(RpcResponseDeserializer responseDeserializer)
responseDeserializer
- the deserializer objectpublic final RpcRequestSubmitter getRequestSubmitter()
public final RpcQueryNode setRequestSubmitter(RpcRequestSubmitter requestSubmitter)
requestSubmitter
- the submitter objectpublic final ExecutorService getExecutorService()
public final RpcQueryNode setExecutorService(ExecutorService executorService)
executorService
- the executor servicepublic <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, int 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 zero for infiniteIOException
- 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)
Future
object
should be used to retrieve the status or result of the request at a later time, and will encapsulate any
IOException
or RpcException
exceptions thrown during the process.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, int timeout)
Future
object
should be used to retrieve the status or result of the request at a later time, and will encapsulate any
IOException
or RpcException
exceptions thrown during the process.Q
- the request typeR
- the response typerequest
- the query request to send to the nodetimeout
- the timeout for the request in milliseconds, or zero for infinitepublic <Q extends RpcRequest<R>,R extends RpcResponse> Future<R> processRequestAsync(Q request, QueryCallback<Q,R> callback)
Future
object may be used to retrieve the status or result of
the request at a later time, and will encapsulate any IOException
or RpcException
exceptions
thrown during the process.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, int timeout, QueryCallback<Q,R> callback)
Future
object may be used to retrieve the status or result of
the request at a later time, and will encapsulate any IOException
or RpcException
exceptions
thrown during the process.Q
- the request typeR
- the response typerequest
- the query request to send to the nodetimeout
- the timeout for the request in milliseconds, or zero for infinitecallback
- the callback to execute after the request has completed (or null for no callback)public <R extends RpcResponse> R processRequestRaw(String jsonRequest, int timeout, Class<R> responseClass) throws IOException, RpcException
R
- the response typejsonRequest
- the JSON query to send to the nodetimeout
- the timeout for the request in milliseconds, or zero for infiniteresponseClass
- 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, int 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,
int, Class)
method.
jsonRequest
- the JSON query to send to the nodetimeout
- the timeout for the request in milliseconds, or zero for infiniteIOException
- if an error occurs with the connection to the nodeCopyright © 2020. All rights reserved.