Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1596] Adding Business Calendar Example #2036

Merged
merged 26 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f722c11
business-calendar-example
Abhitocode Oct 31, 2024
9902fcb
business-calendar-example
Abhitocode Oct 31, 2024
9885344
business-calendar-example
Abhitocode Oct 31, 2024
79afc8e
business-calendar-example
Abhitocode Nov 1, 2024
d7f848d
business-calendar-example
Abhitocode Nov 4, 2024
8776bd4
business-calendar-example
Abhitocode Nov 5, 2024
66cbd6d
incubator-kie-issues-1596-bc-example
Abhitocode Nov 5, 2024
251f233
Merge remote-tracking branch 'origin/main' into incubator-kie-issues-…
Abhitocode Nov 5, 2024
7229d4e
incubator-kie-issues-1596-bc-example
Abhitocode Nov 6, 2024
a18aa1b
incubator-kie-issues-1596-bc-example
Abhitocode Nov 6, 2024
5db4a4b
Merge remote-tracking branch 'Abhitocode/incubator-kie-issues#1596' i…
Abhitocode Nov 6, 2024
0114f07
incubator-kie-issues-1596-bc-example
Abhitocode Nov 6, 2024
6b5eb73
incubator-kie-issues-1596-bc-example
Abhitocode Nov 7, 2024
ee15459
incubator-kie-issues-1596-bc-example
Abhitocode Nov 11, 2024
afe0e81
incubator-kie-issues-1596-bc-example
Abhitocode Nov 11, 2024
0d27c4e
incubator-kie-issues-1596-bc-example
Abhitocode Nov 11, 2024
5800d67
incubator-kie-issues-1596-bc-example
Abhitocode Nov 11, 2024
13f610f
incubator-kie-issues-1596-bc-example
Abhitocode Nov 12, 2024
315ecbb
incubator-kie-issues-1596-bc-example
Abhitocode Nov 12, 2024
68e75ba
incubator-kie-issues-1596-bc-example
Abhitocode Nov 13, 2024
0e1f42f
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
df97315
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
5269084
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
69e2990
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
9483dee
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
a8c402f
incubator-kie-issues-1596-bc-example
Abhitocode Nov 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kogito-quarkus-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>process-business-calendar-example</module>
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved
<module>decisiontable-quarkus-example</module>
<module>dmn-15-quarkus-example</module>
<module>dmn-drools-quarkus-metrics</module>
Expand Down
263 changes: 263 additions & 0 deletions kogito-quarkus-examples/process-business-calendar-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
# Process Business Calendar Example

This project is an illustrative example demonstrating the impact of a business calendar on process execution within a Quarkus application . It showcases a business process involving credit card bill processing, which adapts to a business calendar defined in `calendar.properties`. This configuration modifies timer behaviors to respect working hours, holidays, and other schedule-based constraints.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

### Main Components

**BPMN2-BusinessCalendarBankTransaction.bpmn2**:
Defines the workflow for processing credit card transactions.
Includes tasks such as processing the credit bill, verifying payment, handling timers, cancelling and bill settlement.

**CreditCardService.java**:
Implements the logic for handling credit card payment processes.

**calendar.properties**:
Configures business hours, holidays, and other calendar properties that affect scheduling and timer behavior.

### BPMN Process Details

The BPMN model (`BPMN2-BusinessCalendarBankTransaction.bpmn2`) defines a workflow that includes the following main elements:
<p align="center"><img width=75% height=50% src="docs/images/CreditCardModel.png"></p>

### Start Event

The initial trigger that starts the credit card bill processing workflow.

### Process Credit Bill
* Process Credit Bill Properties (Top)
<p align="center"><img width=75% height=50% src="docs/images/ProcessCreditBillTop.png"></p>

* Process Credit Card Bill Assignments
<p align="center"><img width=75% height=50% src="docs/images/ProcessCreditBillAssign.png"></p>

### Verify Payment
A service task where the credit card details are validated, ensuring the payment is processed under valid terms.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

* Verify Payment
<p align="center"><img width=75% height=50% src="docs/images/VerifyPayment.png"></p>

### Timer

Attached to a human task to simulate waiting for manual confirmation or user action. This timer can be configured to react differently based on the presence of the business calendar.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved
<p align="center"><img width=75% height=50% src="docs/images/Timer.png"></p>

### Cancel Payment
Executed if the timer expires without human action, leading to the cancellation of the payment process.

* Cancel Payment (Top)
<p align="center"><img width=75% height=50% src="docs/images/CancelPaymentTop.png"></p>

