【问题标题】:In Corda, how to start a flow from a service?在 Corda 中,如何从服务启动流?
【发布时间】:2019-12-28 03:11:10
【问题描述】:

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

服务是否有任何流程可以启动流程?我该怎么做?

【问题讨论】:

    标签: corda


    【解决方案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>
    }
    

    【讨论】:

      【解决方案2】:

      是的,将 AppServiceHub 传递给构造函数

      在 Kotlin 中:

      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();
              }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-01
        • 2018-07-16
        • 2019-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 2011-05-09
        相关资源
        最近更新 更多