Interface Network
Gateway.getNetwork(String)
method.
The Network provides the ability for applications to:
- Obtain a specific smart contract deployed to the network using
getContract(String)
, in order to submit and evaluate transactions for that smart contract. - Listen for chaincode events emitted by transactions when they are committed to the ledger using
getChaincodeEvents(String)
ornewChaincodeEventsRequest(String)
. - Listen for block events emitted when blocks are committed to the ledger:
- Blocks using
getBlockEvents()
ornewBlockEventsRequest()
. - Filtered blocks
getFilteredBlockEvents()
ornewFilteredBlockEventsRequest()
. - Blocks and private data
getBlockAndPrivateDataEvents()
ornewBlockAndPrivateDataEventsRequest()
.
- Blocks using
To safely handle connection errors during eventing, it is recommended to use a checkpointer to track eventing progress. This allows eventing to be resumed with no loss or duplication of events.
Chaincode events example
Checkpointer checkpointer = new InMemoryCheckpointer();
while (true) {
ChaincodeEventsRequest request = network.newChaincodeEventsRequest("chaincodeName")
.checkpoint(checkpointer)
.startBlock(blockNumber) // Ignored if the checkpointer has checkpoint state
.build();
try (CloseableIterator<ChaincodeEvent> events = request.getEvents()) {
events.forEachRemaining(event -> {
// Process then checkpoint event
checkpointer.checkpointChaincodeEvent(event);
});
} catch (GatewayRuntimeException e) {
// Connection error
}
}
Block events example
Checkpointer checkpointer = new InMemoryCheckpointer();
while (true) {
ChaincodeEventsRequest request = network.newBlockEventsRequest()
.checkpoint(checkpointer)
.startBlock(blockNumber) // Ignored if the checkpointer has checkpoint state
.build();
try (CloseableIterator<Block> events = request.getEvents()) {
events.forEachRemaining(event -> {
// Process then checkpoint block
checkpointer.checkpointBlock(event.getHeader().getNumber());
});
} catch (GatewayRuntimeException e) {
// Connection error
}
}
-
Method Summary
Modifier and TypeMethodDescriptiondefault CloseableIterator
<org.hyperledger.fabric.protos.peer.BlockAndPrivateData> Get block and private data events.CloseableIterator
<org.hyperledger.fabric.protos.peer.BlockAndPrivateData> Get block and private data events.default CloseableIterator
<org.hyperledger.fabric.protos.common.Block> Get block events.CloseableIterator
<org.hyperledger.fabric.protos.common.Block> getBlockEvents
(UnaryOperator<CallOptions> options) Get block events.default CloseableIterator
<ChaincodeEvent> getChaincodeEvents
(String chaincodeName) Get events emitted by transaction functions of a specific chaincode from the next committed block.getChaincodeEvents
(String chaincodeName, UnaryOperator<CallOptions> options) Get events emitted by transaction functions of a specific chaincode from the next committed block.default CloseableIterator
<ChaincodeEvent> getChaincodeEvents
(String chaincodeName, CallOption... options) Deprecated.getContract
(String chaincodeName) Get an instance of a contract on the current network.getContract
(String chaincodeName, String contractName) Get an instance of a contract on the current network.default CloseableIterator
<org.hyperledger.fabric.protos.peer.FilteredBlock> Get filtered block events.CloseableIterator
<org.hyperledger.fabric.protos.peer.FilteredBlock> getFilteredBlockEvents
(UnaryOperator<CallOptions> options) Get filtered block events.getName()
Get the name of the network channel.Build a request to receive block and private data events.Build a request to receive block events.newChaincodeEventsRequest
(String chaincodeName) Build a new chaincode events request, which can be used to obtain events emitted by transaction functions of a specific chaincode.Build a request to receive filtered block events.
-
Method Details
-
getContract
Get an instance of a contract on the current network.- Parameters:
chaincodeName
- The name of the chaincode that implements the smart contract.- Returns:
- The contract object.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getContract
Get an instance of a contract on the current network. If the chaincode instance contains more than one smart contract class (available using the latest chaincode programming model), then an individual class can be selected.- Parameters:
chaincodeName
- The name of the chaincode that implements the smart contract.contractName
- The name of the smart contract within the chaincode.- Returns:
- The contract object.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getName
String getName()Get the name of the network channel.- Returns:
- The network name.
-
getChaincodeEvents
Get events emitted by transaction functions of a specific chaincode from the next committed block. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
chaincodeName
- A chaincode name.- Returns:
- Ordered sequence of events.
- Throws:
NullPointerException
- if the chaincode name is null.- See Also:
-
getChaincodeEvents
CloseableIterator<ChaincodeEvent> getChaincodeEvents(String chaincodeName, UnaryOperator<CallOptions> options) Get events emitted by transaction functions of a specific chaincode from the next committed block. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
chaincodeName
- A chaincode name.options
- Function that transforms call options.- Returns:
- Ordered sequence of events.
- Throws:
NullPointerException
- if the chaincode name is null.- See Also:
-
getChaincodeEvents
@Deprecated default CloseableIterator<ChaincodeEvent> getChaincodeEvents(String chaincodeName, CallOption... options) Deprecated.Replaced bygetChaincodeEvents(String, UnaryOperator)
.Get events emitted by transaction functions of a specific chaincode from the next committed block. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
chaincodeName
- A chaincode name.options
- Call options.- Returns:
- Ordered sequence of events.
- Throws:
NullPointerException
- if the chaincode name is null.
-
newChaincodeEventsRequest
Build a new chaincode events request, which can be used to obtain events emitted by transaction functions of a specific chaincode. This can be used to specify a specific ledger start position. Supports offline signing flow.- Parameters:
chaincodeName
- A chaincode name.- Returns:
- A chaincode events request builder.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getBlockEvents
Get block events. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Returns:
- Ordered sequence of events.
- See Also:
-
getBlockEvents
CloseableIterator<org.hyperledger.fabric.protos.common.Block> getBlockEvents(UnaryOperator<CallOptions> options) Get block events. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
options
- Function that transforms call options.- Returns:
- Ordered sequence of events.
- See Also:
-
newBlockEventsRequest
BlockEventsRequest.Builder newBlockEventsRequest()Build a request to receive block events. This can be used to specify a specific ledger start position. Supports offline signing flow.- Returns:
- A block events request builder.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getFilteredBlockEvents
default CloseableIterator<org.hyperledger.fabric.protos.peer.FilteredBlock> getFilteredBlockEvents()Get filtered block events. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Returns:
- Ordered sequence of events.
- See Also:
-
getFilteredBlockEvents
CloseableIterator<org.hyperledger.fabric.protos.peer.FilteredBlock> getFilteredBlockEvents(UnaryOperator<CallOptions> options) Get filtered block events. The Java gRPC implementation may not begin reading events until the first use of the returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
options
- Function that transforms call options.- Returns:
- Ordered sequence of events.
- See Also:
-
newFilteredBlockEventsRequest
FilteredBlockEventsRequest.Builder newFilteredBlockEventsRequest()Build a request to receive filtered block events. This can be used to specify a specific ledger start position. Supports offline signing flow.- Returns:
- A filtered block events request builder.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getBlockAndPrivateDataEvents
default CloseableIterator<org.hyperledger.fabric.protos.peer.BlockAndPrivateData> getBlockAndPrivateDataEvents()Get block and private data events. The Java gRPC implementation may not begin reading events until the first use of the* returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Returns:
- Ordered sequence of events.
- See Also:
-
getBlockAndPrivateDataEvents
CloseableIterator<org.hyperledger.fabric.protos.peer.BlockAndPrivateData> getBlockAndPrivateDataEvents(UnaryOperator<CallOptions> options) Get block and private data events. The Java gRPC implementation may not begin reading events until the first use of the* returned iterator.Note that the returned iterator may throw
StatusRuntimeException
during iteration if a gRPC connection error occurs.- Parameters:
options
- Function that transforms call options.- Returns:
- Ordered sequence of events.
- See Also:
-
newBlockAndPrivateDataEventsRequest
BlockAndPrivateDataEventsRequest.Builder newBlockAndPrivateDataEventsRequest()Build a request to receive block and private data events. This can be used to specify a specific ledger start position. Supports offline signing flow.- Returns:
- A block and private data events request builder.
- Throws:
NullPointerException
- if the chaincode name is null.
-
getChaincodeEvents(String, UnaryOperator)
.