1
我正在使用drools引擎构建警报系统。当条件满足时,我们需要执行Spring Framework框架中对规则(RHS)操作实例化的@服务的方法。如何在Drools规则中使用Spring服务?
通过Spring框架创建的Drools规则的动作(RHS)将使用什么方法来获取@service实例?
我按照以下指示:
- 使用表单导入功能(Rule1.drl)。此解决方案不起作用,因为该类在drools中实例化并需要执行静态方法。
- 使用全局会话变量(Rule2.drl)。此解决方案在“运行时”引发异常,表明它不是同一类类型。
有关如何使用它的任何想法?
由于
文件:Rule1.drl
package com.mycompany.alerts.Alert;
import function com.mycompany.alerts.service.SecurityService.notifyAlert;
rule "Activate Alert Type"
salience 9000
when
$alert: Alert(type == "TYPE1")
then
System.out.println("Running action:" + drools.getRule().getName());
$alert.setActive(Boolean.TRUE);
notifyAlert($alert.getStatus(),$alert.getSmsNumber());
System.out.println("End action:" + drools.getRule().getName());
end
文件:Rule2.drl
package com.mycompany.alerts.Alert;
global com.mycompany.alerts.service.SecurityService securityService;
rule "Activate Alert Type 2"
salience 9000
when
$alert: Alert(type == "TYPE2")
then
System.out.println("Running action:" + drools.getRule().getName());
$alert.setActive(Boolean.TRUE);
securityService.notifyAlert($alert.getStatus(),$alert.getSmsNumber());
System.out.println("End action:" + drools.getRule().getName());
end
文件:SecurityService.java
package com.mycompany.alerts.service;
import com.mycompany.alerts.service.UserRepository;
@Service
@Transactional
public class SecurityService {
private final Logger log = LoggerFactory.getLogger(SecurityService.class);
private final UserRepository userRepository;
public SecurityService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void notifyAlert(String status, String sms) {
System.out.println("Alert notify with:" + status + " sms:" + sms);
}
}