net.corda.core.flows

## Class FlowLogic<T>

- net.corda.core.flows.FlowLogic<T>

- Direct Known Subclasses:
    AbstractCashFlow, AbstractStateReplacementFlow.Acceptor, BroadcastTransactionFlow, TwoPartyTradeFlow.Buyer, NotaryFlow.Client, FetchDataFlow, FinalityFlow, AbstractStateReplacementFlow.Instigator, IssuerFlow.IssuanceRequester, IssuerFlow.Issuer, Service.NotifyTransactionHandler, TwoPartyDealFlow.Primary, ResolveTransactionsFlow, NodeSchedulerService.RunScheduled, TwoPartyDealFlow.Secondary, TwoPartyTradeFlow.Seller, NotaryFlow.Service

      public class FlowLogic<T>

  A sub-class of `class FlowLogic` implements a flow using direct, straight line blocking code. Thus you can write complex flow logic in an ordinary fashion, without having to think about callbacks, restarting after a node crash, how many instances of your flow there are running and so on.

  Invoking the network will cause the call stack to be suspended onto the heap and then serialized to a database using the Quasar fibers framework. Because of this, if you need access to data that might change over time, you should request it just-in-time via the serviceHub property which is provided. Don't try and keep data you got from a service across calls to send/receive/sendAndReceive because the world might change in arbitrary ways out from underneath you, for instance, if the node is restarted or reconfigured!

  Additionally, be aware of what data you pin either via the stack or in your `class FlowLogic` implementation. Very large objects or datasets will hurt performance by increasing the amount of data stored in each checkpoint.

  If you'd like to use another FlowLogic class as a component of your own, construct it on the fly and then pass it to the subFlow method. It will return the result of that flow when it completes.

  See Also:
    `class FlowLogic`, `class FlowLogic`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FlowLogic()` A sub-class of `class FlowLogic` implements a flow using direct, straight line blocking code. Thus you can write complex flow logic in an ordinary fashion, without having to think about callbacks, restarting after a node crash, how many instances of your flow there are running and so on.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `T` | `call()` This is where you fill out your business logic. The returned object will usually be ignored, but can be helpful if this flow is meant to be used as a subflow.
    `java.lang.Class<?>` | `getCounterpartyMarker(Party party)` Return the marker Class which party has used to register the counterparty flow that is to execute on the other side. The default implementation returns the class object of this FlowLogic, but any Class instance will do as long as the other side registers with it.
    `org.slf4j.Logger` | `getLogger()` This is where you should log things to.
    `ProgressTracker` | `getProgressTracker()` Override this to provide a `class ProgressTracker`. If one is provided and stepped, the framework will do something helpful with the progress reports. If this flow is invoked as a subflow of another, then the tracker will be made a child of the current step in the parent. If it's null, this flow doesn't track progress.
    `StateMachineRunId` | `getRunId()` Returns a wrapped UUID object that identifies this state machine run (i.e. subflows have the same identifier as their parents).
    `ServiceHub` | `getServiceHub()` Provides access to big, heavy classes that may be reconstructed from time to time, e.g. across restarts. It is only available once the flow has started, which means it cannnot be accessed in the constructor. Either access this lazily or from inside `FlowLogic.call`.
    `FlowStateMachine<?>` | `getStateMachine()` Internal only. Reference to the Fiber instance that is the top level controller for the entire flow. When inside a flow this is equivalent to Strand.currentStrand. This is public only because it must be accessed across module boundaries.
    `<R> UntrustworthyData<R>` | `receive(java.lang.Class<R> receiveType, Party otherParty)` Suspends until the specified otherParty sends us a message of type receiveType.
    `void` | `send(Party otherParty, java.lang.Object payload)` Queues the given payload for sending to the otherParty and continues without suspending.
    `<R> UntrustworthyData<R>` | `sendAndReceive(java.lang.Class<R> receiveType, Party otherParty, java.lang.Object payload)` Serializes and queues the given payload object for sending to the otherParty. Suspends until a response is received, which must be of the given receiveType. Remember that when receiving data from other parties the data should not be trusted until it's been thoroughly verified for consistency and that all expectations are satisfied, as a malicious peer may send you subtly corrupted data in order to exploit your code.
    `void` | `setStateMachine(FlowStateMachine<?> value)` Internal only. Reference to the Fiber instance that is the top level controller for the entire flow. When inside a flow this is equivalent to Strand.currentStrand. This is public only because it must be accessed across module boundaries.
    `<R> R` | `subFlow(FlowLogic<? extends R> subLogic, boolean shareParentSessions)` Invokes the given subflow. This function returns once the subflow completes successfully with the result returned by that subflows `FlowLogic.call` method. If the subflow has a progress tracker, it is attached to the current step in this flow's progress tracker.
    `<R> R` | `subFlow(FlowLogic<? extends R> subLogic)` Invokes the given subflow. This function returns once the subflow completes successfully with the result returned by that subflows `FlowLogic.call` method. If the subflow has a progress tracker, it is attached to the current step in this flow's progress tracker.
    `kotlin.Pair<java.lang.String,rx.Observable>` | `track()` Returns a pair of the current progress step, as a string, and an observable of stringified changes to the progressTracker.
    `SignedTransaction` | `waitForLedgerCommit(SecureHash hash)` Suspends the flow until the transaction with the specified ID is received, successfully verified and sent to the vault for processing. Note that this call suspends until the transaction is considered valid by the local node, but that doesn't imply the vault will consider it relevant.

- - ### Constructor Detail

    - #### FlowLogic

          public FlowLogic()

      A sub-class of `class FlowLogic` implements a flow using direct, straight line blocking code. Thus you can write complex flow logic in an ordinary fashion, without having to think about callbacks, restarting after a node crash, how many instances of your flow there are running and so on.

      Invoking the network will cause the call stack to be suspended onto the heap and then serialized to a database using the Quasar fibers framework. Because of this, if you need access to data that might change over time, you should request it just-in-time via the serviceHub property which is provided. Don't try and keep data you got from a service across calls to send/receive/sendAndReceive because the world might change in arbitrary ways out from underneath you, for instance, if the node is restarted or reconfigured!

      Additionally, be aware of what data you pin either via the stack or in your `class FlowLogic` implementation. Very large objects or datasets will hurt performance by increasing the amount of data stored in each checkpoint.

      If you'd like to use another FlowLogic class as a component of your own, construct it on the fly and then pass it to the subFlow method. It will return the result of that flow when it completes.

      See Also:
        `class FlowLogic`, `class FlowLogic`

  - ### Method Detail

    - #### getLogger

          public org.slf4j.Logger getLogger()

      This is where you should log things to.

    - #### getRunId

          public StateMachineRunId getRunId()

      Returns a wrapped UUID object that identifies this state machine run (i.e. subflows have the same identifier as their parents).

      See Also:
        UUID

    - #### getServiceHub

          public ServiceHub getServiceHub()

      Provides access to big, heavy classes that may be reconstructed from time to time, e.g. across restarts. It is only available once the flow has started, which means it cannnot be accessed in the constructor. Either access this lazily or from inside `FlowLogic.call`.

      See Also:
        `FlowLogic.call`

    - #### getCounterpartyMarker

          public java.lang.Class<?> getCounterpartyMarker(Party party)

      Return the marker Class which party has used to register the counterparty flow that is to execute on the other side. The default implementation returns the class object of this FlowLogic, but any Class instance will do as long as the other side registers with it.

      See Also:
        Class, Class

    - #### sendAndReceive

          public <R> UntrustworthyData<R> sendAndReceive(java.lang.Class<R> receiveType,
                                                         Party otherParty,
                                                         java.lang.Object payload)

      Serializes and queues the given payload object for sending to the otherParty. Suspends until a response is received, which must be of the given receiveType. Remember that when receiving data from other parties the data should not be trusted until it's been thoroughly verified for consistency and that all expectations are satisfied, as a malicious peer may send you subtly corrupted data in order to exploit your code.

      Note that this function is not just a simple send+receive pair: it is more efficient and more correct to use this when you expect to do a message swap than do use send and then receive in turn.

    - #### receive

          public <R> UntrustworthyData<R> receive(java.lang.Class<R> receiveType,
                                                  Party otherParty)

      Suspends until the specified otherParty sends us a message of type receiveType.

      Remember that when receiving data from other parties the data should not be trusted until it's been thoroughly verified for consistency and that all expectations are satisfied, as a malicious peer may send you subtly corrupted data in order to exploit your code.

    - #### send

          public void send(Party otherParty,
                           java.lang.Object payload)

      Queues the given payload for sending to the otherParty and continues without suspending.

      Note that the other party may receive the message at some arbitrary later point or not at all: if otherParty is offline then message delivery will be retried until it comes back or until the message is older than the network's event horizon time.

    - #### subFlow

          public <R> R subFlow(FlowLogic<? extends R> subLogic,
                               boolean shareParentSessions)

      Invokes the given subflow. This function returns once the subflow completes successfully with the result returned by that subflows `FlowLogic.call` method. If the subflow has a progress tracker, it is attached to the current step in this flow's progress tracker.

      Parameters:
        `shareParentSessions` - In certain situations the need arises to use the same sessions the parent flow has already established. However this also prevents the subflow from creating new sessions with those parties. For this reason the default value is false.
      Throws:
        - This is either thrown by subLogic itself or propagated from any of the remote `class FlowLogic`s it communicated with. A subflow retry can be done by catching this exception.
      See Also:
        `FlowLogic.call`

    - #### subFlow

          public <R> R subFlow(FlowLogic<? extends R> subLogic)

      Invokes the given subflow. This function returns once the subflow completes successfully with the result returned by that subflows `FlowLogic.call` method. If the subflow has a progress tracker, it is attached to the current step in this flow's progress tracker.

      Throws:
        - This is either thrown by subLogic itself or propagated from any of the remote `class FlowLogic`s it communicated with. A subflow retry can be done by catching this exception.
      See Also:
        `FlowLogic.call`

    - #### getProgressTracker

          public ProgressTracker getProgressTracker()

      Override this to provide a `class ProgressTracker`. If one is provided and stepped, the framework will do something helpful with the progress reports. If this flow is invoked as a subflow of another, then the tracker will be made a child of the current step in the parent. If it's null, this flow doesn't track progress.

      Note that this has to return a tracker before the flow is invoked. You can't change your mind half way through.

      See Also:
        `class ProgressTracker`

    - #### call

          public T call()

      This is where you fill out your business logic. The returned object will usually be ignored, but can be helpful if this flow is meant to be used as a subflow.

    - #### track

          public kotlin.Pair<java.lang.String,rx.Observable> track()

      Returns a pair of the current progress step, as a string, and an observable of stringified changes to the progressTracker.

      Returns:
        Returns null if this flow has no progress tracker.

    - #### waitForLedgerCommit

          public SignedTransaction waitForLedgerCommit(SecureHash hash)

      Suspends the flow until the transaction with the specified ID is received, successfully verified and sent to the vault for processing. Note that this call suspends until the transaction is considered valid by the local node, but that doesn't imply the vault will consider it relevant.

    - #### getStateMachine

          public FlowStateMachine<?> getStateMachine()

      Internal only. Reference to the Fiber instance that is the top level controller for the entire flow. When inside a flow this is equivalent to Strand.currentStrand. This is public only because it must be accessed across module boundaries.

      See Also:
        Fiber, Strand.currentStrand

    - #### setStateMachine

          public void setStateMachine(FlowStateMachine<?> value)

      Internal only. Reference to the Fiber instance that is the top level controller for the entire flow. When inside a flow this is equivalent to Strand.currentStrand. This is public only because it must be accessed across module boundaries.

      See Also:
        Fiber, Strand.currentStrand