捕获异常策略后无法将控制权恢复到流程

时间:2016-03-31 08:38:31

标签: mule mule-component

使用catch异常策略捕获并处理异常后,我无法将控件恢复到mule中的主流。有没有办法像在java中一样继续正常执行mule。 请参阅代码以获取更多细节。

简单的Web服务类:

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;

    }
}

Handler类:这是处理异常的类

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块执行但是没有执行。在处理异常后,有没有办法让控制器回到thrower类。

2 个答案:

答案 0 :(得分:0)

在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); 
} 
} 

答案 1 :(得分:0)

找到解决问题的方法:

将catch-exception处理器放在单独的流中,并使用主流中的Flow Reference来调用该新流。 catch-exception触发后,将恢复Flow Reference之后的执行。

例如;

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

验证处理器触发异常

catch-exception-strategy运行

执行恢复与set-payload&#34;说你好&#34;在mainFlow中

请注意,otherFlow不是子流,因为子流不能有自己的异常策略。