* Cancel Payment Assignments
<p align="center"><img width=75% height=50% src="docs/images/CancelPaymentAssign.png"></p>

### Settle Payment

The final step where the payment is settled successfully on manual verification.

* Settle Payment (Top)
<p align="center"><img width=75% height=50% src="docs/images/SettlePaymentTop.png"></p>

* Settle Payment Assignments
<p align="center"><img width=75% height=50% src="docs/images/SettlePaymentAssign.png"></p>

## Build and run

### Prerequisites

You will need:
- Java 17+ installed
- Environment variable JAVA_HOME set accordingly
- Maven 3.9.6+ installed

When using native image compilation, you will also need:
- GraalVM 19.3+ installed
- Environment variable GRAALVM_HOME set accordingly
- GraalVM native image needs as well native-image extension: https://www.graalvm.org/reference-manual/native-image/
- Note that GraalVM native image compilation typically requires other packages (glibc-devel, zlib-devel and gcc) to be installed too, please refer to GraalVM installation documentation for more details.

### Compile and Run in Local Dev Mode

```sh
mvn clean compile quarkus:dev
```

NOTE: With dev mode of Quarkus you can take advantage of hot reload for business assets like processes, rules, decision tables and java code. No need to redeploy or restart your running application.

### Package and Run in JVM mode

```sh
mvn clean package
java -jar target/quarkus-app/quarkus-run.jar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this somehow does not work on my machine:
Screenshot 2024-11-13 110210
Screenshot 2024-11-13 110330

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried again, works for, not sure exactly what might be causing the issue in your case
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enabled it in the application.properties, should work now.

```

or on windows

```sh
mvn clean package
java -jar target\quarkus-app\quarkus-run.jar
```

### Package and Run using Local Native Image
Note that the following configuration property needs to be added to `application.properties` in order to enable automatic registration of `META-INF/services` entries required by the workflow engine:
```
quarkus.native.auto-service-loader-registration=true
```

Note that this requires GRAALVM_HOME to point to a valid GraalVM installation

```sh
mvn clean package -Pnative
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to finish this build:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  05:16 min
[INFO] Finished at: 2024-11-13T11:09:29+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:3.8.6:build (default) on project process-business-calendar-example: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[ERROR]         [error]: Build step io.quarkus.deployment.pkg.steps.NativeImageBuildStep#build threw an exception: io.quarkus.deployment.pkg.steps.NativeImageBuildStep$ImageGenerationFailureException: Image generation failed. Exit code: 1
[ERROR]         at io.quarkus.deployment.pkg.steps.NativeImageBuildStep.imageGenerationFailed(NativeImageBuildStep.java:468)
[ERROR]         at io.quarkus.deployment.pkg.steps.NativeImageBuildStep.build(NativeImageBuildStep.java:258)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
[ERROR]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:568)
[ERROR]         at io.quarkus.deployment.ExtensionLoader$3.execute(ExtensionLoader.java:849)
[ERROR]         at io.quarkus.builder.BuildContext.run(BuildContext.java:256)
[ERROR]         at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
[ERROR]         at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
[ERROR]         at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
[ERROR]         at java.base/java.lang.Thread.run(Thread.java:840)
[ERROR]         at org.jboss.threads.JBossThread.run(JBossThread.java:501)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

```

To run the generated native executable, generated in `target/`, execute

```sh
./target/process-usertasks-quarkus-runner
```

