<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    零全零美(www.zzgwt.com)
    生活中的很多事情,并不像If...Else那么簡單!
    posts - 96,comments - 52,trackbacks - 0
           前面我有一篇《JBPM源碼解讀之:Fork》,大致分析了JBPM對于Fork的實(shí)現(xiàn)方式,其實(shí)Fork和Join是不可分割的一對,F(xiàn)ork實(shí)現(xiàn)分拆,Join實(shí)現(xiàn)匯集。先讓我們看一下《JBPM 3.2.3 User Guide》中關(guān)于Join的描述:
     The default join assumes that all tokens that arrive in the join are children of the same parent. This situation is created when using the fork as mentioned above and when all tokens created by a fork arrive in the same join. A join will end every token that enters the join. Then the join will examine the parent-child relation of the token that enters the join. When all sibling tokens have arrived in the join, the parent token will be propagated over the (unique!) leaving transition. When there are still sibling tokens active, the join will behave as a wait state.
         下面就讓我們從JBPM源碼的角度分析一下Join是如何關(guān)閉每一個到達(dá)它的Token,是如何檢查各個子Token與父Token的父子關(guān)系,又是如何重新激活父Token實(shí)現(xiàn)流程的繼續(xù)流轉(zhuǎn),更重要的也是我寫這篇文章的原因:我們?nèi)绾文軓腏oin默認(rèn)的實(shí)現(xiàn)方式中得到啟發(fā),讓我們能夠更好的理解JBPM整個運(yùn)轉(zhuǎn)流程,更好的駕馭整個項(xiàng)目。

     我們來看Join類的成員變量:

     1    /**
     2     * specifies wether what type of hibernate lock should be acquired. null
     3     * value defaults to LockMode.Force
     4     */

     5    String parentLockMode;
     6
     7    /**
     8     * specifies if this joinhandler is a discriminator. a descriminator
     9     * reactivates the parent when the first concurrent token enters the join.
    10     * 注:
    11     */

    12    boolean isDiscriminator = false;
    13
    14    /**
    15     * a fixed set of concurrent tokens.
    16     */

    17    Collection tokenNames = null;
    18
    19    /**
    20     * a script that calculates concurrent tokens at runtime.
    21     */

    22    Script script = null;
    23
    24    /**
    25     * reactivate the parent if the n-th token arrives in the join.
    26     */

    27    int nOutOfM = -1;

          parentLockMode用于控制Hibernate的鎖機(jī)制,這點(diǎn)我們暫且不去深究,有意思的是下面幾個變量:isDiscriminator、tokenNames、script、nOutOfM共同決定了Join節(jié)點(diǎn)內(nèi)被的具體行為,本文稍后會具體解說。

    1public void read(Element element, JpdlXmlReader jpdlReader) {
    2        String lock = element.attributeValue("lock");
    3        if ((lock != null&& (lock.equalsIgnoreCase("pessimistic"))) {
    4            parentLockMode = LockMode.UPGRADE.toString();
    5        }

    6    }

           Join的read方法很簡單,只是起到了讀取JPDL中對lock的配置,并沒有關(guān)心其他成員變量的初始化,這也就直接說明了isDiscriminator、tokenNames、script、nOutOfM這幾個變量均屬于運(yùn)行期,也就是我們沒有辦法在配置文件里頭像Fork一樣配置Script,就算配了Join也不認(rèn)。
     execute(ExecutionContext executionContext)方法是每個繼承自Node的類的核心方法,Join類也是在這個方法中實(shí)現(xiàn)了Join的控制機(jī)制。在解讀這個方法之前必須應(yīng)該明白的一件事是:Join的execute方法就像Fork的Node-Leave 事件一樣是會執(zhí)行多次的,同樣這取決于與Join搭配的Fork具體產(chǎn)生了幾個子Token。
     1      //取得Token
     2        Token token = executionContext.getToken();
     3        //得到Token的isAbleToReactivateParent屬性
     4        boolean isAbleToReactivateParent = token.isAbleToReactivateParent();
     5
     6        //如果當(dāng)前Token沒有結(jié)束,則結(jié)束該Token,到這里Fork產(chǎn)生的子Token的生命周期也就結(jié)束了
     7        if (!token.hasEnded()) {
     8            token.end(false);
     9        }

    10        //檢查該Token是否具有激活父Token的能力,如果有方法繼續(xù),如果沒有方法結(jié)束。
    11        if (isAbleToReactivateParent) {
    12            
    13        }

      Join的這種處理方式,讓我們可以方便的實(shí)現(xiàn)一種生活中經(jīng)常用到的抄送機(jī)制,例如:在某個流程中有一個審批的環(huán)節(jié),這個審批的默認(rèn)執(zhí)行人為李副處長,既定情況下,如果這位李副處長審批完畢,流程就應(yīng)該繼續(xù),但是現(xiàn)在王正處長要求所有審批過的文件都要自己親自過目,但是只是過目,王處長的這個“過目”的行為要求并不會影響流程的運(yùn)行,意思就是說,王處長完全有可能在流程都已經(jīng)結(jié)束過了才去過目。稍后我會另有文章具體介紹我的使用Fork+Join實(shí)現(xiàn)抄送的思路。
      繼續(xù),呵呵,接下我們分析的方法都是在if (isAbleToReactivateParent)塊內(nèi)的,剛才我們說如果isAbleToReactivateParent == false那么整個方法就結(jié)束了。
     1            // the token arrived in the join and can only reactivate
     2            // the parent once
     3            token.setAbleToReactivateParent(false);
     4            總感覺這句是多余的,因?yàn)楹孟褚粋€子Token只有一次機(jī)會進(jìn)入Join節(jié)點(diǎn),然后他的生命周期也就結(jié)束了,再者在execute方法的后面有
     5                    Iterator iter = parentToken.getChildren().values().iterator();
     6                    while (iter.hasNext()) {
     7                        ((Token) iter.next()).setAbleToReactivateParent(false);
     8                    }

     9            略去關(guān)于處理Hibernate鎖機(jī)制的代碼,因?yàn)樗⒉恢苯佑绊戇@個流程的運(yùn)作.
    10            
    11                boolean reactivateParent = true;
    12
    13                // if this is a discriminator
    14                if (isDiscriminator) {
    15                    // reactivate the parent when the first token arrives in the
    16                    // join. this must be the first token arriving because
    17                    // otherwise
    18                    // the isAbleToReactivateParent() of this token should have
    19                    // been false
    20                    // above.
    21                    reactivateParent = true;
    22
    23                    // if a fixed set of tokenNames is specified at design
    24                    // time
    25                }
     else if (tokenNames != null{
    26                    // check reactivation on the basis of those tokenNames
    27                    reactivateParent = mustParentBeReactivated(parentToken, tokenNames.iterator());
    28
    29                    // if a script is specified
    30                }
     else if (script != null{
    31
    32                    // check if the script returns a collection or a boolean
    33                    Object result = null;
    34                    try {
    35                        result = script.eval(token);
    36                    }
     catch (Exception e) {
    37                        this.raiseException(e, executionContext);
    38                    }

    39                    // if the result is a collection
    40                    if (result instanceof Collection) {
    41                        // it must be a collection of tokenNames
    42                        Collection runtimeTokenNames = (Collection) result;
    43                        reactivateParent = mustParentBeReactivated(parentToken, runtimeTokenNames.iterator());
    44
    45                        // if it's a boolean
    46                    }
     else if (result instanceof Boolean) {
    47                        // the boolean specifies if the parent needs to be
    48                        // reactivated
    49                        reactivateParent = ((Boolean) result).booleanValue();
    50                    }

    51
    52                    // if a nOutOfM is specified
    53                }
     else if (nOutOfM != -1{
    54
    55                    int n = 0;
    56                    // wheck how many tokens already arrived in the join
    57                    Iterator iter = parentToken.getChildren().values().iterator();
    58                    while (iter.hasNext()) {
    59                        Token concurrentToken = (Token) iter.next();
    60                        if (this.equals(concurrentToken.getNode())) {
    61                            n++;
    62                        }

    63                    }

    64                    if (n < nOutOfM) {
    65                        reactivateParent = false;
    66                    }

    67
    68                    // if no configuration is specified..
    69                }
     else {
    70                    // the default behaviour is to check all concurrent tokens
    71                    // and reactivate
    72                    // the parent if the last token arrives in the join
    73                    reactivateParent = mustParentBeReactivated(parentToken, parentToken.getChildren().keySet().iterator());
    74                }

    75
    76                // if the parent token needs to be reactivated from this join
    77                // node
    78                if (reactivateParent) {
    79
    80                    // write to all child tokens that the parent is already
    81                    // reactivated
    82                    Iterator iter = parentToken.getChildren().values().iterator();
    83                    while (iter.hasNext()) {
    84                        ((Token) iter.next()).setAbleToReactivateParent(false);
    85                    }

    86
    87                    // write to all child tokens that the parent is already
    88                    // reactivated
    89                    ExecutionContext parentContext = new ExecutionContext(parentToken);
    90                    leave(parentContext);
    91                }

    92                

        讀了這段邏輯很少,但是注釋很多的代碼相信已經(jīng)明白了Join內(nèi)部的核心了,有一種豁然開朗的感覺,JBPM的設(shè)計(jì)思想真的很迷人:幾句簡單的If、Else
     就實(shí)現(xiàn)了讓人看來很神秘的功能。這個時候我們可以來分析一下他的幾個成員變量的作用了。
       如果isDiscriminator為True,這個時候Join節(jié)點(diǎn)其實(shí)是起到一種選擇器的作用:當(dāng)?shù)谝粋€子Token到達(dá)Join之后,Join就會馬上取消其他子Token執(zhí)行自身execute方法的能力,而且流程會馬上繼續(xù)而不會再理會其他的子Token有沒有到達(dá)Join或結(jié)束,因?yàn)楦鶕?jù)我們上面的分析,isDiscriminator被設(shè)置為false的子token是不具備激活父Token的能力的。原來實(shí)現(xiàn)網(wǎng)上經(jīng)常爭論的用Join實(shí)現(xiàn)多選一是那么簡單(呵呵)!
       下一個有意思的是nOutOfM,代碼寫的很明白,當(dāng)子Token到達(dá)Join的時候n就加1,如果n<nOutOfM Join就一個處于等待狀態(tài),直到n > nOutOfM 流程馬上繼續(xù),這就Join實(shí)現(xiàn)多選多的機(jī)制,真的很簡單,當(dāng)然如果我們把nOutOfM設(shè)置為1,那么他所起到的作用就跟isDiscriminator一樣了。
       在說明其他兩個變量之前,讓我們先來看一下public boolean mustParentBeReactivated(Token parentToken, Iterator childTokenNameIterator)方法
     1    /**
     2     * 檢查有沒有子Token的isAbleToReactivateParent為真,如果有一個子Token的isAbleToReactivateParent為真,就返回false
     3     */

     4    public boolean mustParentBeReactivated(Token parentToken, Iterator childTokenNameIterator) {
     5        boolean reactivateParent = true;
     6        while ((childTokenNameIterator.hasNext()) && (reactivateParent)) {
     7            String concurrentTokenName = (String) childTokenNameIterator.next();
     8
     9            Token concurrentToken = parentToken.getChild(concurrentTokenName);
    10
    11            if (concurrentToken.isAbleToReactivateParent()) {
    12                log.debug("join will not yet reactivate parent: found concurrent token '" + concurrentToken + "'");
    13                reactivateParent = false;
    14            }

    15        }

    16        return reactivateParent;
    17    }


        isDiscriminator和nOutOfM實(shí)現(xiàn)了多選一和多選多,但是這兩個變量起到的控制作用是死的,也就是我們并不能指定從Fork那里產(chǎn)生的子Token中哪幾個到Join之后流程可繼續(xù)。例如Fork創(chuàng)建了A、B、C、D、E五個Token,如果用前面那兩個控制機(jī)制,這五個哪幾個到Join之后流程會繼續(xù)完全是不可控的,如果我們要實(shí)現(xiàn)必須是A、C、E到達(dá)Join之后流程才可繼續(xù),這樣的需求用isDiscriminator和nOutOfM是無法實(shí)現(xiàn)的。別著急,tokenNames是專為解決這個問題而設(shè)置的,我們選定幾個子Token塞給tokenNames,那么Join就會自動為我們做這些事情了。
     相比tokenNames Script為我們提供了更靈活的控制機(jī)制。如果Script返回Collection類型,那么Script起到了tokenNames的作用,如果返回Bolean類型,那么Script起到isDiscriminator的作用,源碼上注釋的很清楚,我就不在這羅嗦了。
         到這Join的代碼我們也就讀完了,如果上述四個成員變量都為默認(rèn)值,那么Join也就按默認(rèn)的行為執(zhí)行,Join是JBPM源碼中少數(shù)注釋很全的類,這也說明這是JBPM開發(fā)組的得意之作。通過四個屬性我們可以非常靈活的使用Join實(shí)現(xiàn)很多實(shí)用的效果。

              文章原創(chuàng),轉(zhuǎn)載請注明出處!

     

    posted on 2008-11-14 23:55 零全零美 閱讀(1913) 評論(2)  編輯  收藏 所屬分類: jbpm

    FeedBack:
    # re: [原創(chuàng)]JBPM源碼解讀之:Join[未登錄]
    2008-11-15 11:09 | moon
    不錯工作之余來逛逛居家生活網(wǎng) <a href="life126.com">life126.com</a>  回復(fù)  更多評論
      
    # re: [原創(chuàng)]JBPM源碼解讀之:Join[未登錄]
    2008-11-15 11:10 | life126.com
    好文章 ,工具之余來逛逛居家生活網(wǎng)  回復(fù)  更多評論
      
    主站蜘蛛池模板: 久草视频在线免费| 亚洲综合色在线观看亚洲| 亚洲av一综合av一区| 亚洲人成电影网站色| 亚洲国产精品免费视频| 亚洲M码 欧洲S码SSS222| 激情综合亚洲色婷婷五月APP | 国产亚洲情侣久久精品| 最近的中文字幕大全免费8| 久久久久久A亚洲欧洲AV冫| 亚洲男人的天堂网站| 亚洲最大免费视频网| aa毛片免费全部播放完整| 成人免费福利电影| 亚洲精品日韩专区silk| 两个人看的www免费视频中文| 成年女人喷潮毛片免费播放| 久久久久亚洲AV无码麻豆| 中文字幕一区二区三区免费视频| 国产成人免费a在线资源| 国产亚洲福利在线视频| 120秒男女动态视频免费| 亚洲妇熟XXXX妇色黄| 国产va免费精品| 亚洲成年看片在线观看| 久久人午夜亚洲精品无码区| 最近最新中文字幕完整版免费高清| 亚洲综合在线视频| 久久免费线看线看| 亚洲中文字幕在线乱码| 一级做a毛片免费视频| 亚洲 小说区 图片区 都市| 亚洲精品蜜夜内射| 成年在线观看免费人视频草莓| 亚洲国产成人无码av在线播放| 免费A级毛片无码视频| 亚洲三级电影网站| 久久成人a毛片免费观看网站| 亚洲熟妇无码乱子AV电影 | 国产一级黄片儿免费看| 国产成人亚洲综合无码|