13 Replies Latest reply on May 8, 2014 7:45 PM by Shobhit Tyagi

    Abort/Delete process instance

    Shobhit Tyagi Master

      Hi,

       

      I am looking to add following functionality to my application but cant get it working.

       

      a) Stop Process Instance: To abort an active process instance which would mean tasks disappearing from user task lists but the history of the process stays.

      b) Delete Process Instance: To delete a process instance(active or inactive) which would mean tasks go away along with any trace of the process in the db.

       

      Can anyone help me with this please.

       

      Thanks

        • 1. Re: Abort/Delete process instance
          Thomas Setiabudi Expert

          Hi Roxy1987,

           

          Can you make use of JBPM Console's REST Service for this?

           

          from the REST list of jbpm-console-server at /gwt-console-server/rs/server/resources/jbpm

           

          they provides

          /gwt-console-server/rs/process/instance/{id}/delete

          and

          /gwt-console-server/rs/process/instance/{id}/end/{result}

           

          haven't used them personally, but the jbpm-console that comes with the installer have a delete feature that use one of that REST service

           

          Regards,

          Thomas Setiabudi

          • 2. Re: Abort/Delete process instance
            Shobhit Tyagi Master

            Hi Thomas,

             

            Thanks for the answer. Though I cant use REST. any other way to treat this that you might know of? there is a method called abortProcessINstance. but calling it is not terminating the prcoess.

            • 3. Re: Abort/Delete process instance
              Shobhit Tyagi Master

              ANything regarding this please guys.. Ve been struggling from a long time..

              • 4. Re: Abort/Delete process instance
                Maciej Swiderski Master

                abortProcessInstance on ksession is the way to go, which will abort the process and remove it from runtime engine data base tables. If it does not work, make sure that session that you use is persistent session so it is capable of finding the right process instance. But it will not remove the history log. An option for this is that you could create dedicated session that will be used for deleting process instances and register a custom process event listener on it that will remove data from log db in afterProcessCompleted method.

                 

                HTH

                • 5. Re: Abort/Delete process instance
                  Shobhit Tyagi Master

                  Hi Maciej,

                   

                  Thanks for the reply. I have used abortProcessINstance, it just runs without any exceptions but doesnt rilly stop the process. WHat do you mean by a persistent session? How do I maintain that.?

                  • 6. Re: Abort/Delete process instance
                    Massimiliano Diodati Newbie

                    Hi Shobhit, did you mamnage to add this functionality? I successfully deployed your application on tomcat whith h2, HornetQ and bitronix, but have the same problem you described here.

                    I mean there is no exception if I stop the process instance, but the tasks are still there and the process instance is still in the list and if I try to stop it again, the following exception is thrown:

                    java.lang.IllegalArgumentException: Could not find process instance.

                    So it seems all correct except for the fact that the process instance should not be returned by findActiveProcessInstances method!

                     

                    I manually went in the database and I have seen that the process instance was removed from the "ProcessInstanceInfo" table but is still in the "ProcessInstanceLog" table. As I read in the forum this is the correct behaviour.

                    I thought that maybe the findActiveProcessInstances method should only return the instances in the "ProcessInstanceInfo" table, while findProcessInstances would return the instances in "ProcessInstanceLog" table; but as I see this is not the behaviour.

                     

                    So I ask everyone can help, what is going wrong in calling ksession.abortProcessInstance?

                     

                    Thank you in advance,

                    Massimiliano

                    • 7. Re: Abort/Delete process instance
                      Shobhit Tyagi Master

                      Massimiliano,

                       

                      Aborting a process instance involves aborting the work items as well. The Human task service is separate in jbpm 5 and hence you need to stop the work items. I have a code snippet which may help you. It may not be the best solution. I set the status of tasks to Exited. I am using Exited as a status for cancelled tasks here.

                       

                      public void terminateProcess(final long intProcessInstId) {

                        //get ksession

                        final KnowledgeRuntimeLogger objKRuntimelogger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger

                                    (objKSession, BpmComunConstants.STR_KNOWLEDGE_RUNTIME_LOGGER, 1000);

                           final JPAWorkingMemoryDbLogger objJPADBlogger = new JPAWorkingMemoryDbLogger(objKSession);

                        final StringBuffer objStrBuffer = new StringBuffer();

                        objStrBuffer.append(BpmComunConstants.STR_TASK_CLIENT_NAME).append(UUID.randomUUID());

                        final String strName = objStrBuffer.toString();

                        final TaskClient objClient = new TaskClient(new HornetQTaskClientConnector(strName,

                                new HornetQTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

                        /*Estable la conexion con BPM*/

                        objClient.connect(strIpAddress, intPort);

                        BlockingTaskSummaryResponseHandler objResHandler = new BlockingTaskSummaryResponseHandler();

                        final BlockingTaskOperationResponseHandler objResHandlerOp = new BlockingTaskOperationResponseHandler();

                        final List<Status> arlStatusList = new ArrayList<Status>(BpmComunConstants.INT_NUMBER_ONE);

                             try {

                              arlStatusList.add(Status.Reserved);

                              arlStatusList.add(Status.Ready);

                              arlStatusList.add(Status.InProgress);

                              arlStatusList.add(Status.Created);

                            objClient.getTasksByStatusByProcessId(intProcessInstId, arlStatusList,

                                          BpmComunConstants.STR_TASK_CLIENT_LANGUAGE, objResHandler);

                            List<TaskSummary> arlTaskSumLst = objResHandler.getResults();

                            Task objTask;

                            long intTaskId = BpmComunConstants.INT_NUMBER_ZERO;

                            long intWorkItemId = BpmComunConstants.INT_NUMBER_ZERO;

                            objTaskHandler = new HornetQHTWorkItemHandler(objKSession);

                         objTaskHandler.setIpAddress(strIpAddress);

                         objTaskHandler.setPort(intPort);

                         final WorkItemManager objWiMgr = objKSession.getWorkItemManager();

                         objWiMgr.registerWorkItemHandler(BpmComunConstants.STR_TASK_HANDLER_NAME, objTaskHandler);

                            while(true) {

                             for(TaskSummary objTaskSummary : arlTaskSumLst) {

                              intTaskId = objTaskSummary.getId();

                              objTask = getNewTask();

                              objTask = getTaskObject(intTaskId);

                              intWorkItemId = objTask.getTaskData().getWorkItemId();

                                 objClient.exit(intTaskId, BpmComunConstants.STR_BPM_SUPER_USER, objResHandlerOp);

                                 objWiMgr.abortWorkItem(intWorkItemId);

                             }

                             objResHandler = getNewBlocking();

                             objClient.getTasksByStatusByProcessId(intProcessInstId, arlStatusList,

                                           BpmComunConstants.STR_TASK_CLIENT_LANGUAGE, objResHandler);

                             arlTaskSumLst = objResHandler.getResults();

                             if(arlTaskSumLst.size() == BpmComunConstants.INT_NUMBER_ZERO) {

                              break;

                             }

                            }

                           

                            setProcessStatus(intProcessInstId);

                             } catch(Exception objException) {

                              LOGGER.loggerError(objException, null);

                             } finally {

                         try {

                          /*Cerrar la conexion BPM*/

                          if(objTaskHandler.isConnected()) {

                           objTaskHandler.dispose();

                          }

                          disconnectClient(objClient);

                         } catch (Exception objException) {

                          LOGGER.loggerError(objException, null);

                         }

                        }

                             

                             objJPADBlogger.dispose();

                             objKRuntimelogger.close();

                             objKSession.dispose();

                      }

                       


                      • 8. Re: Abort/Delete process instance
                        Paulo Ricardo Fernandes Newbie

                        Hi you guys,

                         

                        I'm new to jBPM world, but i have some practice on business process modelling with bpmn.

                         

                        Conceptually speaking, shouldn't we try to solve this kind of questions on our process diagram? BPMN 2.0 specification has a special kind of end event that denotes an "abnormal termination" and aborts the process instance. It's a circle with a minor coloured circle inside it.

                         

                        I don't know how to delete a process instance, but conceptually speaking you shouldn't be deleting any processes information if you have modelled it correctly. That information should be important for tracking processes abnormal terminations.

                        • 9. Re: Abort/Delete process instance
                          Massimiliano Diodati Newbie

                          Shobhit thank you for your fast reply!

                          mmmmmmm.. yeah you're right, I must abort the human tasks first.

                          Your code snippet is very helpful although I'm a little lost in all the lines of code needed to retrieve the workItemManager. In particular, how should the following methods be implemented?

                          getNewTask

                          getTaskObject

                          getNewBlocking

                          setProcessStatus

                          disconnectClient

                          I think to understand how to implement getNewBlocking and disconnectClient, but don't know how to do with the other three.

                          Sorry for asking you about code but the step that takes from Task to WorkItemManager is a little confusing to me.

                          Thank you again for your valuable support.

                          Regards,

                          Massimiliano

                          • 10. Re: Abort/Delete process instance
                            Massimiliano Diodati Newbie

                            Hi Paulo, I understand what you say, probably a process instance should never be terminated; what I need is a functionality for my end-users.

                            If they accidentally start a process, or if they insert wrong data because of a minunderstanding among the employees, I want to provide them a way to abort the instance and start all over again.

                            So this functionality has nothing to do with BPMN 2.0, is just a handful "button" inside my application.

                            Regards,

                            Massimiliano

                            • 11. Re: Abort/Delete process instance
                              Shobhit Tyagi Master


                              Massimiliano,


                              Here is the code for these methods :


                              getNewTask = new Task();

                              getTaskObject =

                               

                              public Task getTaskObject(final long intTask) {

                                final StringBuffer objStrBuffer = new StringBuffer();

                                objStrBuffer.append(BpmComunConstants.STR_TASK_CLIENT_NAME).append(UUID.randomUUID());

                                final String strName = objStrBuffer.toString();

                                final TaskClient objClient = new TaskClient(new HornetQTaskClientConnector(strName,

                                     new HornetQTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

                                /*Estable la conexion con BPM*/

                                objClient.connect(strIpAddress, intPort);

                                final BlockingGetTaskResponseHandler objResHandler = new BlockingGetTaskResponseHandler();

                                Task objTask = new Task();

                                try {

                                 /*Obtiene la Tarea por el Id Tarea*/

                                 objClient.getTask(intTask, objResHandler);

                                 objTask = objResHandler.getTask();

                                } catch(Exception objException) {

                                 LOGGER.loggerError(objException, null);

                                } finally {

                                 disconnectClient(objClient);

                                }

                                return objTask;

                              }

                               

                              getNewBlocking = new BlockingTaskSummaryResponseHandler();

                              setProcessStatus = (you can use JPA here and APIs of jBPM. I use jdbc directly coz its faster) AbortProcessInstance changes the status to 2. I use a different status 3 for terminated processes. so i change it to 3. You can use different value as per your needs.

                               

                              private void setProcessStatus(final long intProcessInstId) throws SQLException, NamingException {

                                setDataSource();

                                Connection con = null;

                                PreparedStatement pstmt;

                                try {

                                 con = objDataSource.getConnection();

                                 pstmt = con.prepareStatement("update "+strSchema+".PROCESSINSTANCELOG set STATUS = 3 where PROCESSINSTANCEID = ?");

                                 pstmt.setLong(1, intProcessInstId);

                                 pstmt.executeUpdate();

                                 pstmt.close();

                                } finally {

                                 if (con != null) {

                                  con.close();

                                 }

                                     }

                              }

                               

                              disconnectClient = objClient.disconnect();

                               

                              Regards,

                              Shobhit Tyagi

                              • 12. Re: Abort/Delete process instance
                                Massimiliano Diodati Newbie

                                Thank you very much!

                                I temporarily stopped working on the stop instance issue; your code is ok but when I abort a work item, the process slips to a gateway where it is necessary that a variable was set, but aborting the workitem means that the variable is null and I me getting exception

                                So I think that to get the whole thing working I must manage the fact that the variables that are used in gateways cannot be null.

                                 

                                Regards,

                                Massimiliano

                                • 13. Re: Abort/Delete process instance
                                  Shobhit Tyagi Master

                                  Managing the variable in the gateway itself so it is never null.