### OpenAPI (Swagger) documentation
[Specification at swagger.io](https://swagger.io/docs/specification/about/)

You can take a look at the [OpenAPI definition](http://localhost:8080/openapi?format=json) - automatically generated and included in this service - to determine all available operations exposed by this service. For easy readability you can visualize the OpenAPI definition file using a UI tool like for example available [Swagger UI](https://editor.swagger.io).

In addition, various clients to interact with this service can be easily generated using this OpenAPI definition.

When running in either Quarkus Development or Native mode, we also leverage the [Quarkus OpenAPI extension](https://quarkus.io/guides/openapi-swaggerui#use-swagger-ui-for-development) that exposes [Swagger UI](http://localhost:8080/swagger-ui/) that you can use to look at available REST endpoints and send test requests.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

##curl command can be found below:

### To start the process

```sh
curl -X POST http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-d '{"creditCardNumber": null, "creditCardDetails": {"cardNumber": "434353433", "status": "Bill Due"}}'

```

### To retrieve instances

```sh
curl -X GET http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-H "Accept: application/json"

```

## Comparision of timer with and without calendar.properties
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

### Testing without calendar.properties
The timer for the Verify Payment task will follow a straightforward countdown based on real time. If the specified time elapses i.e., 1 second, it immediately moves to cancel payment task, regardless of the working hours or holidays.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

* Example test results without calendar.properties:


* POST/ BusinessCalendarCreditBill
```sh
curl -X POST http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-d '{"creditCardNumber": null, "creditCardDetails": {"cardNumber": "434353433", "status": "Bill Due"}}'

```
<p align="center"><img width=75% height=50% src="docs/images/Post1.png"></p>

* After 1 second when we send request for GET/ BusinessCalendarCreditBill again we get empty array representing the cancellation.
```sh
curl -X GET http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-H "Accept: application/json"

```
<p align="center"><img width=75% height=50% src="docs/images/Get1.png"></p>

### Example of logs representing the process from start to complete
<p align="center"><img width=75% height=50% src="docs/images/WithoutPropertiesLogs.png"></p>


* The workflow 'BusinessCalendarCreditBill' began at 08:11:58,353 with a unique identifier 130f2eab-ab2e-413d-958e-414d1b3b0dc7.

* At 08:11:58,364, the node 'Start' was triggered.

* The subsequent node 'Process Credit Bill' was activated at 08:11:58,368.

* The 'Verify Payment' node was triggered at 08:11:58,397.

* A human task was registered at 08:11:58,444.

* The workflow transitioned to an 'Active' state at 08:11:58,496.

* Importantly, the log at 08:11:59,406 indicates that the job e3646ed7-76bf-498c-aa72-7086f4b847c6 was started, emphasizing that the timer was triggered after 1 second as anticipated.

* The 'Cancel Payment' node triggered at 08:11:59,425. CreditCardService logged the bill cancellation.

* The final node 'End' was initiated at 08:11:59,430. The workflow completed successfully at 08:11:59,478.

* Hence, without calendar.properties, timer fires immediately after their configured interval, activating tasks without delay.

## Testing with calendar.properties (During non-working hours/Specified Holiday)
### Steps to create & configure calendar.properties with a holiday/non-working hours

* Create a calendar.properties file in the src/main/resources directory. This file activates the Business Calendar feature and essential configurations

* Configure calendar.properties: The calendar.properties file should define the business hours, holidays, and other relevant settings. Below is an example of the format:
```properties
business.end.hour=24
business.hours.per.day=24
business.start.hour=0
business.holiday.date.format=yyyy-MM-dd
business.holidays=2024-11-07
#define today's date as holiday for testing purposes
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved
business.days.per.week =7
#business.cal.timezone= system default timezone
```

* For repeated holidays in a year
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved
```properties
business.holiday.date.format=dd/MM
business.holidays=14/11,25/12,01/01
```

* Weekend's
```properties
business.weekend.days=7,1
```
* After calendar.properties file is added, build the example again "mvn clean compile quarkus:dev" or type 's' in the quarkus terminal and hit enter just to restart.

* POST/ BusinessCalendarCreditBill
```sh
curl -X POST http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-d '{"creditCardNumber": null, "creditCardDetails": {"cardNumber": "434353433", "status": "Bill Due"}}'
```
<p align="center"><img width=75% height=50% src="docs/images/Post2.png"></p>


* GET/ BusinessCalendarCreditBill
```sh
curl -X GET http://localhost:8080/BusinessCalendarCreditBill \
-H "Content-Type: application/json" \
-H "Accept: application/json"

```
* Now, even after 1 second, the process will be in Active State.

<p align="center"><img width=75% height=50% src="docs/images/Get2.png"></p>

### Example of logs representing the active state during non-working hours/specified holiday

<p align="center"><img width=75% height=50% src="docs/images/WithPropertiesLogs.png"></p>

* The node 'Start' for the process 'BusinessCalendarCreditBill', identified by 08ea5258-9d91-4f05-a8d8-184107c042ed, was triggered at 08:54:28,621.
Abhitocode marked this conversation as resolved.
Show resolved Hide resolved

* At 08:54:28,629, the 'Process Credit Bill' node was activated.

* At 08:54:28,653, verification step through the 'Verify Payment' node was started.

* Subsequently, a human task was registered at 08:54:28,773.

* The workflow transitioned to an 'Active' state at 08:54:28,808.

* Due to mentioned "business.holidays property" in calendar.properties, timer does not trigger and the state remains active.

* On next business day, timer will resume at the beginning of the next working hour/day, after the non-working hour/holiday has ended. The timer is set to fire after one second of active business time.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading