jbpm備忘録1 永続化
最近jBPMを使うことが増えたので、忘れないうちに色々まとめておこうと思う。
jBPMは複数のプロセス(フロー)がプロセス単位に動作する一種の状態マシンのようなもので、待機状態→実行状態→待機状態のようなサイクルで動作する。
このサイクル中で待機状態となった時にDBなんかにプロセスの状態を書き出すことを永続化といい、javaで記述すると以下のようになる。 saveの部分が永続化処理となるようだが、当然実際の永続化が行われるのはトランザクションをcommitしたタイミングとなるようだ。
以下に示したソースで考えると、JbpmContextを作ったタイミングでトランザクションが開始され、JbpmContextをcloseしたタイミングでトランザクションが終了すると考えるとわかりやすいだろうか、、(この理解であっているかはあまり詳しくソースを調べてないので不明)
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); try { // ここに処理を記述する。 jbpmContext.save(processInstance); // 永続化処理 finally { jbpmContext.close(); // トランザクションの修了 }
ただ、実際に、closeメソッドの中をのぞくと以下のような実装となっている。autoSaveの部分でJbpmContext内部で保持している、autoSaveProcessInstances(ProcessInstanceのSet)を順次saveしている。
続いて、closeServicesでserviceのclose処理を実施している。
public void close() { if (isClosed) return; RuntimeException saveException = autoSave(); RuntimeException serviceException = closeServices(); isClosed = true; if (saveException != null) throw saveException; if (serviceException != null) throw serviceException; }
autoSaveの実装
private RuntimeException autoSave() { if (autoSaveProcessInstances != null) { try { for (Iterator iter = autoSaveProcessInstances.iterator(); iter.hasNext();) { ProcessInstance processInstance = (ProcessInstance) iter.next(); save(processInstance); } } catch (RuntimeException e) { return e; } } return null; }
closeServicesの実装
private RuntimeException closeServices() { try { services.close(); return null; } catch (RuntimeException e) { return e; } finally { JbpmConfiguration configuration = getJbpmConfiguration(); if (configuration != null) configuration.popJbpmContext(this); } }