You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.6 KiB
2.6 KiB
layout | title | header | prev_section | next_section | doc |
---|---|---|---|---|---|
docs | Using Easy Rules with Spring | Using Easy Rules with Spring | tutorials/scheduling-engine | get-involved/release-notes | true |
In this tutorial, you will learn how to use Easy Rules embedded in a Spring 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:
<dependency>
<groupId>org.easyrules</groupId>
<artifactId>easyrules-spring</artifactId>
<version>{{site.version}}</version>
</dependency>
Then, let's create the dummy 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");
}
}
Now, we we can use the RulesEngineFactoryBean
to configure a rules engine and register the dummy rule:
<?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:
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.