Sample application demonstrating how to run a monthly subscription using Orkes Netflix Conductor - using Java and Spring Boot 2
To run this example locally
- Clone the project to your local
- Update the application.properties with a
orkes.access.key
andorkes.access.secret
that can connect to your conductor instance atorkes.conductor.server.url
- For connecting to playground - refer to this article to get a key and secret
- For connecting locally - follow the instructions here (Install and Run Locally)
- From the root project folder, run
mvn spring-boot:run
This application has a controller - which is exposed in SubcriptionApiController.java
and has two API methods
/startSubscription
: When invoked, it will trigger a subscription workflow that handles the subscription workflow/cancelSubscription
: When invoked, it will call a webhook API into Conductor to signal that the subscription has ended
The business logic is as follows:
- Trigger a subscription workflow - that includes a trial period
- Send a welcome email
- Wait for the trial period
- Once trial period ends, it will start a billing process - that runs once every billing period
- At the end of the number of billing periods, the workflow ends and notifies the user
- At any point during the trial or billing user can cancel the subscription
- User will be notified before trial starts, when billing starts and when subscription is completed or canceled
We can define this workflow visually as follows:
Click here to access this definition on Conductor playground
Here we are using two parallel forks where
- In the first fork, we will handle the subscription flow
- In the second fork, we will wait for a signal that the user is canceling
Either of the forks can complete and will end the workflow. To pass a signal we can use a webhook.
The webhook is configured to expect a header called subscriptionflow
with value subscription-flow-header-unique-value
When this webhook is invoked, it will look for running workflows and match the parameter as defined in the
WAIT_FOR_WEBHOOK
task (docs)
Webhook invocation sample:
curl -H "Content-Type:application/json" -H "Accept:application/json" \
-H 'subscriptionflow: subscription-flow-header-unique-value' \
-X POST 'https://play.orkes.io/webhook/ba70ba33-1a19-449e-98c2-d4581fcd9aad' \
-d '{"event": {"userId" : "user-id-1"}}'
We can also invoke the same API from code - which is what we have done in WorkflowService.java