Mule 捕获异常策略后无法将控制权恢复到流
在使用catch异常策略捕获和处理异常之后,我无法将控制权带回mule中的主流。有没有办法像在java中一样在mule中继续正常执行。 更多详情请参考代码 简单web服务类:Mule 捕获异常策略后无法将控制权恢复到流,mule,mule-component,Mule,Mule Component,在使用catch异常策略捕获和处理异常之后,我无法将控制权带回mule中的主流。有没有办法像在java中一样在mule中继续正常执行。 更多详情请参考代码 简单web服务类: public class HorrorScopeService { public String getHorrorScopeByMonth(String month) throws Exception { /*CustomLogger.info(CustomLogger.requestLogg
public class HorrorScopeService {
public String getHorrorScopeByMonth(String month) throws Exception {
/*CustomLogger.info(CustomLogger.requestLogger, this.getClass(), "getHorrorScopeByMonth()", "PROCESSING REQUEST");
CustomLogger.info(CustomLogger.requestLogger, this.getClass(), "getHorrorScopeByMonth()", String.format("MONTH FOR WHICH REQUEST IS PROCESSING IS %s", month));*/
//CustomException customException=null;
CustomLogger.infoRequest(CustomLogger.requestLogger, "RID_9051844007", "REQUEST", "TESTING", "9051844007", "ha ha ha");
String status="SUCCESS";
System.out.println("1111111111111111111111");
String data=null;;
try{
switch(month) {
case "JAN":
data="Not a good month to be born";
break;
case "FEB":
data="People born in this month are ok ok";
break;
case "MAR":
data="People born in this month are good";
break;
case "APR":
data="People born in this month are brilliant";
break;
default:
status="FAIL";
data="you should not be born";
int number=2;
int result=number/0;
}
}catch(Exception e) {
System.out.println("2222222222222222222222222222");
throw new CustomException(e, "RID_905184", "9051844",
this.getClass(), "getHorrorScopeByMonth()");
} finally {
CustomLogger.infoResponse(CustomLogger.requestLogger, "RID_9051844007", "RESPONSE", "TESTING", "9051844007", "90SEC", status, status,(String) data);
CustomLogger.infoFunctionalKeyValueLog(CustomLogger.functionalLogger, "RID_9051844007", "ERROR", "TESTING", "9051844007", this.getClass(), "getHorrorScopeByMonth()",(String) data);
System.out.println("55555555555555555555555555555");
}
return data;
}
}
处理程序类:这是处理异常的类
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import com.comviva.custom.exception.CustomException;
import com.comviva.custom.logger.CustomLogger;
public class CustomExceptionHandler extends AbstractTransformer{
@Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
System.out.println("33333333333333333333333333 "+src.toString()+ " enc="+enc);
CustomException exceptionClass=(CustomException)src;
//System.out.println("print stack trace=");
//exceptionClass.getEx().printStackTrace();
System.out.println("4444444444444444444444get stack trace="+exceptionClass.getEx().getCause() );
CustomLogger.error(CustomLogger.errorLogger, exceptionClass.getRequestId(), "ERROR", "TESTING", exceptionClass.getUserIdentifier(), exceptionClass.getClassName(),
exceptionClass.getMethodName(),String.valueOf(exceptionClass.getEx().getStackTrace()[0].getLineNumber()), exceptionClass.getEx().getMessage(), exceptionClass.getEx());
return src;
}
}
在处理异常后,我希望最终执行块,但它不会被执行。在处理异常之后,是否有任何方法将控制器返回到抛出器类。在finally块中手动调用流
public class MyMessageProcessor implement MessageProcessor, MuleContextAware
{
private MuleContext muleContext;
public void setMuleContext(MuleContext context)
{
this.muleContext = muleContext;
}
public MuleEvent process(MuleEvent event)
{
Flow flow = (Flow)muleContext.getRegistry().lookupFlowConstruct("flowName");
//here you can mutate the payload, variables, properties, etc. before calling the flow
return flow.process(event);
}
}
找到问题的解决方法: 将catch异常处理器放在一个单独的流中,并使用来自主流的流引用来调用该新流。catch异常触发后,流引用恢复后执行 比如,
<flow name="mainFlow">
<http:listener config-ref="HTTP_Listener" path="/" doc:name="HTTP"/>
<flow-ref name="otherFlow" doc:name="otherFlow"/>
<set-payload value="hello world" doc:name="Say hello"/>
</flow>
<flow name="otherFlow">
<validation:is-true expression="#[false]" doc:name="throw exception"/>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="exception occured" doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
mainFlow调用otherFlow
验证处理器触发异常
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import com.comviva.custom.exception.CustomException;
import com.comviva.custom.logger.CustomLogger;
public class CustomExceptionHandler extends AbstractTransformer{
@Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
System.out.println("33333333333333333333333333 "+src.toString()+ " enc="+enc);
CustomException exceptionClass=(CustomException)src;
//System.out.println("print stack trace=");
//exceptionClass.getEx().printStackTrace();
System.out.println("4444444444444444444444get stack trace="+exceptionClass.getEx().getCause() );
CustomLogger.error(CustomLogger.errorLogger, exceptionClass.getRequestId(), "ERROR", "TESTING", exceptionClass.getUserIdentifier(), exceptionClass.getClassName(),
exceptionClass.getMethodName(),String.valueOf(exceptionClass.getEx().getStackTrace()[0].getLineNumber()), exceptionClass.getEx().getMessage(), exceptionClass.getEx());
return src;
}
}
捕获异常策略运行
在mainFlow中使用设置的有效负载“Say hello”继续执行
请注意,otherFlow不是子流,因为子流不能有自己的异常策略。我将尝试这种方法。但我认为这将返回流,但当我们调用flow.process时,它将从一开始处理流。如果我错了,请纠正我。如果您想处理特定组件的异常,请尝试将它们包装在事务块中,并使它们具有自己的异常策略