使用 ConsequenceExceptionHandler 触发规则时记录异常

发布于 2024-12-01 02:26:58 字数 432 浏览 0 评论 0原文

我通过 spring/camel 配置了 drools 服务器,并且希望能够将触发规则时发生的所有运行时异常以及有关异常发生时工作内存状态的详细信息记录到文件中。

我发现 drools-spring 的 drools 版本 >= 5.2 确实允许在 spring 配置中设置自定义 ConsequenceExceptionHandler 类:

https://issues.jboss.org/browse/JBRULES-2674

我遇到了一些麻烦(其中一些与从 drools 5.1 迁移到 5.2)所以我想知道是否有人以前做过异常日志记录并且可以分享一些实现细节。或者如果有人可以告诉我是否有比通过自定义异常处理程序更好的方法来实现此目的。

I have my drools server configured via spring/camel and I'd like to be able log to a file all runtime exceptions that occur when rules are fired, along with details about the state of the working memory at the time of the exception.

I found that drools version >= 5.2 of drools-spring does allow for the setting of a custom ConsequenceExceptionHandler class in the spring configuration:

https://issues.jboss.org/browse/JBRULES-2674

I'm having some trouble (some of which related from migrating from drools 5.1 to 5.2) so I was wondering if anyone has done the logging of exceptions before and could share some implementation details. Or if someone can tell me if there's a better way to achieve this than through a custom exception handler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-12-08 02:26:58

在我的项目中(我想我必须在我的博客http://tooomuchcoding.blogspot.com 我有一些关于 Drools 的文章)我用以下方式编写了一个自定义监听器(我想我在这里找到了一个很好的教程 http://members.inode.at/w.laun/drools/CustomConsequenceExceptionHandlingHowTo.html)

我的 KnowledgeBase 定义为 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>

然后我定义了 MyConsequenceExceptionHandler

public class MyConsequenceExceptionHandler implements ConsequenceExceptionHandler {
@Override
public void handleException(Activation activation, WorkingMemory workingMemory, Exception exception) {
    throw new MyConsequenceException(activation, workingMemory, exception);
}

和 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();
} 

}

我将 当抛出异常时,您将收到您选择的自定义消息。

In my project (I think I'll have to write about it on my blog http://toomuchcoding.blogspot.com where I have some articles about Drools) I wrote a custom Listener in the following manner (I think I found a nice tutorial over here http://members.inode.at/w.laun/drools/CustomConsequenceExceptionHandlingHowTo.html)

I defined my KnowledgeBase as a bean in my applicationContext:

<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>

Then I defined MyConsequenceExceptionHandler

public class MyConsequenceExceptionHandler implements ConsequenceExceptionHandler {
@Override
public void handleException(Activation activation, WorkingMemory workingMemory, Exception exception) {
    throw new MyConsequenceException(activation, workingMemory, exception);
}

and the 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();
} 

}

In that way when an exception is thrown you will get a custom message of your choosing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文