66.2. ランタイムマネージャー
RuntimeManager
クラスは、プロセスエンジン API でレイヤーを提供し、その使用方法を単純化し、強化します。このクラスは、KIE ベースおよび KIE セッションと、プロセス内のすべてのタスクにハンドラーを提供するタスクサービスをカプセル化および管理します。ランタイムマネージャーの KIE セッションとタスクサービスが相互に機能するように設定されているため、このような設定を指定する必要はありません。たとえば、ヒューマンタスクハンドラーを登録し、必要なサービスに接続されていることを確認する必要はありません。
ランタイムマネージャーは、事前定義されたストラテジーに従って KIE セッションを管理します。以下のストラテジーを利用できます。
-
シングルトン: ランタイムマネージャーは単一の
KieSession
を維持し、要求されたすべてのプロセスに使用します。 -
リクエストごと: ランタイムマネージャーはリクエストごとに新しい
KieSession
を作成します。 -
プロセスインスタンスごと: ランタイムマネージャーはプロセスインスタンスと
KieSession
の間のマッピングを維持し、指定のプロセスインスタンスを使用するたびに常に同じKieSession
を提供します。
ストラテジーに関係なく、RuntimeManager
クラスはプロセスエンジンコンポーネントの初期化と設定で同じ機能を保証します。
-
KieSession
インスタンスは同じファクトリーで読み込まれます (メモリーまたは JPA ベース)。 -
ワークアイテムハンドラーは、すべての
KieSession
インスタンスに登録されます (データベースから読み込むか、新規に作成されているかのいずれか)。 -
イベントリスナー (
Process
、Agenda
、WorkingMemory
) は、データベースからセッションが読み込まれるか、新たに作成されるかに関係なく、すべての KIE セッションに登録されます。 タスクサービスは、以下の必須コンポーネントで設定されます。
- JTA トランザクションマネージャー
-
KieSession
インスタンスに使用されるものと同じエンティティーマネージャーファクトリー -
環境で設定できる
UserGroupCallback
インスタンス
ランタイムマネージャーは、プロセスエンジンを正常に破棄することもできます。これは、必要がなくなった場合に RuntimeEngine
インスタンスを破棄する専用のメソッドを提供し、取得した可能性のあるリソースを解放します。
以下のコードは、RuntimeManager
インターフェイスの定義を示しています。
RuntimeManager
インターフェイスの定義
public interface RuntimeManager { /** * Returns a <code>RuntimeEngine</code> instance that is fully initialized: * <ul> * <li>KieSession is created or loaded depending on the strategy</li> * <li>TaskService is initialized and attached to the KIE session (through a listener)</li> * <li>WorkItemHandlers are initialized and registered on the KIE session</li> * <li>EventListeners (process, agenda, working memory) are initialized and added to the KIE session</li> * </ul> * @param context the concrete implementation of the context that is supported by given <code>RuntimeManager</code> * @return instance of the <code>RuntimeEngine</code> */ RuntimeEngine getRuntimeEngine(Context<?> context); /** * Unique identifier of the <code>RuntimeManager</code> * @return */ String getIdentifier(); /** * Disposes <code>RuntimeEngine</code> and notifies all listeners about that fact. * This method should always be used to dispose <code>RuntimeEngine</code> that is not needed * anymore. <br/> * Do not use KieSession.dispose() used with RuntimeManager as it will break the internal * mechanisms of the manager responsible for clear and efficient disposal.<br/> * Disposing is not needed if <code>RuntimeEngine</code> was obtained within an active JTA transaction, * if the getRuntimeEngine method was invoked during active JTA transaction, then disposing of * the runtime engine will happen automatically on transaction completion. * @param runtime */ void disposeRuntimeEngine(RuntimeEngine runtime); /** * Closes <code>RuntimeManager</code> and releases its resources. Call this method when * a runtime manager is not needed anymore. Otherwise it will still be active and operational. */ void close(); }
RuntimeManager
クラスは、基礎となるプロセスエンジンコンポーネントへのアクセスを取得するメソッドが含まれる RuntimeEngine
クラスも提供します。
RuntimeEngine
インターフェイスの定義
public interface RuntimeEngine { /** * Returns the <code>KieSession</code> configured for this <code>RuntimeEngine</code> * @return */ KieSession getKieSession(); /** * Returns the <code>TaskService</code> configured for this <code>RuntimeEngine</code> * @return */ TaskService getTaskService(); }
RuntimeManager
クラスの識別子は、ランタイムの実行中に deploymentId
として使用されます。たとえば、識別子は、Task
が永続化される Task
の deploymentId
として永続化されています。Task
の deploymentID
は、Task
が完了してプロセスインスタンスを再開する際に、これを RuntimeManager
に関連付けます。
同じ deploymentId
も履歴ログテーブルの externalId
として永続化されます。
RuntimeManager
インスタンスの作成時に識別子を指定しない場合は、ストラテジーに応じてデフォルト値が適用されます (例: PerProcessInstanceRuntimeManager
の default-per-pinstance
)。つまり、アプリケーションはライフサイクル全体で RuntimeManager
クラスと同じデプロイメントを使用することを意味します。
アプリケーションで複数のランタイムマネージャーを維持する場合は、すべての RuntimeManager
インスタンスに一意の ID を指定する必要があります。
たとえば、デプロイメントサービスは複数のランタイムマネージャーを維持し、KJAR ファイルの GAV 値を識別子として使用します。Business Central と KIE Server でも、デプロイメントサービスに依存するため、同じロジックが使用されます。
ハンドラーまたはリスナー内からプロセスエンジンまたはタスクサービスと対話する必要がある場合は、RuntimeManager
インターフェイスを使用して、指定のプロセスインスタンスの RuntimeEngine
インスタンスを取得し、RuntimeEngine
インスタンスを使用して KieSession
インスタンスまたは TaskService
インスタンスを取得します。このアプローチにより、選択したストラテジーに従って管理されるエンジンの正常な状態が維持されます。
66.2.1. ランタイムマネージャーストラテジー
RuntimeManager
クラスは、KIE セッションを管理するための以下のストラテジーをサポートします。
- シングルトンストラテジー
このストラテジーは、単一の
RuntimeEngine
インスタンスを維持するようにランタイムマネージャーに指示します (これにより、単一のKieSession
インスタンスおよびTaskService
インスタンス)。ランタイムエンジンへのアクセスは同期されるためスレッドセーフになりますが、同期によりパフォーマンスの低下が発生します。このストラテジーは、簡単なユースケースに使用します。
このストラテジーには以下の特徴があります。
- ランタイムエンジンのインスタンスおよびタスクサービスが 1 つのインスタンスを持つメモリーフットプリントは小さくなります。
- 設計と使用はシンプルでコンパクトです。
- アクセスが同期されているため、プロセスエンジンの低から中程度の負荷に適しています。
-
このストラテジーでは、単一の
KieSession
インスタンスが原因で、(ファクトなど) すべての状態オブジェクトがすべてのプロセスインスタンスに直接表示され、その逆も同様です。 -
ストラテジーはコンテキストではありません。シングルトン
RuntimeManager
からRuntimeEngine
のインスタンスを取得する場合は、Context
インスタンスを考慮する必要はありません。通常は、EmptyContext.get()
をコンテキストとして使用できますが、null 引数も受け入れ可能です。 このストラテジーでは、ランタイムマネージャーは
KieSession
の ID を追跡するため、RuntimeManager
の再起動後も同じセッションが使用されたままになります。ID はファイルシステムの一時的な場所にシリアライズファイルとして保存され、環境によっては以下のディレクトリーのいずれかになります。-
jbpm.data.dir
システムプロパティーの値 -
jboss.server.data.dir
システムプロパティーの値 -
java.io.tmpdir
システムプロパティーの値
-
警告Singleton ストラテジーと EJB Timer スケジューラーの組み合わせにより、負荷がかかった状態で Hibernate の問題が発生する可能性があります。この組み合わせは、実稼働アプリケーションで使用しないでください。EJB Timer スケジューラーは、KIE Server のデフォルトスケジューラーです。
- リクエスト別のストラテジー
このストラテジーは、ランタイムマネージャーに対し、リクエストごとに
RuntimeEngine
の新しいインスタンスを提供するように指示します。1 つのトランザクション内でのプロセスエンジンを 1 つ以上呼び出すことは、1 つのリクエストとみなされます。状態が正確になるように、単一のトランザクション内で
RuntimeEngine
の同じインスタンスを使用する必要があります。それ以外の場合は、1 つの呼び出しで完了した操作が次の呼び出しで表示されません。プロセスの状態は要求内でのみ保持されるため、このストラテジーはステートレスです。要求が完了すると、
RuntimeEngine
インスタンスは永続的に破棄されます。永続性を使用する場合は、KIE セッションに関連する情報は永続データベースから削除されます。このストラテジーには以下の特徴があります。
- すべてのリクエストに対して完全に分離されたプロセスエンジンとタスクサービス操作を提供します。
- ファクトは要求期間中のみ保存されるため、完全にステートレスになります。
- これは、高負荷のステートレスプロセスに適しています。この場合、リクエスト間でファクトやタイマーを保持する必要はありません。
- このストラテジーでは、KIE セッションは要求のライフサイクル中にのみ利用でき、要求の最後に破棄されます。
-
ストラテジーはコンテキストではありません。リクエストごとの
RuntimeManager
からRuntimeEngine
のインスタンスを取得する場合は、Context
インスタンスを考慮する必要はありません。通常は、EmptyContext.get()
をコンテキストとして使用できますが、null 引数も受け入れ可能です。
- プロセスインスタンスごとのストラテジー
このストラテジーは、KIE セッションとプロセスインスタンス間の厳密な関係を維持するように
RuntimeManager
に指示します。各KieSession
は、それが属するProcessInstance
がアクティブである限り利用できます。このストラテジーは、ルール評価やプロセスインスタンス間の分離など、プロセスエンジンの高度な機能を使用する最も柔軟なアプローチを提供します。これにより、パフォーマンスを最大化し、同期によって生じる潜在的なボトルネックを減らします。同時に、要求ストラテジーとは異なり、KIE セッションの数は、要求の合計数ではなく、実際のプロセスインスタンス数に減らされます。
このストラテジーには以下の特徴があります。
- すべてのプロセスインスタンスに分離を提供します。
-
これは、
KieSession
とProcessInstance
の間の厳密な関係を維持し、指定のProcessInstance
に常に同じKieSession
を提供するようにします。 -
これは、
KieSession
のライフサイクルをProcessInstance
にマージし、プロセスインスタンスの完了時または中止時に両方破棄されます。 - これにより、プロセスインスタンスの範囲内でファクトやタイマーなどのデータのメンテナーンスが可能になります。プロセスインスタンスのみがデータにアクセスできます。
-
プロセスインスタンスの
KieSession
を検索して読み込む必要があるため、オーバーヘッドが発生します。 -
KieSession
の使用状況をすべて検証し、他のプロセスインスタンスには使用できません。別のプロセスインスタンスが同じKieSession
を使用する場合は、例外が発生します。 ストラテジーはコンテキストであり、以下のコンテキストインスタンスを許可します。
-
EmptyContext
または null: プロセスインスタンス ID が利用できないため、プロセスインスタンスを開始するときに使用 -
ProcessInstanceIdContext
: プロセスインスタンスの作成後に使用します。 -
CorrelationKeyContext
:ProcessInstanceIdContext
の代わりに、プロセスインスタンス ID の代わりにカスタム (business) キーを使用します。
-
66.2.2. ランタイムマネージャーの典型的な使用シナリオ
ランタイムマネージャーの典型的な使用シナリオは、以下の段階で設定されます。
アプリケーションの起動時に、以下の段階を完了します。
-
RuntimeManager
インスタンスをビルドし、アプリケーションの有効期間全体を維持します。これはスレッドセーフで、同時にアクセスできます。
-
要求時に、以下のステージを完了します。
-
RuntimeManager
クラスに設定したストラテジーによって決定される適切なコンテキストインスタンスを使用して、RuntimeManager
からRuntimeEngine
を取得します。 -
RuntimeEngine
からKieSession
オブジェクトおよびTaskService
オブジェクトを取得します。 -
startProcess
、completeTask
などの操作には、KieSession
オブジェクトおよびTaskService
オブジェクトを使用します。 -
処理が完了したら、
RuntimeManager.disposeRuntimeEngine
メソッドを使用してRuntimeEngine
を破棄します。
-
アプリケーションのシャットダウン時に、以下の段階を完了します。
-
RuntimeManager
インスタンスを閉じます。
-
アクティブな JTA トランザクションで RuntimeManager
から RuntimeEngine
を取得した場合は、トランザクションの完了時に、(完了ステータスが commit または rollback に関わらず) RuntimeManager
が自動的に RuntimeEngine
を破棄するため、最後に RuntimeEngine
を破棄する必要はありません。
以下の例は、RuntimeManager
インスタンスをビルドし、そこから (KieSession
クラスおよび TaskService
クラスをカプセル化する) RuntimeEngine
インスタンスを取得する方法を示しています 。
RuntimeManager
インスタンスをビルドして、RuntimeEngine
および KieSession
を取得します。
// First, configure the environment to be used by RuntimeManager RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get() .newDefaultInMemoryBuilder() .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2) .get(); // Next, create the RuntimeManager - in this case the singleton strategy is chosen RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment); // Then get RuntimeEngine from the runtime manager, using an empty context because singleton does not keep track // of runtime engine as there is only one RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get()); // Get the KieSession from the RuntimeEngine - already initialized with all handlers, listeners, and other requirements // configured on the environment KieSession ksession = runtimeEngine.getKieSession(); // Add invocations of the process engine here, // for example, ksession.startProcess(processId); // Finally, dispose the runtime engine manager.disposeRuntimeEngine(runtimeEngine);
この例では、RuntimeManager
クラスおよび RuntimeEngine
クラスを使用する最も簡単な、または最小限の方法を提供します。これには、以下の特徴があります。
-
KieSession
インスタンスは、newDefaultInMemoryBuilder
ビルダーを使用してメモリーに作成されます。 - アセットとして追加される 1 つのプロセスが実行に利用できます。
-
TaskService
クラスはLocalHTWorkItemHandler
インターフェイスを介して設定されてKieSession
インスタンスに割り当てられ、プロセス内でユーザータスク機能をサポートします。
66.2.3. ランタイム環境設定オブジェクト
RuntimeManager
クラスは、ハンドラーの作成、破棄、登録など、内部プロセスエンジンの複雑さをカプセル化します。
また、プロセスエンジンの設定を詳細に制御することもできます。この設定を設定するには、RuntimeEnvironment
オブジェクトを作成してから、RuntimeManager
オブジェクトを作成する必要があります。
次の定義は、RuntimeEnvironment
インターフェイスで利用可能なメソッドを示しています。
RuntimeEnvironment
インターフェイスのメソッド
public interface RuntimeEnvironment { /** * Returns <code>KieBase</code> that is to be used by the manager * @return */ KieBase getKieBase(); /** * KieSession environment that is to be used to create instances of <code>KieSession</code> * @return */ Environment getEnvironment(); /** * KieSession configuration that is to be used to create instances of <code>KieSession</code> * @return */ KieSessionConfiguration getConfiguration(); /** * Indicates if persistence is to be used for the KieSession instances * @return */ boolean usePersistence(); /** * Delivers a concrete implementation of <code>RegisterableItemsFactory</code> to obtain handlers and listeners * that is to be registered on instances of <code>KieSession</code> * @return */ RegisterableItemsFactory getRegisterableItemsFactory(); /** * Delivers a concrete implementation of <code>UserGroupCallback</code> that is to be registered on instances * of <code>TaskService</code> for managing users and groups. * @return */ UserGroupCallback getUserGroupCallback(); /** * Delivers a custom class loader that is to be used by the process engine and task service instances * @return */ ClassLoader getClassLoader(); /** * Closes the environment, permitting closing of all dependent components such as ksession factories */ void close();
66.2.4. ランタイム環境ビルダー
必要なデータが含まれる RuntimeEnvironment
のインスタンスを作成するには、RuntimeEnvironmentBuilder
クラスを使用します。このクラスは Fluent API を提供し、事前定義された設定で RuntimeEnvironment
インスタンスを設定します。
次の定義は、RuntimeEnvironmentBuilder
インターフェイスのメソッドを示しています。
RuntimeEnvironmentBuilder
インターフェイスのメソッド
public interface RuntimeEnvironmentBuilder { public RuntimeEnvironmentBuilder persistence(boolean persistenceEnabled); public RuntimeEnvironmentBuilder entityManagerFactory(Object emf); public RuntimeEnvironmentBuilder addAsset(Resource asset, ResourceType type); public RuntimeEnvironmentBuilder addEnvironmentEntry(String name, Object value); public RuntimeEnvironmentBuilder addConfiguration(String name, String value); public RuntimeEnvironmentBuilder knowledgeBase(KieBase kbase); public RuntimeEnvironmentBuilder userGroupCallback(UserGroupCallback callback); public RuntimeEnvironmentBuilder registerableItemsFactory(RegisterableItemsFactory factory); public RuntimeEnvironment get(); public RuntimeEnvironmentBuilder classLoader(ClassLoader cl); public RuntimeEnvironmentBuilder schedulerService(Object globalScheduler);
RuntimeEnvironmentBuilderFactory
クラスを使用して RuntimeEnvironmentBuilder
のインスタンスを取得します。設定のない空のインスタンスとともに、ランタイムマネージャーの設定オプションがいくつか事前設定されたビルダーを取得できます。
次の定義は、RuntimeEnvironmentBuilderFactory
インターフェイスのメソッドを示しています。
RuntimeEnvironmentBuilderFactory
インターフェイスのメソッド
public interface RuntimeEnvironmentBuilderFactory { /** * Provides a completely empty <code>RuntimeEnvironmentBuilder</code> instance to manually * set all required components instead of relying on any defaults. * @return new instance of <code>RuntimeEnvironmentBuilder</code> */ public RuntimeEnvironmentBuilder newEmptyBuilder(); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultBuilder(); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * but does not have persistence for the process engine configured so it will only store process instances in memory * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultInMemoryBuilder(); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * This method is tailored to work smoothly with KJAR files * @param groupId group id of kjar * @param artifactId artifact id of kjar * @param version version number of kjar * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultBuilder(String groupId, String artifactId, String version); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * This method is tailored to work smoothly with KJAR files and use the kbase and ksession settings in the KJAR * @param groupId group id of kjar * @param artifactId artifact id of kjar * @param version version number of kjar * @param kbaseName name of the kbase defined in kmodule.xml stored in kjar * @param ksessionName name of the ksession define in kmodule.xml stored in kjar * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultBuilder(String groupId, String artifactId, String version, String kbaseName, String ksessionName); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * This method is tailored to work smoothly with KJAR files and use the release ID defined in the KJAR * @param releaseId <code>ReleaseId</code> that described the kjar * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultBuilder(ReleaseId releaseId); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * This method is tailored to work smoothly with KJAR files and use the kbase, ksession, and release ID settings in the KJAR * @param releaseId <code>ReleaseId</code> that described the kjar * @param kbaseName name of the kbase defined in kmodule.xml stored in kjar * @param ksessionName name of the ksession define in kmodule.xml stored in kjar * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newDefaultBuilder(ReleaseId releaseId, String kbaseName, String ksessionName); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * It relies on KieClasspathContainer that requires the presence of kmodule.xml in the META-INF folder which * defines the kjar itself. * Expects to use default kbase and ksession from kmodule. * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newClasspathKmoduleDefaultBuilder(); /** * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on: * <ul> * <li>DefaultRuntimeEnvironment</li> * </ul> * It relies on KieClasspathContainer that requires the presence of kmodule.xml in the META-INF folder which * defines the kjar itself. * @param kbaseName name of the kbase defined in kmodule.xml * @param ksessionName name of the ksession define in kmodule.xml * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults * * @see DefaultRuntimeEnvironment */ public RuntimeEnvironmentBuilder newClasspathKmoduleDefaultBuilder(String kbaseName, String ksessionName);
ランタイムマネージャーは、KIE セッションと通信するように設定された RuntimeEngine
オブジェクトの統合コンポーネントとして TaskService
オブジェクトへのアクセスも提供します。デフォルトビルダーのいずれかを使用する場合は、タスクサービスの以下の設定が表示されます。
-
永続ユニット名は
org.jbpm.persistence.jpa
(プロセスエンジンとタスクサービスの両方) に設定されます。 - ヒューマンタスクハンドラーは KIE セッションに登録されます。
- JPA ベースの履歴ログイベントリスナーは KIE セッションに登録されます。
-
ルールタスク評価をトリガーするイベントリスナー (
fireAllRules
) が KIE セッションに登録されます。
66.2.5. ランタイムエンジンのハンドラーおよびリスナーの登録
ランタイムマネージャー API を使用する場合、ランタイムエンジンオブジェクトはプロセスエンジンを表します。
独自のハンドラーまたはリスナーを使用してランタイムエンジンを拡張するには、RegisterableItemsFactory
インターフェイスを実装し、RuntimeEnvironmentBuilder.registerableItemsFactory()
メソッドを使用してランタイム環境に追加します。次に、ランタイムマネージャーは、ハンドラーまたはリスナーを、作成するすべてのランタイムエンジンに自動的に追加します。
以下の定義は、RegisterableItemsFactory
インターフェイスのメソッドを示しています。
RegisterableItemsFactory
インターフェイスのメソッド
/** * Returns new instances of <code>WorkItemHandler</code> that will be registered on <code>RuntimeEngine</code> * @param runtime provides <code>RuntimeEngine</code> in case handler need to make use of it internally * @return map of handlers to be registered - in case of no handlers empty map shall be returned. */ Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime); /** * Returns new instances of <code>ProcessEventListener</code> that will be registered on <code>RuntimeEngine</code> * @param runtime provides <code>RuntimeEngine</code> in case listeners need to make use of it internally * @return list of listeners to be registered - in case of no listeners empty list shall be returned. */ List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime); /** * Returns new instances of <code>AgendaEventListener</code> that will be registered on <code>RuntimeEngine</code> * @param runtime provides <code>RuntimeEngine</code> in case listeners need to make use of it internally * @return list of listeners to be registered - in case of no listeners empty list shall be returned. */ List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime); /** * Returns new instances of <code>WorkingMemoryEventListener</code> that will be registered on <code>RuntimeEngine</code> * @param runtime provides <code>RuntimeEngine</code> in case listeners need to make use of it internally * @return list of listeners to be registered - in case of no listeners empty list shall be returned. */ List<WorkingMemoryEventListener> getWorkingMemoryEventListeners(RuntimeEngine runtime);
プロセスエンジンは、RegisterableItemsFactory
のデフォルト実装を提供します。これらの実装を拡張して、カスタムハンドラーおよびリスナーを定義できます。
利用可能な実装は役に立ちます。
-
org.jbpm.runtime.manager.impl.SimpleRegisterableItemsFactory
: 最も単純な実装。事前定義されたコンテンツがなく、リフレクションを使用して指定のクラス名に基づいてハンドラーおよびリスナーのインスタンスを生成します。 -
org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory
: Simple 実装の拡張。この拡張により、デフォルトのランタイム環境ビルダーと同じデフォルトが導入され、引き続き Simple 実装と同じ機能を提供します。 -
org.jbpm.runtime.manager.impl.cdi.InjectableRegisterableItemsFactory
: CDI 環境向けに調整され、プロデューサーを使用してハンドラーとリスナーを検索する CDI スタイルのアプローチを提供するデフォルト実装の拡張。
66.2.5.1. ファイルを使用したワークアイテムハンドラーの登録
CustomWorkItem.conf
ファイルでファイルを定義し、クラスパスにファイルを配置して、ステートレスまたは KieSession
の状態に依存する簡単なワークアイテムハンドラーを登録することができます。
手順
-
クラスパスのルートの
META-INF
サブディレクトリーにdrools.session.conf
という名前のファイルを作成します。Web アプリケーションの場合、ディレクトリーはWEB-INF/classes/META-INF
になります。 以下の行を
drools.session.conf
ファイルに追加します。drools.workItemHandlers = CustomWorkItemHandlers.conf
-
同じディレクトリーに
CustomWorkItemHandlers.conf
という名前のファイルを作成します。 CustomWorkItemHandlers.conf
ファイルで、以下の例のように MVEL スタイルを使用してカスタムのワークアイテムハンドラーを定義します。[ "Log": new org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler(), "WebService": new org.jbpm.process.workitem.webservice.WebServiceWorkItemHandler(ksession), "Rest": new org.jbpm.process.workitem.rest.RESTWorkItemHandler(), "Service Task" : new org.jbpm.process.workitem.bpmn2.ServiceTaskHandler(ksession) ]
結果
リストしたワークアイテムハンドラーは、アプリケーションがランタイムマネージャー API を使用しているかどうかに関係なく、アプリケーションによって作成されたすべての KIE セッションに登録されます。
66.2.5.2. CDI 環境におけるハンドラーおよびリスナーの登録
アプリケーションがランタイムマネージャー API を使用して、CDI 環境で実行される場合、クラスは専用のプロデューサーインターフェイスを実装し、カスタムワークアイテムハンドラーおよびイベントリスナーをすべてのランタイムエンジンに提供できます。
ワークアイテムハンドラーを作成するには、WorkItemHandlerProducer
インターフェイスを実装する必要があります。
WorkItemHandlerProducer
インターフェイスの定義
public interface WorkItemHandlerProducer { /** * Returns a map of work items (key = work item name, value= work item handler instance) * to be registered on the KieSession * <br/> * The following parameters are accepted: * <ul> * <li>ksession</li> * <li>taskService</li> * <li>runtimeManager</li> * </ul> * * @param identifier - identifier of the owner - usually RuntimeManager that allows the producer to filter out * and provide valid instances for given owner * @param params - the owner might provide some parameters, usually KieSession, TaskService, RuntimeManager instances * @return map of work item handler instances (recommendation is to always return new instances when this method is invoked) */ Map<String, WorkItemHandler> getWorkItemHandlers(String identifier, Map<String, Object> params); }
イベントリスナーを作成するには、EventListenerProducer
インターフェイスを実装する必要があります。イベントリスナープロデューサーに適切な修飾子にアノテーションを付け、提供されるリスナーのタイプを示します。以下のアノテーションのいずれかを使用します。
-
ProcessEventListener
の@Process
-
AgendaEventListener
の@Agenda
-
WorkingMemoryEventListener
の場合は@WorkingMemory
EventListenerProducer
インターフェイスの定義
public interface EventListenerProducer<T> { /** * Returns a list of instances for given (T) type of listeners * <br/> * The following parameters are accepted: * <ul> * <li>ksession</li> * <li>taskService</li> * <li>runtimeManager</li> * </ul> * @param identifier - identifier of the owner - usually RuntimeManager that allows the producer to filter out * and provide valid instances for given owner * @param params - the owner might provide some parameters, usually KieSession, TaskService, RuntimeManager instances * @return list of listener instances (recommendation is to always return new instances when this method is invoked) */ List<T> getEventListeners(String identifier, Map<String, Object> params); }
META-INF
サブディレクトリーに beans.xml
を追加し、これらのインターフェイスの実装を Bean アーカイブとしてパッケージ化します。アプリケーションクラスパスに Bean アーカイブを配置します (例: Web アプリケーションの WEB-INF/lib
)。CDI ベースのランタイムマネージャーはパッケージを検出し、データストアから作成または読み込みを行うすべての KieSession
でワークアイテムハンドラーおよびイベントリスナーを登録します。
プロセスエンジンは、ステートフルで高度な操作を有効にするために、特定のパラメーターをプロデューサーに提供します。たとえば、ハンドラーまたはリスナーはパラメーターを使用して、エラーが発生した場合にプロセスエンジンまたはプロセスインスタンスに通知できます。プロセスエンジンは、以下のコンポーネントをパラメーターとして提供します。
-
KieSession
-
TaskService
-
RuntimeManager
さらに、RuntimeManager
クラスインスタンスの識別子はパラメーターとして提供されます。フィルターを識別子に適用して、この RuntimeManager
インスタンスがハンドラーとリスナーを受け取るかどうかを決定することができます。