Java 使用ConsequenceExceptionHandler触发规则时记录异常
我通过spring/camel配置了drools服务器,我希望能够将触发规则时发生的所有运行时异常以及异常时工作内存状态的详细信息记录到一个文件中 我发现drools spring的drools版本>=5.2确实允许在spring配置中设置自定义ConsequenceExceptionHandler类: 我遇到了一些问题(其中一些与从drools 5.1迁移到5.2有关),所以我想知道是否有人以前做过异常日志记录,并可以分享一些实现细节。或者如果有人能告诉我是否有比通过自定义异常处理程序更好的方法来实现这一点。在我的项目中(我想我必须在我的博客上写一些关于Drools的文章),我以以下方式编写了一个自定义侦听器(我想我在这里找到了一个很好的教程) 我在applicationContext中将知识库定义为一个bean:Java 使用ConsequenceExceptionHandler触发规则时记录异常,java,drools,Java,Drools,我通过spring/camel配置了drools服务器,我希望能够将触发规则时发生的所有运行时异常以及异常时工作内存状态的详细信息记录到一个文件中 我发现drools spring的drools版本>=5.2确实允许在spring配置中设置自定义ConsequenceExceptionHandler类: 我遇到了一些问题(其中一些与从drools 5.1迁移到5.2有关),所以我想知道是否有人以前做过异常日志记录,并可以分享一些实现细节。或者如果有人能告诉我是否有比通过自定义异常处理程序更好的
<drools:kbase id="fxKBase">
<drools:resources>
<drools:resource type="DRL" source="classpath:path/first.drl"/>
<drools:resource type="DRL" source="classpath:path/second.drl"/>
</drools:resources>
<drools:configuration>
<drools:consequenceExceptionHandler handler="a.b.c.MyConsequenceExceptionHandler" />
</drools:configuration>
</drools:kbase>
以及MyConsequenceException:
public class MyConsequenceException extends RuntimeException {
private final WorkingMemory workingMemory;
private final Activation activation;
public MyConsequenceException(final Activation activation, final WorkingMemory workingMemory, final Exception exception) {
super(exception);
this.activation = activation;
this.workingMemory = workingMemory;
}
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder( "Exception executing consequence for " );
if( activation != null && ( activation.getRule() ) != null ){
Rule rule = activation.getRule();
String ruleName = rule.getName();
sb.append("rule [\"").append( ruleName ).append( "\"]. " );
} else {
sb.append( "rule, name unknown" );
}
Throwable throwable = ExceptionUtils.getRootCause(getCause());
sb.append("The thrown exception is [").append(throwable).append("]. ");
return sb.toString();
}
@Override
public String toString() {
return getMessage();
}
}
这样,当抛出异常时,您将得到一条您选择的自定义消息 在我的项目中(我想我必须在我的博客上写一些关于Drools的文章),我以以下方式编写了一个自定义侦听器(我想我在这里找到了一个不错的教程)
我在applicationContext中将知识库定义为一个bean:
<drools:kbase id="fxKBase">
<drools:resources>
<drools:resource type="DRL" source="classpath:path/first.drl"/>
<drools:resource type="DRL" source="classpath:path/second.drl"/>
</drools:resources>
<drools:configuration>
<drools:consequenceExceptionHandler handler="a.b.c.MyConsequenceExceptionHandler" />
</drools:configuration>
</drools:kbase>
以及MyConsequenceException:
public class MyConsequenceException extends RuntimeException {
private final WorkingMemory workingMemory;
private final Activation activation;
public MyConsequenceException(final Activation activation, final WorkingMemory workingMemory, final Exception exception) {
super(exception);
this.activation = activation;
this.workingMemory = workingMemory;
}
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder( "Exception executing consequence for " );
if( activation != null && ( activation.getRule() ) != null ){
Rule rule = activation.getRule();
String ruleName = rule.getName();
sb.append("rule [\"").append( ruleName ).append( "\"]. " );
} else {
sb.append( "rule, name unknown" );
}
Throwable throwable = ExceptionUtils.getRootCause(getCause());
sb.append("The thrown exception is [").append(throwable).append("]. ");
return sb.toString();
}
@Override
public String toString() {
return getMessage();
}
}
这样,当抛出异常时,您将得到一条您选择的自定义消息 迟来(在我发布问题将近两年后得到你的答案)总比不来好!天竺葵!迟来(在我发布问题将近两年后得到你的答案)总比不来好!天竺葵!