我目前在 Drools 服务器中遇到了问题。
我们收到了对 drools 服务器场的相当大的请求,该请求抛出 OutOfMemoryError
。那些大的请求不应该被处理(它们是错误),如果请求花费超过 5 秒,则应该被取消。
我尝试使用自定义超时 DRL 规则来解决此问题 has explained here :
rule "Stop the rule engine after 5s"
timer ( int: 5s )
salience 0
when
then
System.err.println("*** Stop the rule engine after 5s ***");
drools.halt();
end
但这条规则从未被考虑在内。
所以我尝试了另一种解决方法:将我的 ksession.fireAllRules();
在一个线程中,如果需要更多 5 秒,请使用 ksession.halt();
停止流口水。 ;像这样:
KieSessionConfiguration conf = kieServices.newKieSessionConfiguration();
final KieSession ksession = kcontainer.newKieSession(conf);
Thread droolsThread = new Thread(new Runnable() {
public void run() {
ksession.fireAllRules();
}
}, "drools");
long tStart = System.currentTimeMillis();
droolsThread.start();
/*wait until the end of the Thread or 5 seconds max*/
while (System.currentTimeMillis() - tStart < 5000 && droolsThread.isAlive())
Thread.sleep(100);
/*if alive for more than 5 seconds stop it*/
if (droolsThread.isAlive())
{
System.err.println("timeout killing drools");
ksession.halt();
ksession.dispose();
}
在第二种情况下,当使用 ksession.fireAllRules();
调用 drools 时,它开始执行请求,如果这是一个巨大的请求,则会抛出 Exception: java.lang.OutOfMemoryError
异常(exception):
[apache-tomcat-9.0.39]: timeout killing drools
[apache-tomcat-9.0.39]: Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Catalina-utility-1"
java.lang.OutOfMemoryError: Java heap space
我不知道该怎么做才能阻止那些大请求占用所有服务器内存。 是否可以停止当前在 drools 中运行的长请求?
最佳答案
您的方向是正确的,但您需要使用 fireUntilHalt (而不是 fireAllRules)开始规则执行: https://docs.drools.org/7.40.0.Final/kie-api-javadoc/org/kie/api/runtime/rule/StatefulRuleSession.html#fireUntilHalt--
然后您将能够通过 halt 方法停止它。
我还建议您了解一下为什么您会看到这种“吃掉所有服务器内存”的模式。也许会有机会优化你的规则..
关于java - 处理长火 Drools 中的所有规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64897267/