Java SDK中的Hyperledger Fabric添加commitListener。

huangapple go评论68阅读模式
英文:

Java SDK Hyperledger Fabric Add commitListener

问题

我正在尝试使用Java SDK创建一个commit监听器,以便在提交事务后监听提交事件,尽管该监听器没有响应。

我正在使用fabcar示例。

// 创建网关连接
try (Gateway gateway = builder.connect()) {

    // 获取网络和合约
    Network network = gateway.getNetwork("mychannel");
    Contract contract = network.getContract("fabcar");

    FabcarCommitListener listener = new FabcarCommitListener();
    network.addCommitListener(listener, network.getChannel().getPeers(), "createCar");
}

FabcarCommitListener:

public class FabcarCommitListener implements CommitListener {
    @Override
    public void acceptCommit(BlockEvent.TransactionEvent transactionEvent) {
        System.out.println("TX COMMITTED");
    }

    @Override
    public void acceptDisconnect(PeerDisconnectEvent peerDisconnectEvent) {
        System.out.println("peerDisconnected");
    }
}

有关使用Java SDK如何工作的commit监听器的任何想法吗?

英文:

I'm trying to create a commitListener using the Java SDK to listen for commit events after submitting a transaction, although the listener is not responding.

I'm using the fabcar example.

// create a gateway connection
		try (Gateway gateway = builder.connect()) {

			// get the network and contract
			Network network = gateway.getNetwork("mychannel");
			Contract contract = network.getContract("fabcar");

			FabcarCommitListener listener = new FabcarCommitListener();
			network.addCommitListener(listener, network.getChannel().getPeers(), "createCar");
		}

The FabcarCommitListener:

public class FabcarCommitListener implements CommitListener {
    @Override
    public void acceptCommit(BlockEvent.TransactionEvent transactionEvent) {
        System.out.println("TX COMMITTED");
    }

    @Override
    public void acceptDisconnect(PeerDisconnectEvent peerDisconnectEvent) {
        System.out.println("peerDisconnected");
    }
}

Any ideas how a commitListener works using the Java SDK?

答案1

得分: 1

A commit listener receives events only for a specific transaction invocation, not for all invocations of a given transaction name. Every transaction invocation has its own unique transaction ID, which you can obtain from the Transaction object prior to submitting:

默认情况下,事务提交还会监听交易在对等体中的提交情况,因此您的代码无需监听事务提交。有几种内置策略可用于确定事务何时已成功提交,您可以选择以下之一:

  1. 当连接到网关时:链接:https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Gateway.Builder.html#commitHandler-org.hyperledger.fabric.gateway.spi.CommitHandlerFactory-
  2. 对于特定事务调用:链接:https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Transaction.html#setCommitHandler-org.hyperledger.fabric.gateway.spi.CommitHandlerFactory-

如果您想要实现自己的自定义逻辑以确定事务是否已成功提交,可以编写自己的自定义提交处理程序实现,此实现可以使用提交监听器来识别您关心的所有对等体的提交和连接状态。以下是一个示例提交处理程序和工厂实现,其利用了提交监听器:

如果您想要查看提交到区块链的所有交易,即使只是为了挑选出您关心的某些交易,那么请使用区块监听器:

链接:https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html#addBlockListener-java.util.function.Consumer-

从区块事件中,您可以逐级导航至区块中包含的所有交易。

尽管如此,区块监听器和提交监听器实际上都处理了 Fabric 区块链的机制。因此,检查已在分类账上操作的交易并检查它们是否已成功提交。如果您想要在事务事件周围编排业务流程,那么您实际上应该使用合约事件监听器。

如果您希望在创建新汽车时触发某些业务流程,请实现您的 createCar 事务函数,以便在提交时发出事件:

链接:https://hyperledger.github.io/fabric-chaincode-java/release-2.2/api/org/hyperledger/fabric/shim/ChaincodeStub.html#setEvent-java.lang.String-byte:A-

在客户端应用程序中,只需使用合约事件监听器来监听此事件:

链接:https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Contract.html#addContractListener-java.util.function.Consumer-java.lang.String-

您可以使用 checkpointing 允许您的客户端在客户端应用程序重新启动后在最后处理的区块位置上恢复监听事件:

链接:https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Contract.html#addContractListener-org.hyperledger.fabric.gateway.spi.Checkpointer-java.util.function.Consumer-java.lang.String-

英文:

A commit listener receives events only for a specific transaction invocation, not for all invocations of a given transaction name. Every transaction invocation has its own unique transaction ID, which you can obtain from the Transaction object prior to submitting:

https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Transaction.html#getTransactionId--

By default, a transaction submit will also listen for the transaction to be committed by peers so there is no need for your code to listen for transaction commits. There are several built-in strategies for determining when a transaction has been successfully committed, which you can select either:

  1. When connecting the Gateway: https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Gateway.Builder.html#commitHandler-org.hyperledger.fabric.gateway.spi.CommitHandlerFactory-
  2. For a specific transaction invocation: https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Transaction.html#setCommitHandler-org.hyperledger.fabric.gateway.spi.CommitHandlerFactory-

If you want to implement your own custom logic for identifying whether a transaction has committed successfully, you can write your own custom commit handler implementation, and this implementation can use a commit listener to identify the commit and connection status of all the peers you care about. Here is a sample commit handler and factory implementation that make use of commit listeners:

If you want to look at all the transactions committed to the blockchain, even if only to pick out certain ones you care about, then use a block listener:

https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html#addBlockListener-java.util.function.Consumer-

From the block event you can navigate down through all the transactions included in the block.

Having said all this, both block listeners and commit listeners really deal with the mechanics of Fabric blockchains. So inspecting the transactions that have operated on the ledger and checking whether they were successfully committed. If you want to orchestrate business processes around transactional events then probably you should actually be using a contract event listener.

If you want to trigger some business process when a new car is created, implement your createCar transaction function so that it emits an event when it is committed:

https://hyperledger.github.io/fabric-chaincode-java/release-2.2/api/org/hyperledger/fabric/shim/ChaincodeStub.html#setEvent-java.lang.String-byte:A-

In your client application, simply listen for this event using a contract event listener:

https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Contract.html#addContractListener-java.util.function.Consumer-java.lang.String-

You can use checkpointing to allow your client to resume listening for events at the last processed block position after a client application restart:

https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Contract.html#addContractListener-org.hyperledger.fabric.gateway.spi.Checkpointer-java.util.function.Consumer-java.lang.String-

huangapple
  • 本文由 发表于 2020年9月21日 18:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63990179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定