ルール・セット・キャッシュのカスタマイズ

ルール・セット・キャッシュは、独自の実装クラスを作成し、それに従って実行単位 (XU) の配布記述子を変更することによってカスタマイズできます。

始める前に

メモリー情報を入手できるようにするには、Rule Execution Server メモリー・プロファイラーの Java™ エージェントをインストールする必要があります。

このタスクについて

実行単位 (XU) は、ルール・セット・インスタンスのキャッシュを使用して、各実行のルール・セットを解析しなくてもよいようにします。 ルール・セットを参照する XU の JCA 接続プールに少なくとも 1 つの接続があれば、そのルール・セットはメモリーにとどまります。 JCA 接続は、使用中でなくても、アプリケーション・サーバーによって削除されるまでルール・セットを参照します。 ルール・セットを参照する JCA 接続がなくなると、そのルール・セットにガーベッジ・コレクションを実行できます。 このデフォルトの動作によって、使用されていないルール・セットがメモリーから削除されることが保証されます。 ルール・セットが別のルール・セットと比較してほとんど使用されない場合は、再解析が行われる可能性があります。

ruleset.cache.maintenance.period プロパティーは、 デフォルトで 300 に設定されています。この値は、保守スレッドが 5 分ごとに実行されることを意味しています。

ra.xml XU 構成ファイルに他の実装クラスをパッケージ化することで、ルール・セット・キャッシュの独自の実装を作成することができます。

手順

  1. IlrRulesetCache インターフェースを実装するクラスを作成します。 クラスでカスタム・プロパティーを定義することが可能です。
    package ilog.rules.res.xu.ruleset.cache.sample;
    
    import java.util.*;
    import java.util.logging.*;
    
    import ilog.rules.res.xu.ruleset.*;
    
    public class RulesetCacheImpl implements IlrRulesetCache {
    	public static final int DEFAULT_MAXIMUM_SIZE = 2;
    
    	protected IlrRulesetUsageInformationMonitor monitor;
    	protected Logger logger;
    	protected ArrayList<IlrXURuleset> rulesets = new ArrayList<IlrXURuleset>();
    
    	/** Maximum number of rulesets that can be stored in the cache */
    	protected int maxSize = DEFAULT_MAXIMUM_SIZE;
    
    	public synchronized void addRuleset(IlrXURuleset ruleset) {
    		if (rulesets.size() == maxSize) {
    			logger.info("Max size reached, removing less used ruleset");
    			removeLessUsedRuleset();
    		}
    		rulesets.add(ruleset);
    	}
    
    
    	/**
    	 * Removes the least executed ruleset.
    	 */
    	protected void removeLessUsedRuleset() {
    		long minExecCount = Long.MAX_VALUE;
    		IlrXURuleset minExecRuleset = null;
    
    		for(IlrXURuleset ruleset: rulesets) {
    			IlrRulesetUsageInformation info
    				= monitor.getRulesetUsageInformation(ruleset.getCanonicalRulesetPath());
    			long execCounter = info.getExecutionCount();
    
    			if (execCounter <= minExecCount) {
    				minExecRuleset = ruleset;
    				minExecCount = execCounter;
    			}
    		}
    
    		if (minExecRuleset != null) {
    			logger.info("Remove ruleset: "+minExecRuleset.getCanonicalRulesetPath());
    			rulesets.remove(minExecRuleset);
    		}
    	}
    
    	public synchronized void clear() {
    		rulesets.clear();
    	}
    
    	public synchronized IlrXURuleset getDeprecatedRuleset(String canonicalRulesetPath,
    			ClassLoader xomClassLoader) {
    		return null;
    	}
    
    	public synchronized IlrXURuleset getRuleset(String canonicalRulesetPath,
    			ClassLoader xomClassLoader) {
    		for(IlrXURuleset ruleset: rulesets) {
    			if (ruleset.getCanonicalRulesetPath().equals(canonicalRulesetPath) &&
    					ruleset.getXOMClassLoader() == xomClassLoader) {
    				return ruleset;
    			}
    		}
    
    		return null;
    	}
    
    	public void initialize(Logger logger, Map<String, String> properties,
    			IlrRulesetUsageInformationMonitor monitor)
    			throws IlrRulesetCacheException {
    		this.monitor = monitor;
    		this.logger = logger;
    	}
    
    	public synchronized void rulesetChanged(String canonicalRulesetPath) {
    		Iterator<IlrXURuleset> iterator = rulesets.iterator();
    		while(iterator.hasNext()) {
    			IlrXURuleset ruleset = iterator.next();
    			if (ruleset.getCanonicalRulesetPath().equals(canonicalRulesetPath)) {
    				iterator.remove();
    			}
    		}
    	}
    
    }
     
  2. 実装クラスを ra.xml XU 配布記述子に追加します。
    1. カスタム・ルール・セット・キャッシュを実装します。
      <config-property>
         <config-property-name>rulesetCacheProperties</config-property-name>
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>ruleset.cache.class=mypackage.MyRulesetCache</config-property-value>
      </config-property>
    2. カスタム・キャッシュ・プロパティーを設定します。
      <config-property>
         <config-property-name>rulesetCacheProperties</config-property-name>
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>ruleset.cache.class=mypackage.MyRulesetCache,mycustomprop=mycustomvalue/config-property-value>
      </config-property>
    3. rulesetUsageMonitorEnabled プロパティーが true (デフォルト値) に設定されていることを確認します。
      <config-property>
         <config-property-name>rulesetUsageMonitorEnabled</config-property-name>
         <config-property-type>java.lang.Boolean</config-property-type> 
         <config-property-value>true</config-property-value>
      </config-property>
  3. このクラスをパッケージ化します。

    Java SE 配布の場合は、このクラスを XU またはその親と同じクラスパスに追加する必要があります。 Java EE の場合は、このクラスを XU .rar ファイル内にパッケージ化する必要があります。