我已经在我的节点上创建了CordaService
。我希望这项服务能够根据各种条件启动流程。但是,提供给服务的ServiceHub
不提供启动流的功能。
服务启动流程是否有任何流程?我该怎么办?
答案 0 :(得分:1)
是。只需在其构造函数中传递CordaService
AppServiceHub
而不是ServiceHub
。
AppServiceHub
接口扩展了ServiceHub
接口,使节点能够启动流:
interface AppServiceHub : ServiceHub {
fun <T> startFlow(flow: FlowLogic<T>): FlowHandle<T>
fun <T> startTrackedFlow(flow: FlowLogic<T>): FlowProgressHandle<T>
}
答案 1 :(得分:0)
是的,将AppServiceHub传递给构造函数
在科特林:
class MyCordaService(private val serviceHub: AppServiceHub) : SingletonSerializeAsToken() {
init {
// code ran at service creation / node startup
}
// public api of service
}
或Java:
public class MyCordaService extends SingletonSerializeAsToken {
private AppServiceHub serviceHub;
public MyCordaService(AppServiceHub serviceHub) {
this.serviceHub = serviceHub;
// code ran at service creation / node startup
}
// public api of service
}
重要:为避免运行的节点之间可能出现任何死锁,请从其自己的 线程
开始内部流程例如:
public class MyCordaService extends SingletonSerializeAsToken {
private AppServiceHub serviceHub;
public MyCordaService(AppServiceHub serviceHub) {
this.serviceHub = serviceHub;
// code ran at service creation / node startup
}
// public api of service
public void doSomething(){
// do something and start a new flow
Thread flowThread = new Thread(new StartFlow());
flowThread.start();
}
private class StartFlow implements Runnable {
@Override
public void run() {
// start new flow
CordaFuture<SignedTransaction> cordaFuture= appServiceHub.startFlow(new
Flow(params).getReturnValue();
SignedTransaction signedTransaction = cordaFuture.get();
}
}
}