update order tutorial to make suspect order rule configurable via JMX at runtime

pull/3/head
benas 12 years ago
parent 5fc8eb0895
commit 36dbf8446e

@ -31,8 +31,6 @@ package net.benas.easyrules.samples.order;
*/
class Order {
public static float ORDER_AMOUNT_THRESHOLD = 1000;
private long orderId;
private float amount;

@ -34,7 +34,7 @@ import net.benas.easyrules.core.DefaultRulesEngine;
*/
public class OrderSampleLauncher {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
Order order = new Order(6654, 1200);
Customer customer = new Customer(2356, true);
@ -44,7 +44,7 @@ public class OrderSampleLauncher {
*/
SuspectOrderRule suspectOrderRule = new SuspectOrderRule(
"Suspect Order",
"Send alert if a new customer checks out an order with amount greater than 1000$",
"Send alert if a new customer checks out an order with amount greater than a threshold",
1);
/**
@ -57,12 +57,21 @@ public class OrderSampleLauncher {
* Create a default rules engine and register the business rule
*/
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.registerRule(suspectOrderRule);
rulesEngine.registerJmxManagedRule(suspectOrderRule, true, SuspectOrderJmxManagedRule.class);
/**
* Fire rules
*/
rulesEngine.fireRules();
// Suspend execution for 30s to have time to update suspect order amount threshold via a JMX client.
Thread.sleep(30000);
System.out.println("**************************************************************");
System.out.println("Re fire rules after updating suspect order amount threshold...");
System.out.println("**************************************************************");
rulesEngine.fireRules();
}
}

@ -0,0 +1,29 @@
package net.benas.easyrules.samples.order;
import net.benas.easyrules.api.JmxManagedRule;
import javax.management.MXBean;
/**
* Interface to make suspect order rule manageable via JMX.<br/>
* Suspect order threshold should be changed at runtime.
*
* @author benas (md.benhassine@gmail.com)
*/
@MXBean
public interface SuspectOrderJmxManagedRule extends JmxManagedRule {
/**
* 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);
}

@ -31,7 +31,9 @@ import net.benas.easyrules.core.Rule;
*
* @author benas (md.benhassine@gmail.com)
*/
class SuspectOrderRule extends Rule {
class SuspectOrderRule extends Rule implements SuspectOrderJmxManagedRule {
private float suspectOrderAmountThreshold = 1000;
private Order order;
@ -43,13 +45,13 @@ class SuspectOrderRule extends Rule {
@Override
public boolean evaluateConditions() {
return order.getAmount() > Order.ORDER_AMOUNT_THRESHOLD && customer.isNew();
return order.getAmount() > suspectOrderAmountThreshold && customer.isNew();
}
@Override
public void performActions() throws Exception {
System.out.println("Alert : A new customer [id=" + customer.getCustomerId() + "] has checked out an order [id=" +
order.getOrderId() + "] with amount " + order.getAmount() + " > " + Order.ORDER_AMOUNT_THRESHOLD);
order.getOrderId() + "] with amount " + order.getAmount() + " > " + suspectOrderAmountThreshold);
}
public void setOrder(Order order) {
@ -59,4 +61,13 @@ class SuspectOrderRule extends Rule {
public void setCustomer(Customer customer) {
this.customer = customer;
}
public float getSuspectOrderAmountThreshold() {
return suspectOrderAmountThreshold;
}
public void setSuspectOrderAmountThreshold(float suspectOrderAmountThreshold) {
this.suspectOrderAmountThreshold = suspectOrderAmountThreshold;
}
}

Loading…
Cancel
Save