update documentation for version 2.2
parent
63cbddf7fe
commit
3ad299a699
Binary file not shown.
Before Width: | Height: | Size: 56 KiB |
Binary file not shown.
After Width: | Height: | Size: 157 KiB |
@ -1,224 +0,0 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Dynamic configuration
|
||||
header: Online shop tutorial
|
||||
prev_section: tutorials/rule-priority
|
||||
next_section: tutorials/scheduling-engine
|
||||
doc: true
|
||||
---
|
||||
|
||||
In this tutorial, we have an online shop application and we would like to implement the following requirements:
|
||||
|
||||
1. Whenever a new customer places an order with an amount greater than a defined threshold, send an alert about this suspect order to the monitoring team.
|
||||
2. Moreover, the order amount threshold should be configurable via JMX so we can change it at runtime.
|
||||
|
||||
In the first part of this tutorial, we will see how to use Easy Rules to implement the business rule described in requirement #1.
|
||||
|
||||
In the second part, we will add JMX capability to the business rule developed in part 1 to be able to change the order amount threshold at runtime (requirement #2).
|
||||
|
||||
## Part 1 : Implementing the business rule
|
||||
|
||||
In this application, orders and customers are represented by the _Order_ and _Customer_ classes:
|
||||
|
||||
```java
|
||||
class Order {
|
||||
private long orderId;
|
||||
private float amount;
|
||||
//getters and setters
|
||||
}
|
||||
```
|
||||
<br/>
|
||||
|
||||
```java
|
||||
class Customer {
|
||||
private long customerId;
|
||||
private boolean isNew;
|
||||
//getters and setters
|
||||
}
|
||||
```
|
||||
|
||||
First, let's implement the rule's logic by extending the `BasicRule` class:
|
||||
|
||||
```java
|
||||
public class SuspectOrderRule extends BasicRule {
|
||||
|
||||
private float suspectOrderAmountThreshold = 1000;
|
||||
|
||||
private Order order;
|
||||
|
||||
private Customer customer;
|
||||
|
||||
SuspectOrderRule(String name, String description) {
|
||||
super(name, description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate() {
|
||||
return order.getAmount() > suspectOrderAmountThreshold
|
||||
&& customer.isNew();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws Exception {
|
||||
System.out.printf("Alert : A new customer [id=%s] has placed an order [id=%s] with amount %f > %f\n",
|
||||
customer.getCustomerId(), order.getOrderId(), order.getAmount(), suspectOrderAmountThreshold);
|
||||
}
|
||||
|
||||
// getters and setters for customer and order fields
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
This rule operates on an order and a customer instances which represent the business data to operate on.
|
||||
|
||||
The `evaluate` method evaluates to true when the customer is new and the order amount is greater than the defined threshold.
|
||||
|
||||
The `execute` method simply writes to the console the specified alert (this could be sending an email or another action in a real use case).
|
||||
|
||||
Then, let's create a `JmxRulesEngine` and register the `SuspectOrderRule` rule:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Order order = new Order(6654, 1200);
|
||||
Customer customer = new Customer(2356, true);
|
||||
|
||||
/**
|
||||
* Create a business rule instance
|
||||
*/
|
||||
SuspectOrderRule suspectOrderRule = new SuspectOrderRule(
|
||||
"Suspect Order",
|
||||
"Send alert if a new customer places an order with amount greater than a threshold");
|
||||
|
||||
/**
|
||||
* Set data to operate on
|
||||
*/
|
||||
suspectOrderRule.setOrder(order);
|
||||
suspectOrderRule.setCustomer(customer);
|
||||
|
||||
/**
|
||||
* Create a rules engine and register the business rule
|
||||
*/
|
||||
JmxRulesEngine rulesEngine = aNewJmxRulesEngine().build();
|
||||
rulesEngine.registerJmxRule(suspectOrderRule);
|
||||
|
||||
/**
|
||||
* Fire rules
|
||||
*/
|
||||
rulesEngine.fireRules();
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To run this tutorial, you can follow these instructions from the root directory of Easy Rules :
|
||||
|
||||
{% highlight bash %}
|
||||
$ mvn install
|
||||
$ cd easyrules-samples
|
||||
$ mvn exec:java -P runOnlineShopTutorial
|
||||
{% endhighlight %}
|
||||
|
||||
When you run this tutorial, the rule named _Suspect Order_ that we registered will be applied since the order amount (1200) is greater than the threshold (1000) and the customer 2356 is a new customer.
|
||||
|
||||
## Part 2 : Changing the order amount threshold at runtime
|
||||
|
||||
In this tutorial, we need to expose the order amount threshold as a JMX attribute. So first, let's define an interface that allows us to change order amount threshold via JMX:
|
||||
|
||||
```java
|
||||
@javax.management.MXBean
|
||||
public interface SuspectOrderJmxRule extends JmxRule {
|
||||
|
||||
/**
|
||||
* Get the current suspect order amount threshold
|
||||
* @return current suspect order amount threshold
|
||||
*/
|
||||
float getSuspectOrderAmountThreshold();
|
||||
|
||||
/**
|
||||
* Set the suspect order amount threshold
|
||||
* @param suspectOrderAmountThreshold the new suspect order amount threshold
|
||||
*/
|
||||
void setSuspectOrderAmountThreshold(float suspectOrderAmountThreshold);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Then, we should make our `SuspectOrderRule` implement the `SuspectOrderJmxRule` interface to expose the order amount threshold as a JMX attribute.
|
||||
|
||||
So here is the new `SuspectOrderRule` class:
|
||||
|
||||
```java
|
||||
public class SuspectOrderRule extends BasicRule implements SuspectOrderJmxRule {
|
||||
|
||||
// same implementation of the rule
|
||||
|
||||
public float getSuspectOrderAmountThreshold() {
|
||||
return suspectOrderAmountThreshold;
|
||||
}
|
||||
|
||||
public void setSuspectOrderAmountThreshold(float suspectOrderAmountThreshold) {
|
||||
this.suspectOrderAmountThreshold = suspectOrderAmountThreshold;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Finally, let's suspend the program to change the order amount threshold value at runtime via any compliant JMX client and see the engine behavior after this change:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Order order = new Order(6654, 1200);
|
||||
Customer customer = new Customer(2356, true);
|
||||
|
||||
/**
|
||||
* Create a business rule instance
|
||||
*/
|
||||
SuspectOrderRule suspectOrderRule = new SuspectOrderRule(
|
||||
"Suspect Order",
|
||||
"Send alert if a new customer places an order with amount greater than a threshold");
|
||||
|
||||
/**
|
||||
* Set data to operate on
|
||||
*/
|
||||
suspectOrderRule.setOrder(order);
|
||||
suspectOrderRule.setCustomer(customer);
|
||||
|
||||
/**
|
||||
* Create a Jmx rules engine and register the business rule
|
||||
*/
|
||||
JmxRulesEngine rulesEngine = aNewJmxRulesEngine().build();
|
||||
rulesEngine.registerJmxRule(suspectOrderRule);
|
||||
|
||||
/**
|
||||
* Fire rules
|
||||
*/
|
||||
rulesEngine.fireRules();
|
||||
|
||||
// Update suspect order amount threshold via a JMX client.
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Change suspect order amount threshold to a value > 1200 via a JMX client and then press enter");
|
||||
scanner.nextLine();
|
||||
|
||||
System.out.println("**************************************************************");
|
||||
System.out.println("Re fire rules after updating suspect order amount threshold...");
|
||||
System.out.println("**************************************************************");
|
||||
|
||||
rulesEngine.fireRules();
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In the next screenshot, we use VisualVM to change the threshold value:
|
||||
|
||||
<img style="width: 100%" src="{{site.url}}/img/jmx-suspectOrderRule.png">
|
||||
|
||||
If you change the threshold value to 1400 for example, you will see that the rule _Suspect Order_ will not be applied the second time since the order amount (1200) is no longer greater than the new threshold (1400).
|
||||
|
||||
That's all! In this tutorial, we have seen how to create a real business rule with Easy Rules and how to reconfigure it at runtime.
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Hello World
|
||||
title: Hello world tutorial
|
||||
header: Hello World tutorial
|
||||
prev_section: user-guide/managing-rules
|
||||
next_section: tutorials/rule-priority
|
||||
prev_section: user-guide/embedding-rules-engine
|
||||
next_section: tutorials/shop-tutorial
|
||||
doc: true
|
||||
---
|
||||
|
||||
This tutorial shows how to use Easy Rules in a very simple application.
|
||||
The program should ask the user if he is a friend of duke and says 'Hello duke's friend!' only if he replies 'yes'.
|
||||
The goal is to ask the user if he is a friend of duke and says 'Hello duke's friend!' only if he replies 'yes'.
|
||||
|
||||
Based on this requirement, the rule is pretty straightforward :
|
||||
|
@ -1,160 +0,0 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Rule Priority
|
||||
header: Rule Priority tutorial
|
||||
prev_section: tutorials/hello-world
|
||||
next_section: tutorials/dynamic-configuration
|
||||
doc: true
|
||||
---
|
||||
|
||||
This tutorial shows how to define priority in which rules must be fired.
|
||||
In Easy Rules, every rule has a priority. Rules are fired by default according to their priorities (this can be changed as described in the [user guide]({{site.url}}/user-guide/defining-rules.html#rules-priorities)).
|
||||
|
||||
In this tutorial, we have an application that sells alcohol. The application must flag the customer as adult if his age is greater than 18,
|
||||
and must deny children from buying alcohol. Customers are represented by the following `Person` class:
|
||||
|
||||
```java
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private boolean adult;
|
||||
|
||||
//getters and setters omitted
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Based on these requirements, we can define the following rules:
|
||||
|
||||
* Rule 1: should operate an a `Person` instance, check that the person age is greater than 18 and set the adult flag.
|
||||
* Rule 2: should operate an a `Person` instance, check that the person is adult and deny children (ie, non adult) from buying alcohol.
|
||||
|
||||
Rule 1 should be fired **_before_** rule 2. We will set rule 1 priority to 1 and rule 2 priority to 2 so that Easy Rules engine fire them in this order.
|
||||
|
||||
First, let's create a class for rule 1:
|
||||
|
||||
```java
|
||||
@Rule(name = "AgeRule",
|
||||
description = "Check if person's age is > 18
|
||||
and marks the person as adult")
|
||||
public class AgeRule {
|
||||
|
||||
private Person person;
|
||||
|
||||
private int adultAge = 18;
|
||||
|
||||
public AgeRule(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
@Condition
|
||||
public boolean isAdult() {
|
||||
return person.getAge() > adultAge;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void markAsAdult(){
|
||||
person.setAdult(true);
|
||||
System.out.printf(
|
||||
"Person %s has been marked as adult.\n",
|
||||
person.getName());
|
||||
}
|
||||
|
||||
@Priority
|
||||
public int getPriority() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
As required, this rule class operates on a person that is passed at construction time.
|
||||
|
||||
The `isAdult` method annotated with `@Condition` tells Easy Rules engine to call this method to check if the rule should be fired, in this case, if the person's age is greater than 18.
|
||||
|
||||
The `markAsAdult` method annotated with `@Action` will mark the person as adult by setting the `adult` flag.
|
||||
|
||||
Finally, the `getPriority` method annotated with `@Priority` tells Easy Rules engine to fire this rule in first order.
|
||||
|
||||
Now, let's create a class for rule 2:
|
||||
|
||||
```java
|
||||
@Rule(name = "alcoholRule",
|
||||
description = "Children are not allowed to buy alcohol.")
|
||||
public class AlcoholRule {
|
||||
|
||||
private Person person;
|
||||
|
||||
public AlcoholRule(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
@Condition
|
||||
public boolean isChildren() {
|
||||
return !person.isAdult();
|
||||
}
|
||||
|
||||
@Action
|
||||
public void denyAlcohol(){
|
||||
System.out.printf(
|
||||
"Sorry %s, you are not allowed to buy alcohol.\n",
|
||||
person.getName());
|
||||
}
|
||||
|
||||
@Priority
|
||||
public int getPriority() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
As for rule 1, the class operates on a person instance and provides methods to define rule condition, action and priority.
|
||||
|
||||
To launch the tutorial, we will use the following class:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//create a person instance
|
||||
Person tom = new Person("Tom", 16);
|
||||
System.out.println(
|
||||
"Tom: Hi! can I have some Vodka please?");
|
||||
|
||||
//create a rules engine
|
||||
RulesEngine rulesEngine = aNewRulesEngine().build();
|
||||
|
||||
//register rules
|
||||
rulesEngine.registerRule(new AgeRule(tom));
|
||||
rulesEngine.registerRule(new AlcoholRule(tom));
|
||||
|
||||
//fire rules
|
||||
rulesEngine.fireRules();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
To run the tutorial, you can follow these instructions from the root directory of Easy Rules :
|
||||
|
||||
{% highlight bash %}
|
||||
$ mvn install
|
||||
$ cd easyrules-samples
|
||||
$ mvn exec:java -P runRulePriorityTutorial
|
||||
{% endhighlight %}
|
||||
|
||||
If you run this tutorial, you would get the following output:
|
||||
|
||||
```
|
||||
Tom: Hi! can I have some Vodka please?
|
||||
INFO: Rule alcoholRule triggered.
|
||||
Sorry Tom, you are not allowed to buy alcohol.
|
||||
INFO: Rule alcoholRule performed successfully.
|
||||
```
|
||||
|
||||
As expected, since Tom's age is under 18, he has not been allowed to buy alcohol.
|
||||
|
@ -0,0 +1,261 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Shop tutorial
|
||||
header: Shop tutorial
|
||||
prev_section: tutorials/hello-world-tutorial
|
||||
next_section: tutorials/scheduler-tutorial
|
||||
doc: true
|
||||
---
|
||||
|
||||
In this tutorial, we have a shop application and we would like to implement the following requirement: deny children from buying alcohol.
|
||||
The minimal legal age to be considered as adult is 18. This tutorial is split in two parts:
|
||||
|
||||
1. Part 1: Implement the business rule of denying children from buying alcohol
|
||||
2. Part 2: Make the legal age configurable via JMX so we can change it at runtime
|
||||
|
||||
## Part 1 : Implement the business rule
|
||||
|
||||
Our shop customers are represented by the _Person_ class:
|
||||
|
||||
```java
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
private boolean adult;
|
||||
//getters and setters omitted
|
||||
}
|
||||
```
|
||||
|
||||
We will define the following rules:
|
||||
|
||||
* Rule 1: should operate an a `Person` instance, check that the person age is greater than 18 and set the adult flag.
|
||||
* Rule 2: should operate an a `Person` instance, check that the person is adult and deny children (ie, non adult) from buying alcohol.
|
||||
|
||||
Rule 1 should be fired **_before_** rule 2. We will set rule 1 priority to 1 and rule 2 priority to 2 so that Easy Rules engine fire them in this order.
|
||||
|
||||
First, let's create a class for rule 1:
|
||||
|
||||
```java
|
||||
public class AgeRule extends BasicRule {
|
||||
|
||||
private static final int ADULT_AGE = 18;
|
||||
|
||||
private Person person;
|
||||
|
||||
public AgeRule(Person person) {
|
||||
super("AgeRule",
|
||||
"Check if person's age is > 18 and
|
||||
marks the person as adult", 1);
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate() {
|
||||
return person.getAge() > ADULT_AGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
person.setAdult(true);
|
||||
System.out.printf("Person %s has been marked as adult",
|
||||
person.getName());
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
As required, this rule class operates on a person that is passed at construction time.
|
||||
|
||||
The `evaluate` method checks if the person's age is greater than 18.
|
||||
|
||||
The `execute` will mark the person as adult by setting the `adult` flag.
|
||||
|
||||
Finally, the third constructor argument which represents the rule priority is set to 1 to tells Easy Rules engine to fire this rule in first order.
|
||||
|
||||
Now, let's create a class for rule 2:
|
||||
|
||||
```java
|
||||
public class AlcoholRule extends BasicRule {
|
||||
|
||||
private Person person;
|
||||
|
||||
public AlcoholRule(Person person) {
|
||||
super("AlcoholRule",
|
||||
"Children are not allowed to buy alcohol",
|
||||
2);
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
@Condition
|
||||
public boolean evaluate() {
|
||||
return !person.isAdult();
|
||||
}
|
||||
|
||||
@Action
|
||||
public void execute(){
|
||||
System.out.printf("Shop: Sorry %s,
|
||||
you are not allowed to buy alcohol",
|
||||
person.getName());
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
As for rule 1, the class operates on a person instance and prints the denial message for children.
|
||||
|
||||
To launch the tutorial, we will use the following class:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//create a person instance
|
||||
Person tom = new Person("Tom", 14);
|
||||
System.out.println("Tom:
|
||||
Hi! can I have some Vodka please?");
|
||||
|
||||
//create a rules engine
|
||||
RulesEngine rulesEngine = aNewRulesEngine()
|
||||
.named("shop rules engine")
|
||||
.build();
|
||||
|
||||
//register rules
|
||||
rulesEngine.registerRule(new AgeRule(tom));
|
||||
rulesEngine.registerRule(new AlcoholRule(tom));
|
||||
|
||||
//fire rules
|
||||
rulesEngine.fireRules();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
To run the first part of the tutorial, you can follow these instructions from the root directory of Easy Rules :
|
||||
|
||||
{% highlight bash %}
|
||||
$ mvn install
|
||||
$ cd easyrules-samples
|
||||
$ mvn exec:java -P runShopTutorialPart1
|
||||
{% endhighlight %}
|
||||
|
||||
You should get the following output:
|
||||
|
||||
```
|
||||
Tom: Hi! can I have some Vodka please?
|
||||
INFO: Rule alcoholRule triggered.
|
||||
Shop: Sorry Tom, you are not allowed to buy alcohol
|
||||
INFO: Rule alcoholRule performed successfully.
|
||||
```
|
||||
|
||||
As expected, since Tom's age is under 18, he has not been allowed to buy alcohol.
|
||||
|
||||
## Part 2 : Changing the legal adult age at runtime
|
||||
|
||||
In this second part, we will expose the legal adult age as a JMX attribute.
|
||||
So first, let's define an interface that allows us to change this age via JMX:
|
||||
|
||||
```java
|
||||
@javax.management.MXBean
|
||||
public interface AgeJmxRule extends JmxRule {
|
||||
|
||||
int getAdultAge();
|
||||
|
||||
void setAdultAge(int adultAge);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Then, we should make our `AgeRule` implement the `AgeJmxRule` interface to expose the the legal adult age as a JMX attribute.
|
||||
So here is the new `AgeRule` class:
|
||||
|
||||
```java
|
||||
public class AgeRule extends BasicRule implements AgeJmxRule {
|
||||
|
||||
private int adultAge = 18;
|
||||
|
||||
private Person person;
|
||||
|
||||
public AgeRule(Person person) {
|
||||
super("AgeRule",
|
||||
"Check if person's age is > 18
|
||||
and marks the person as adult", 1);
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate() {
|
||||
return person.getAge() > adultAge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
person.setAdult(true);
|
||||
System.out.printf("Person %s has been marked as adult",
|
||||
person.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAdultAge() {
|
||||
return adultAge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAdultAge(int adultAge) {
|
||||
this.adultAge = adultAge;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Finally, let's suspend the program to change the legal adult age value at runtime via any compliant JMX client and see the engine behavior after this change:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//create a person instance
|
||||
Person tom = new Person("Tom", 14);
|
||||
System.out.println("Tom:
|
||||
Hi! can I have some Vodka please?");
|
||||
|
||||
//create a Jmx rules engine
|
||||
JmxRulesEngine rulesEngine = aNewJmxRulesEngine()
|
||||
.named("shop rules engine")
|
||||
.build();
|
||||
|
||||
//register rules
|
||||
rulesEngine.registerJmxRule(new AgeRule(tom));
|
||||
rulesEngine.registerRule(new AlcoholRule(tom));
|
||||
|
||||
//fire rules
|
||||
rulesEngine.fireRules();
|
||||
|
||||
// Update adult age via a JMX client.
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Change adult age via a JMX client
|
||||
and then press enter");
|
||||
scanner.nextLine();
|
||||
|
||||
System.out.println("Re fire rules after
|
||||
updating adult age...");
|
||||
|
||||
rulesEngine.fireRules();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
To run the second part of the tutorial, run the following command:
|
||||
{% highlight bash %}
|
||||
$ mvn exec:java -P runShopTutorialPart2
|
||||
{% endhighlight %}
|
||||
|
||||
You will be asked to change the legal adult age via a JMX compliant client.
|
||||
|
||||
In the next screenshot, we use <a href="https://visualvm.java.net" target="_blank">VisualVM</a> to change the age value:
|
||||
|
||||
<img style="width: 100%" src="{{site.url}}/img/jmx.png">
|
||||
|
||||
If you change the age value to 13 for example, you will see that Tom will be able to buy alcohol since his age (14) is greater than the new legal adult age (13).
|
||||
|
||||
That's all! In this tutorial, we have seen how to create a real business rule with Easy Rules and how to reconfigure it at runtime.
|
@ -0,0 +1,38 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Rule listener
|
||||
header: Rule listener
|
||||
prev_section: user-guide/defining-rules
|
||||
next_section: user-guide/rules-engine
|
||||
doc: true
|
||||
---
|
||||
|
||||
You can listen to rule execution events through the `RuleListener` API:
|
||||
|
||||
```java
|
||||
public interface RuleListener {
|
||||
/**
|
||||
* Triggered before a rule is executed.
|
||||
*/
|
||||
void beforeExecute(Rule rule);
|
||||
/**
|
||||
* Triggered after a rule is executed successfully.
|
||||
*/
|
||||
void onSuccess(Rule rule);
|
||||
/**
|
||||
* Triggered after a rule is executed with error.
|
||||
*/
|
||||
void onFailure(Rule rule, Exception exception);
|
||||
}
|
||||
```
|
||||
|
||||
You can implement this interface to provide custom behavior to execute before/after each rule.
|
||||
To register your listener, use the following snippet:
|
||||
|
||||
```java
|
||||
RulesEngine rulesEngine = aNewRulesEngine()
|
||||
.withRuleListener(myRuleListener)
|
||||
.build();
|
||||
```
|
||||
|
||||
You can register as many listeners as you want, they will be executed in their registration order.
|
Loading…
Reference in New Issue