5 Replies Latest reply on Jul 3, 2012 5:28 AM by gigazhang

    Using multiple kSessions

    r3vans

      I have not been able to get multiple sessions working properly:

       

      • I declare multiple sessions in the spring context (see below).
      • I start a pool of threads. As each thread is executed I grab session and save it in a ThreadLocal.
      • I despatch calls to kSession.startProcess in each thread.

       

      Is this a reasonable approach?

       

      I find that this will run OK for a while but ultimately will fail either with an NPE in ProcessInstanceImpl getProcess because kruntime is null OR will fail to commit a transaction because it has already been committed.


      Regards,

      Richard

       

       

       

       

       

      <drools:ksession id="kSession1" type="stateful" kbase="kBase">

          <drools:configuration>

              <drools:work-item-handlers>

                  <!-- Process : ServiceNowCMDB CreateCI -->

                  <drools:work-item-handler name="ServiceNowCmdbMapperST" ref="ServiceNowCmdbMapperHandler"/>

                  ...etc

             

              </drools:work-item-handlers>

              <!-- jBPM Persistence -->

              <drools:jpa-persistence>

                  <drools:transaction-manager ref="transactionManager" />

                  <drools:entity-manager-factory ref="entityManagerFactory" />  

              </drools:jpa-persistence>

          </drools:configuration>

      </drools:ksession>

       

      <drools:ksession id="kSession2" type="stateful" kbase="kBase">

          <drools:configuration>

              <drools:work-item-handlers>

                  <!-- Process : ServiceNowCMDB CreateCI -->

                  <drools:work-item-handler name="ServiceNowCmdbMapperST" ref="ServiceNowCmdbMapperHandler"/>

                  ...etc

             

              </drools:work-item-handlers>

              <!-- jBPM Persistence -->

              <drools:jpa-persistence>

                  <drools:transaction-manager ref="transactionManager" />

                  <drools:entity-manager-factory ref="entityManagerFactory" />  

              </drools:jpa-persistence>

          </drools:configuration>

      </drools:ksession>

        • 1. Re: Using multiple kSessions
          salaboy21

          Hmm..

          What are you trying to achieve with the multiple sessions?

          I mean, what's the purpose of having multiple sessions?

          Are you running a process in each of those sessions?.

          When are the threads being called?

           

          Cheers

          • 2. Re: Using multiple kSessions
            r3vans

            Hello Maurcion.

             

            What are you trying to achieve with the multiple sessions?

            => Get some throughput. I have some latent service tasks so want some concurrency.

            => I realise that an alternative would be to make the service tasks async but this option (I thought) would be a little easier.

             

            I mean, what's the purpose of having multiple sessions?

            => Without multiple sessions, multiple threads synchronise on kSession methods and activity is not concurrent

             

            Are you running a process in each of those sessions?.

            => Yes.

             

            When are the threads being called?

            => I have a request queue (specific to my app) which is queried in one scheduler thread.

            => When the scheduler finds a request it hands to a thread from a pool. Each thread in the pool has its own ksession and starts a corresponding process.

            (=> If the process hits a timer then it resumes in a session-specific time  thread.)

             

            Does this make sense?

            Thanks for your help!

            Richard

            • 3. Re: Using multiple kSessions
              salaboy21

              Now I understand a little bit more about your scenario.

              So my next question is, does your processes require persistence? are they long running?

              If they include Human Tasks, you will have a bottle neck in the human interactions not in starting the sessions.

               

              If you have short running processes / or in memory processes, you can just create a session per request and as soon as they finish you can drop that session.

               

              Based on your configuration, it seam that you need persistence (but I'm just checking here).

               

              And basically yes, my suggestion will be go to the async service task approach.. so the engine can do all the instantiation, but you don't need to wait to call your service and expect the results before moving on to the next process creation.

               

              Cheers

              • 4. Re: Using multiple kSessions
                r3vans

                Thanks again,

                 

                Firstly the answers to your questions...

                 

                Processes require persistence. We want to ensure that we lose no data on failure and plan to make them restartable

                There are no human tasks.

                Each service task lasts up to a few seconds. We use timers a lot to poll for asynch responses so total process execution time may run to many minutes.

                 

                 

                I will pursue the async handler approach. Just for my satisfaction, though, what are the blockers to my current approach. It is generally much easier to write sync code and let the workflow engine m-thread.

                 

                I appreciate your views.

                Cheers,

                Richard

                • 5. Re: Using multiple kSessions
                  gigazhang

                  drools config:

                   

                  <drools:ksession id="ksession" type="stateful" kbase="kbase" node="node">

                          <drools:configuration>

                              <drools:jpa-persistence>

                                  <drools:transaction-manager ref="transactionManager" />

                                  <drools:entity-manager-factory ref="entityManagerFactory" />

                              </drools:jpa-persistence>

                          </drools:configuration>

                      </drools:ksession>

                   

                  The ksession infor can't be persistenced into db, but the task information can be.

                   

                  what's wrong ?