public class RpcQueryNode extends Object
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();
}
RpcServiceProviders
,
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 RPC communications object via
http on localhost (::1 ) on port
. |
RpcQueryNode(int port) |
Constructs a new RPC communications object via
http on localhost (::1 ) on the specified port. |
RpcQueryNode(String address,
int port) |
Constructs a new RPC communications object via
http on the specified host address and port. |
RpcQueryNode(URL url) |
Constructs a new RPC communications object via the specified protocol, host address and port.
|
Modifier and Type | Method | Description |
---|---|---|
static RpcQueryNode.Builder |
builder() |
Returns a new builder object for constructing
RpcQueryNode objects. |
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()
http
on localhost (::1
) on port
.public RpcQueryNode(int port)
http
on localhost (::1
) on the specified port.port
- the endpoint port numberRpcQueryNode.Builder
public RpcQueryNode(String address, int port)
http
on the specified host address and port.address
- the endpoint address or hostnameport
- the endpoint port numberRpcQueryNode.Builder
public RpcQueryNode(URL url)
http
or https
.url
- the endpoint URL of the node (protocol, address and port)RpcQueryNode.Builder
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
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 nodepublic static RpcQueryNode.Builder builder()
RpcQueryNode
objects.Copyright © 2021. All rights reserved.