exception handler in drools rule
2014-05-30 11:36
585 查看
As I know, we have 2 ways to handle exception in drools rule:
1. try-catch in rule file(.drl) consequence:
e.g.
rule "r1"
when
//conditions
then
try{
//do something...
}catch(Exception e){
//exception handler
}
end
2. create your own exception handler class implement ConsequenceExceptionHandler, register it to knowledgebase
e.g:
public StatefulKnowledgeSession getSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(fileName,
TestListObjOnlyRunOnce.class), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println("规则错误:");
Iterator<KnowledgeBuilderError> it = kbuilder.getErrors()
.iterator();
while (it.hasNext())
System.out.println(it.next());
return null;
}
KnowledgeBaseConfiguration kBaseConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
@SuppressWarnings("unchecked")
Class ehClass = MyConsequenceExceptionHandler.class;
ConsequenceExceptionHandlerOption cehOption = ConsequenceExceptionHandlerOption.get( ehClass );
kBaseConfig.setOption( cehOption );
KnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
kb.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kb.newStatefulKnowledgeSession();
}
-----exception handler class---
package com.test.DroolsRuleTests;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.drools.runtime.rule.Activation;
import org.drools.runtime.rule.ConsequenceExceptionHandler;
import org.drools.runtime.rule.WorkingMemory;
public class MyConsequenceExceptionHandler implements ConsequenceExceptionHandler, Externalizable {
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException {}
public void writeExternal( ObjectOutput out ) throws IOException {}
public void handleException( Activation activation,
WorkingMemory workingMemory,
Exception exception ){
// exception handler logic here
System.err.println("excpeiton occured when execute rule: " + activation.getRule().getName());
XXX xxx =(XXX) workingMemory.getObject(activation.getPropagationContext().getFactHandle());
exception.printStackTrace();
}
}
1. try-catch in rule file(.drl) consequence:
e.g.
rule "r1"
when
//conditions
then
try{
//do something...
}catch(Exception e){
//exception handler
}
end
2. create your own exception handler class implement ConsequenceExceptionHandler, register it to knowledgebase
e.g:
public StatefulKnowledgeSession getSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(fileName,
TestListObjOnlyRunOnce.class), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println("规则错误:");
Iterator<KnowledgeBuilderError> it = kbuilder.getErrors()
.iterator();
while (it.hasNext())
System.out.println(it.next());
return null;
}
KnowledgeBaseConfiguration kBaseConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
@SuppressWarnings("unchecked")
Class ehClass = MyConsequenceExceptionHandler.class;
ConsequenceExceptionHandlerOption cehOption = ConsequenceExceptionHandlerOption.get( ehClass );
kBaseConfig.setOption( cehOption );
KnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
kb.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kb.newStatefulKnowledgeSession();
}
-----exception handler class---
package com.test.DroolsRuleTests;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.drools.runtime.rule.Activation;
import org.drools.runtime.rule.ConsequenceExceptionHandler;
import org.drools.runtime.rule.WorkingMemory;
public class MyConsequenceExceptionHandler implements ConsequenceExceptionHandler, Externalizable {
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException {}
public void writeExternal( ObjectOutput out ) throws IOException {}
public void handleException( Activation activation,
WorkingMemory workingMemory,
Exception exception ){
// exception handler logic here
System.err.println("excpeiton occured when execute rule: " + activation.getRule().getName());
XXX xxx =(XXX) workingMemory.getObject(activation.getPropagationContext().getFactHandle());
exception.printStackTrace();
}
}
相关文章推荐
- laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
- System.Exception when running SearchAsync of Contacts in application_launching event handler 未处理Exception
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
- laravel - Error in exception handler
- java.lang.IllegalArgumentException: At least one handler method must be found in class [class SayThr
- azkaban java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "main"
- Handler execution resulted in exception: Content type 'application/json;charset=UTF-8' not supported
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread 报错解决
- IDEA错误:Failed to start end point associated with ProtocolHandler [http-nio-9999] java.net.BindException: Address already in use: bind
- ERROR SparkUncaughtExceptionHandler: Uncaught exception in thread
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- Drools 运行时出现Exception in thread "main" java.lang.RuntimeException: wrong class format 的问题
- 【tomcat】启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"] java.lang.Exception: Socket bind failed 和java.net.BindException: Address already in use: JVM_Bind错误解决
- 奋斗了n(n>7)小时,终于解决了连接远程JMS JNDI的问题:java.rmi.NoSuchObjectException: no such object in table
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContex
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- {已经解决}android java.net.BindException: bind failed: EADDRINUSE (Address already in use)
- 严重: Field 'id' doesn't have a default value Exception in thread "main" org.hibernate.exception.Gener
- Exception in thread "main" java.util.NoSuchElementException
- MySQLIntegrityConstraintViolationException: Column 'ID' in where clause is ambiguous ;