add documentation and tutorial about Spring support

pull/27/head
Mahmoud Ben Hassine 10 years ago
parent 1c94075cf8
commit 3871e3df67

@ -64,6 +64,17 @@
<artifactId>easyrules-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.easyrules</groupId>
<artifactId>easyrules-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
</dependencies>
<profiles>
@ -155,6 +166,28 @@
</plugins>
</build>
</profile>
<profile>
<id>runSpringTutorial</id>
<build>
<defaultGoal>exec:java</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<mainClass>org.easyrules.samples.spring.Launcher</mainClass>
<systemProperties>
<systemProperty>
<key>java.util.logging.SimpleFormatter.format</key>
<value>[%1$tc] %4$s: %5$s%n</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

@ -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>

@ -98,6 +98,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.easyrules</groupId>
<artifactId>easyrules-spring</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

@ -28,6 +28,9 @@
<li class="{% if page.title == "Scheduling rules engine" %}current{% endif %}">
<a href="{{ site.url }}/user-guide/scheduling-rules-engine.html">Scheduling rules engine</a>
</li>
<li class="{% if page.title == "Embedding rules engine" %}current{% endif %}">
<a href="{{ site.url }}/user-guide/embedding-rules-engine.html">Embedding rules engine</a>
</li>
</ul>
<h4>Tutorials</h4>
@ -44,6 +47,9 @@
<li class="{% if page.title == "Engine scheduler" %}current{% endif %}">
<a href="{{ site.url }}/tutorials/scheduling-engine.html">Engine scheduler</a>
</li>
<li class="{% if page.title == "Using Easy Rules with Spring" %}current{% endif %}">
<a href="{{ site.url }}/tutorials/spring-tutorial.html">Spring tutorial</a>
</li>
</ul>

@ -1,7 +1,7 @@
---
layout: news
title: "Release notes"
prev_section: tutorials/scheduling-engine
prev_section: tutorials/spring-tutorial
doc: true
---

@ -3,7 +3,7 @@ layout: docs
title: Engine scheduler
header: Engine scheduler
prev_section: tutorials/dynamic-configuration
next_section: get-involved/release-notes
next_section: tutorials/spring-tutorial
doc: true
---

@ -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).

@ -3,7 +3,7 @@ layout: docs
title: Scheduling rules engine
header: Scheduling rules engine
prev_section: user-guide/managing-rules
next_section: tutorials/hello-world
next_section: user-guide/embedding-rules-engine
doc: true
---
@ -12,13 +12,11 @@ Easy Rules provides APIs to schedule a rules engine using the popular Java sched
To schedule a rules engine instance, first you need to add the following dependency to your **_pom.xml_**:
```xml
<dependencies>
<dependency>
<groupId>org.easyrules</groupId>
<artifactId>easyrules-quartz</artifactId>
<version>{{site.version}}</version>
</dependency>
</dependencies>
```
Then, you can create a `RulesEngineScheduler` as follows:

Loading…
Cancel
Save