|
18 | 18 | - [Handle Car Auction Bids (Scheduled start Event state)](#Handle-Car-Auction-Bids-Example)
|
19 | 19 | - [Check Inbox Periodically (Cron-based Workflow start)](#Check-Inbox-Periodically)
|
20 | 20 | - [Event-based service invocation (Event triggered actions)](#Event-Based-Service-Invocation)
|
| 21 | +- [Reusing Function and Event Definitions](#Reusing-Function-And-Event-Definitions) |
21 | 22 |
|
22 | 23 | ### Hello World Example
|
23 | 24 |
|
@@ -2597,3 +2598,242 @@ states:
|
2597 | 2598 | </td>
|
2598 | 2599 | </tr>
|
2599 | 2600 | </table>
|
| 2601 | + |
| 2602 | +### Reusing Function And Event Definitions |
| 2603 | + |
| 2604 | +#### Description |
| 2605 | + |
| 2606 | +This example shows how [function](../specification.md#Function-Definition) and [event](../specification.md#Event-Definition) definitions |
| 2607 | +can be declared independently and referenced by workflow definitions. |
| 2608 | +This is useful when you would like to reuse event and function definitions across multiple workflows. In those scenarios it allows you to make |
| 2609 | +changed/updates to these definitions in a single place without having to modify multiple workflows. |
| 2610 | + |
| 2611 | +For the example we have two files, namely our "functiondefs.json" and "eventdefs.yml" (to show that they can be expressed in either JSON or YAML). |
| 2612 | +These hold our function and event definitions which then can be referenced by multiple workflows. |
| 2613 | + |
| 2614 | +* functiondefs.json |
| 2615 | +```json |
| 2616 | +{ |
| 2617 | + "functions": [ |
| 2618 | + { |
| 2619 | + "name": "checkFundsAvailability", |
| 2620 | + "resource": "accountFundsResource" |
| 2621 | + }, |
| 2622 | + { |
| 2623 | + "name": "sendSuccessEmail", |
| 2624 | + "resource": "emailServiceResource" |
| 2625 | + }, |
| 2626 | + { |
| 2627 | + "name": "sendInsufficientFundsEmail", |
| 2628 | + "resource": "emailServiceResource" |
| 2629 | + } |
| 2630 | + ] |
| 2631 | +} |
| 2632 | +``` |
| 2633 | + |
| 2634 | +* eventdefs.yml |
| 2635 | +```yaml |
| 2636 | +events: |
| 2637 | +- name: PaymentReceivedEvent |
| 2638 | + type: payment.receive |
| 2639 | + source: paymentEventSource |
| 2640 | + correlation: |
| 2641 | + - contextAttributeName: accountId |
| 2642 | +- name: ConfirmationCompletedEvent |
| 2643 | + type: payment.confirmation |
| 2644 | + kind: produced |
| 2645 | +
|
| 2646 | +``` |
| 2647 | + |
| 2648 | +In our workflow definition then we can reference these files rather than defining function and events in-line. |
| 2649 | + |
| 2650 | +#### Workflow Diagram |
| 2651 | + |
| 2652 | +<p align="center"> |
| 2653 | +<img src="../media/examples/example-reusefunceventdefs.png" height="400px" alt="Reusing Function and Event Definitions Example"/> |
| 2654 | +</p> |
| 2655 | + |
| 2656 | +#### Workflow Definitions |
| 2657 | + |
| 2658 | +<table> |
| 2659 | +<tr> |
| 2660 | + <th>JSON</th> |
| 2661 | + <th>YAML</th> |
| 2662 | +</tr> |
| 2663 | +<tr> |
| 2664 | +<td valign="top"> |
| 2665 | + |
| 2666 | +```json |
| 2667 | +{ |
| 2668 | + "id": "paymentconfirmation", |
| 2669 | + "version": "1.0", |
| 2670 | + "name": "Payment Confirmation Workflow", |
| 2671 | + "description": "Performs Payment Confirmation", |
| 2672 | + "functions": "functiondefs.json", |
| 2673 | + "events": "eventdefs.yml", |
| 2674 | + "states": [ |
| 2675 | + { |
| 2676 | + "name": "PaymentReceived", |
| 2677 | + "type": "event", |
| 2678 | + "onEvents": [ |
| 2679 | + { |
| 2680 | + "eventRefs": [ |
| 2681 | + "PaymentReceivedEvent" |
| 2682 | + ], |
| 2683 | + "actions": [ |
| 2684 | + { |
| 2685 | + "name": "checkfunds", |
| 2686 | + "functionRef": { |
| 2687 | + "refName": "checkFundsAvailability", |
| 2688 | + "parameters": { |
| 2689 | + "account": "{{ $.accountId }}", |
| 2690 | + "paymentamount": "{{ $.payment.amount }}" |
| 2691 | + } |
| 2692 | + } |
| 2693 | + } |
| 2694 | + ] |
| 2695 | + } |
| 2696 | + ], |
| 2697 | + "transition": { |
| 2698 | + "nextState": "ConfirmBasedOnFunds" |
| 2699 | + } |
| 2700 | + }, |
| 2701 | + { |
| 2702 | + "name": "ConfirmBasedOnFunds", |
| 2703 | + "type": "switch", |
| 2704 | + "dataConditions": [ |
| 2705 | + { |
| 2706 | + "condition": "{{ $.funds[?(@.available == 'true')] }}", |
| 2707 | + "transition": { |
| 2708 | + "nextState": "SendPaymentSuccess" |
| 2709 | + } |
| 2710 | + }, |
| 2711 | + { |
| 2712 | + "condition": "{{ $.funds[?(@.available == 'false')] }}", |
| 2713 | + "transition": { |
| 2714 | + "nextState": "SendInsufficientResults" |
| 2715 | + } |
| 2716 | + } |
| 2717 | + ], |
| 2718 | + "default": { |
| 2719 | + "transition": { |
| 2720 | + "nextState": "SendPaymentSuccess" |
| 2721 | + } |
| 2722 | + } |
| 2723 | + }, |
| 2724 | + { |
| 2725 | + "name": "SendPaymentSuccess", |
| 2726 | + "type": "operation", |
| 2727 | + "actions": [ |
| 2728 | + { |
| 2729 | + "functionRef": { |
| 2730 | + "refName": "sendSuccessEmail", |
| 2731 | + "parameters": { |
| 2732 | + "applicant": "{{ $.customer }}" |
| 2733 | + } |
| 2734 | + } |
| 2735 | + } |
| 2736 | + ], |
| 2737 | + "end": { |
| 2738 | + "kind": "event", |
| 2739 | + "produceEvents": [ |
| 2740 | + { |
| 2741 | + "eventRef": "ConfirmationCompletedEvent", |
| 2742 | + "data": "{{ $.payment }}" |
| 2743 | + } |
| 2744 | + ] |
| 2745 | + } |
| 2746 | + }, |
| 2747 | + { |
| 2748 | + "name": "SendInsufficientResults", |
| 2749 | + "type": "operation", |
| 2750 | + "actions": [ |
| 2751 | + { |
| 2752 | + "functionRef": { |
| 2753 | + "refName": "sendInsufficientFundsEmail", |
| 2754 | + "parameters": { |
| 2755 | + "applicant": "{{ $.customer }}" |
| 2756 | + } |
| 2757 | + } |
| 2758 | + } |
| 2759 | + ], |
| 2760 | + "end": { |
| 2761 | + "kind": "event", |
| 2762 | + "produceEvents": [ |
| 2763 | + { |
| 2764 | + "eventRef": "ConfirmationCompletedEvent", |
| 2765 | + "data": "{{ $.payment }}" |
| 2766 | + } |
| 2767 | + ] |
| 2768 | + } |
| 2769 | + } |
| 2770 | + ] |
| 2771 | +} |
| 2772 | +``` |
| 2773 | + |
| 2774 | +</td> |
| 2775 | +<td valign="top"> |
| 2776 | + |
| 2777 | +```yaml |
| 2778 | +id: paymentconfirmation |
| 2779 | +version: '1.0' |
| 2780 | +name: Payment Confirmation Workflow |
| 2781 | +description: Performs Payment Confirmation |
| 2782 | +functions: functiondefs.json |
| 2783 | +events: eventdefs.yml |
| 2784 | +states: |
| 2785 | +- name: PaymentReceived |
| 2786 | + type: event |
| 2787 | + onEvents: |
| 2788 | + - eventRefs: |
| 2789 | + - PaymentReceivedEvent |
| 2790 | + actions: |
| 2791 | + - name: checkfunds |
| 2792 | + functionRef: |
| 2793 | + refName: checkFundsAvailability |
| 2794 | + parameters: |
| 2795 | + account: "{{ $.accountId }}" |
| 2796 | + paymentamount: "{{ $.payment.amount }}" |
| 2797 | + transition: |
| 2798 | + nextState: ConfirmBasedOnFunds |
| 2799 | +- name: ConfirmBasedOnFunds |
| 2800 | + type: switch |
| 2801 | + dataConditions: |
| 2802 | + - condition: "{{ $.funds[?(@.available == 'true')] }}" |
| 2803 | + transition: |
| 2804 | + nextState: SendPaymentSuccess |
| 2805 | + - condition: "{{ $.funds[?(@.available == 'false')] }}" |
| 2806 | + transition: |
| 2807 | + nextState: SendInsufficientResults |
| 2808 | + default: |
| 2809 | + transition: |
| 2810 | + nextState: SendPaymentSuccess |
| 2811 | +- name: SendPaymentSuccess |
| 2812 | + type: operation |
| 2813 | + actions: |
| 2814 | + - functionRef: |
| 2815 | + refName: sendSuccessEmail |
| 2816 | + parameters: |
| 2817 | + applicant: "{{ $.customer }}" |
| 2818 | + end: |
| 2819 | + kind: event |
| 2820 | + produceEvents: |
| 2821 | + - eventRef: ConfirmationCompletedEvent |
| 2822 | + data: "{{ $.payment }}" |
| 2823 | +- name: SendInsufficientResults |
| 2824 | + type: operation |
| 2825 | + actions: |
| 2826 | + - functionRef: |
| 2827 | + refName: sendInsufficientFundsEmail |
| 2828 | + parameters: |
| 2829 | + applicant: "{{ $.customer }}" |
| 2830 | + end: |
| 2831 | + kind: event |
| 2832 | + produceEvents: |
| 2833 | + - eventRef: ConfirmationCompletedEvent |
| 2834 | + data: "{{ $.payment }}" |
| 2835 | +``` |
| 2836 | + |
| 2837 | +</td> |
| 2838 | +</tr> |
| 2839 | +</table> |
0 commit comments