在Corda中,如何从服务启动流?

在Corda中,如何从服务启动流?,corda,Corda,我已经创建了一个运行在我的节点上的CordaService。我希望此服务根据各种条件启动流。但是,提供给服务的ServiceHub无法启动流 服务是否有启动流的流?我该怎么做?是的。只需在构造函数中传递CordaService一个AppServiceHub,而不是ServiceHub AppServiceHub接口扩展了ServiceHub接口,使节点能够启动流: interface AppServiceHub : ServiceHub { fun <T> startFlo

我已经创建了一个运行在我的节点上的
CordaService
。我希望此服务根据各种条件启动流。但是,提供给服务的
ServiceHub
无法启动流


服务是否有启动流的流?我该怎么做?

是的。只需在构造函数中传递
CordaService
一个
AppServiceHub
,而不是
ServiceHub

AppServiceHub
接口扩展了
ServiceHub
接口,使节点能够启动流:

interface AppServiceHub : ServiceHub {

    fun <T> startFlow(flow: FlowLogic<T>): FlowHandle<T>

    fun <T> startTrackedFlow(flow: FlowLogic<T>): FlowProgressHandle<T>
}
接口AppServiceHub:ServiceHub{
乐趣开始流(流:流逻辑):流句柄
fun StartTracketFlow(流:FlowLogic):FlowProgressHandle
}

是,将AppServiceHub传递给构造函数

在科特林:

class MyCordaService(private val serviceHub: AppServiceHub) : SingletonSerializeAsToken() {

    init {
        // code ran at service creation / node startup
    }

    // public api of service
}
或Java:

公共类MyCordaService扩展了SingletonSerializeAsToken{
专用AppServiceHub服务中心;
公共MyCordaService(AppServiceHub serviceHub){
this.serviceHub=serviceHub;
//在服务创建/节点启动时运行的代码
}
//公共服务api
}
重要:为了避免运行节点之间可能出现的任何潜在死锁,请从它们自己的线程启动内部流

例如:

公共类MyCordaService扩展了SingletonSerializeAsToken{
专用AppServiceHub服务中心;
公共MyCordaService(AppServiceHub serviceHub){
this.serviceHub=serviceHub;
//在服务创建/节点启动时运行的代码
}
//公共服务api
公共无效doSomething(){
//做点什么,开始一个新的流程
Thread flowThread=新线程(new StartFlow());
flowThread.start();
}
私有类StartFlow实现可运行{
@凌驾
公开募捐{
//启动新流程
CordaFuture CordaFuture=appServiceHub.startFlow(新)
流(params).getReturnValue();
SignedTransaction SignedTransaction=cordaFuture.get();
}
}
}