https://github.com/subkanthi/spring-boot-drools
Spring Boot sample application with Drools rules
https://github.com/subkanthi/spring-boot-drools
cep drools rest spring-boot
Last synced: 6 months ago
JSON representation
Spring Boot sample application with Drools rules
- Host: GitHub
- URL: https://github.com/subkanthi/spring-boot-drools
- Owner: subkanthi
- Created: 2018-09-03T11:44:25.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-04T08:41:35.000Z (about 5 years ago)
- Last Synced: 2025-03-26T07:21:45.049Z (7 months ago)
- Topics: cep, drools, rest, spring-boot
- Language: Java
- Size: 14.2 MB
- Stars: 2
- Watchers: 0
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Boot Drools sample Application
Spring Boot sample application with Drools rulesSample application that will load all drools rules on startup
and will expose a webservice to pass the object and execute
rules on the object.TLDR; This is not tested for production quality in the scale of 100s and thousands of rules.
# Singleton KieSession Bean
KieSession is wrapped in a Spring Singleton Bean.
```
/**
* Singleton class that wraps
* the Drools KieSession and is responsible
* for loading rules and firing rules.
*/
@Component
public class RulesService {private KieServices ks;
private KieContainer kContainer;
private KieSession kieSession;@Bean("accountService")
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public RulesService getRulesService() {
return new RulesService();
}/**
* Function to initialize the kieSession.
* This will be initialized on
* application startup.
*/
public void initializeRules() {this.ks = KieServices.Factory.get();
this.kContainer = ks.getKieClasspathContainer();
this.kieSession = kContainer.newKieSession();
}public int fireRules(Item item) {
this.kieSession.insert(item);
return this.kieSession.fireAllRules();}
}
```# Web Service to pass Object and execute rules.
```
@RestController
public class RulesController {@Autowired
RulesService rulesService;@PostMapping("/rules")
public String index(@RequestBody Item item) {
int fired = this.rulesService.fireRules(item);
```
# Sample JSON call to pass Object
```
HTTP POST
http://localhost:8080/rules
{
"id": 2,
"name": "Test",
"cost": 100
}
```