public class RpcQueryNode extends Object
This class represents a connection to a specified Nano node endpoint, with the main intent of sending and queuing RPC requests.
To use this class, set the endpoint address and port in the constructor, or use the provided RpcQueryNode.Builder
class
for additional configuration parameters). To make a request, first create a request object that inherits from
RpcRequest
, specifying any parameters within the request's constructor. Now pass this request object to one
of the processRequest
methods within this class, and use the returned RpcResponse
object to access
the response data.
The asynchronous processing methods (processRequestAsync
) support the options to take a callback object
(QueryCallback
) as a parameter, or to return a Future
object. Asynchronous requests will be
processed on threads issued by the ExecutorService
specified within this class.
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 to console
System.out.println("New account: " + response.getAccountAddress());
} catch (RpcException | IOException e) {
e.printStackTrace();
}
RpcQueryNode.Builder
Modifier and Type | Class | Description |
---|---|---|
static class |
RpcQueryNode.Builder |
This builder class allows you to customize and create new instances of the
RpcQueryNode class. |
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() |
|
RpcRequestExecutor |
getRequestExecutor() |
|
RpcRequestSerializer |
getRequestSerializer() |
|
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<? super Q,? super 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<? super Q,? super 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.
|
public RpcQueryNode()
[::1]:7076
).public RpcQueryNode(int port)
[::1]
and the specified port.port
- the port which the node is listening onRpcQueryNode.Builder
public 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 parsedRpcQueryNode.Builder
public RpcQueryNode(URL address)
address
- the HTTP URL (address and port) which the node is listening onRpcQueryNode.Builder
public final URL getAddress()
public final int getDefaultTimeout()
0
if timeouts are disabledpublic final RpcRequestSerializer getRequestSerializer()
public final RpcResponseDeserializer getResponseDeserializer()
public final RpcRequestExecutor getRequestExecutor()
public final ExecutorService getExecutorService()
public <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<? super Q,? super 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<? super Q,? super 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 © 2021. All rights reserved.