add documentation and tutorial about Spring support
parent
1c94075cf8
commit
3871e3df67
@ -0,0 +1,19 @@
|
||||
package org.easyrules.samples.spring;
|
||||
|
||||
import org.easyrules.annotation.Action;
|
||||
import org.easyrules.annotation.Condition;
|
||||
import org.easyrules.annotation.Rule;
|
||||
|
||||
@Rule(name = "dummy rule")
|
||||
public class DummyRule {
|
||||
|
||||
@Condition
|
||||
public boolean when() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void then(){
|
||||
System.out.println("Hey, I'm managed by Spring");
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2014, Mahmoud Ben Hassine (mahmoud@benhassine.fr)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.easyrules.samples.spring;
|
||||
|
||||
import org.easyrules.api.RulesEngine;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Main class to run the Spring tutorial.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
|
||||
*/
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("org/easyrules/samples/spring/application-context.xml");
|
||||
RulesEngine rulesEngine = (RulesEngine) context.getBean("rulesEngine");
|
||||
|
||||
rulesEngine.fireRules();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- configure rule -->
|
||||
<bean id="rule" class="org.easyrules.samples.spring.DummyRule"/>
|
||||
|
||||
<!-- configure rules engine -->
|
||||
<bean id="rulesEngine" class="org.easyrules.spring.RulesEngineFactoryBean">
|
||||
<property name="rules">
|
||||
<list>
|
||||
<ref bean="rule"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
@ -0,0 +1,99 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Using Easy Rules with Spring
|
||||
header: Using Easy Rules with Spring
|
||||
prev_section: tutorials/scheduling-engine
|
||||
next_section: get-involved/release-notes
|
||||
doc: true
|
||||
---
|
||||
|
||||
In this tutorial, you will learn how to use Easy Rules embedded in a <a href="http://www.spring.io" target="_blank">Spring</a> container.
|
||||
|
||||
You will create a dummy rule and a rules engine and configure them as Spring beans. So let's get started.
|
||||
|
||||
First you need to add the following dependency to your **_pom.xml_**:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.easyrules</groupId>
|
||||
<artifactId>easyrules-spring</artifactId>
|
||||
<version>{{site.version}}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
Then, let's create the dummy rule:
|
||||
|
||||
```java
|
||||
@Rule(name = "dummy rule")
|
||||
public class DummyRule {
|
||||
|
||||
@Condition
|
||||
public boolean when() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void then(){
|
||||
System.out.println("Hey, I'm managed by Spring");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now, we we can use the `RulesEngineFactoryBean` to configure a rules engine and register the dummy rule:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- configure rule -->
|
||||
<bean id="rule" class="org.easyrules.samples.spring.DummyRule"/>
|
||||
|
||||
<!-- configure rules engine -->
|
||||
<bean id="rulesEngine" class="org.easyrules.spring.RulesEngineFactoryBean">
|
||||
<property name="rules">
|
||||
<list>
|
||||
<ref bean="rule"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
```
|
||||
|
||||
Finally, we can get the rules engine from the Spring context and fire rules:
|
||||
|
||||
```java
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("org/easyrules/samples/spring/application-context.xml");
|
||||
RulesEngine rulesEngine = (RulesEngine) context.getBean("rulesEngine");
|
||||
|
||||
rulesEngine.fireRules();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
That's all. 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 runSpringTutorial
|
||||
{% endhighlight %}
|
||||
|
||||
You would get the following output:
|
||||
|
||||
```
|
||||
INFO: Rule priority threshold: 2,147,483,647
|
||||
INFO: Skip on first applied rule: false
|
||||
INFO: Skip on first failed rule: false
|
||||
INFO: Rule 'dummy rule' triggered.
|
||||
Hey, I'm managed by Spring
|
||||
INFO: Rule 'dummy rule' performed successfully.
|
||||
```
|
||||
|
@ -0,0 +1,85 @@
|
||||
---
|
||||
layout: docs
|
||||
title: Embedding rules engine
|
||||
header: Embedding rules engine
|
||||
prev_section: user-guide/scheduling-rules-engine
|
||||
next_section: tutorials/hello-world
|
||||
doc: true
|
||||
---
|
||||
|
||||
Easy Rules is a lightweight library that can be used in a standalone Java application or embedded in a web server or a DI container.
|
||||
|
||||
As of version {{ site.version }}, Easy Rules provides support for <a href="http://www.spring.io" target="_blank">Spring</a>.
|
||||
Support for other DI containers will be added in future versions.
|
||||
|
||||
In this section, you will learn how to configure rules and rules engine as Spring beans.
|
||||
First you need to add the following dependency to your **_pom.xml_**:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.easyrules</groupId>
|
||||
<artifactId>easyrules-spring</artifactId>
|
||||
<version>{{site.version}}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
Then, you can configure your rules and the rules engine as follows:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- configure rule -->
|
||||
<bean id="rule" class="DummyRule"/>
|
||||
|
||||
<!-- configure rule listener -->
|
||||
<bean id="ruleListener" class="DummyRuleListener"/>
|
||||
|
||||
<!-- configure rules engine -->
|
||||
<bean id="rulesEngine" class="org.easyrules.spring.RulesEngineFactoryBean">
|
||||
<property name="skipOnFirstAppliedRule" value="true"/>
|
||||
<property name="skipOnFirstFailedRule" value="true"/>
|
||||
<property name="rulePriorityThreshold" value="10"/>
|
||||
<property name="silentMode" value="false"/>
|
||||
<property name="rules">
|
||||
<list>
|
||||
<ref bean="rule"/>
|
||||
</list>
|
||||
</property>
|
||||
<property name="ruleListeners">
|
||||
<list>
|
||||
<ref bean="ruleListener"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
```
|
||||
|
||||
The `RulesEngineFactoryBean` is responsible for creating rules engine instances.
|
||||
As you can see, this factory bean is the main entry point to configure:
|
||||
|
||||
* Rules
|
||||
* Rules listeners
|
||||
* And engine parameters (priority threshold, skipOnFirstAppliedRule, silentMode, etc)
|
||||
|
||||
To get the engine and fires rules, you can use the following snippet:
|
||||
|
||||
```java
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
|
||||
RulesEngine rulesEngine = (RulesEngine) context.getBean("rulesEngine");
|
||||
|
||||
rulesEngine.fireRules();
|
||||
```
|
||||
|
||||
<div class="note info">
|
||||
<h5>Hint:</h5>
|
||||
<p>The main advantage of using Easy Rules with Spring is the ability to register/unregister rules through the Xml configuration
|
||||
without recompiling your application.</p>
|
||||
</div>
|
||||
|
||||
|
||||
You can find a complete tutorial on how to use Easy Rules with Spring [here]({{site.url}}/tutorials/spring-tutorial.html).
|
Loading…
Reference in New Issue