カスタム例外ハンドラーの設定

例外ハンドラーを設定して、ルール条件およびアクションでの例外処理をカスタマイズできます。CustomExceptionHandler API を実装すると、例外を無視するか、再スローできます。

このタスクについて

例外がスローされたとき、エンジンのデフォルトの動作は停止することです。CustomExceptionHandler API を実装すると、エンジンのデフォルトの動作を強制するか、例外を無視してエンジンがルールの処理を続行できるようにするかを選択できます。

CustomExceptionHandler をアクティブ化するには、XOM でインターフェースを実装してから、Rule Designer で実装クラス名を指定する必要があります。

手順

  1. XOM で CustomExceptionHandler インターフェースを実装します。 以下の例では、カスタム例外ハンドラーによって NumberFormatException の例外が再スローされます。
    package com.acme;
    import ...
    
    public class MyCustomExceptionHandler implements CustomExceptionHandler {
    
      static Logger logger = Logger.getLogger(MyCustomExceptionHandler.class.getName());
    
      /**
       * The custom exception handler must contain a default constructor.
       */
      public MyCustomExceptionHandler() {
      }
    
      @Override
      public void handleConditionException(Exception e) throws Exception {
        if (e instanceof NumberFormatException) {
          // ignore number format exception
        } else {
          // rethrow the exception, it will stop the engine execution
          throw e;
        }
      }
    
      @Override
      public void handleActionException(Exception e, RuleInstance ruleInstance) throws Exception {
        if (e instanceof NumberFormatException) {
          // ignore number format exception and log
          if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO,
                "Exception while executing action of rule " + ruleInstance.getRuleName(), e);
          }
        } else {
          // rethrow the exception, it will stop the engine execution
          throw e;
        }
      }
    }
  2. Rule Designer を開きます。
  3. ルール・プロジェクトのプロパティー・ウィンドウの「ルール・エンジン」パネルで、カスタム例外ハンドラー名を入力します。例えば、「カスタム例外ハンドラー」フィールドに com.acme.MyCustomExceptionHandler と入力します。
  4. 「OK」をクリックします。

タスクの結果

カスタム例外ハンドラーは、エンジンが開始されると作成され、エンジンがリセットされると削除されます。