【问题标题】:Unable to get Control back to the flow after catch exception strategy捕获异常策略后无法让控制回到流程
【发布时间】:2016-04-01 15:27:15
【问题描述】:

使用 catch 异常策略捕获并处理异常后,我无法将控制权带回 mule 中的主流。有什么方法可以像我们在 java 中一样在 mule 中继续正常执行。 详情请参考代码。

简单的网络服务类:

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

【问题讨论】:

    标签: mule mule-component


    【解决方案1】:

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

    【讨论】:

    • 我会尝试这种方法。但我认为这将返回流程,但是当我们调用 flow.process 时,它将从头开始处理流程。如果我错了,请纠正我。
    • 如果你想处理特定组件的异常,然后尝试将它们包装在事务块中并让它们有自己的异常策略
    【解决方案2】:

    找到解决问题的方法:

    将捕获异常处理器放在单独的流中,并使用主流中的流引用来调用该新流。在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。

    验证处理器触发异常

    捕获异常策略运行

    在 mainFlow 中使用 set-payload "Say hello" 继续执行

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2019-09-25
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      相关资源
      最近更新 更多