2 Replies Latest reply on Mar 27, 2009 4:42 PM by S Shetty

    Recommended way to purge finished process instances

    S Shetty Newbie

      Hi,

      We use JBPM and JBoss ESB in our application. When a process completes we see the database still contains entries for those processes.

      The following tables contain a large number of rows
      JBPM_MODULE_INSTANCE
      JBPM_PROCESS_INSTANCE
      JBPM_TOKEN
      JBPM_TOKENVARIABLEMAP
      JBPM_VARIABLEINSTANCE
      ...

      What is the recommended or best way to purge or cleanup these entries from the database ? We ran into an issue where the database filled up. We have turned off JBPM logging. But the other tables still contain data.

      Any help appreciated.

      We are using:
      jbossesb-4.4.GA

      Thanks

      :)
      SShetty

        • 1. Re: Recommended way to purge finished process instances
          Brad Davis Novice

          Hello. I have created a POJO JMX MBean that uses the jBPM API to do something similar.

          Compiling this will produce a JAR. Put that JAR into the deploy directory of the server, where the jBPM libraries are also on the server's classpath. If you bring up the JMX management console, you should find the JbpmManagementBean. There, you will find some convenience method to cleanup old processes and process instances.

          Cheers.

          See below:

          package com.amentra.support.jmx.jbpm.management;
          import java.math.BigDecimal;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.List;
          
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import org.hibernate.Query;
          import org.hibernate.Session;
          import org.jboss.annotation.ejb.Management;
          import org.jboss.annotation.ejb.Service;
          import org.jbpm.JbpmConfiguration;
          import org.jbpm.JbpmContext;
          import org.jbpm.graph.def.ProcessDefinition;
          import org.jbpm.graph.exe.ProcessInstance;
          
          @Service
          @Management(JbpmManagement.class)
          public class JbpmManagementBean implements JbpmManagement {
           private static final Log log = LogFactory.getLog(JbpmManagementBean.class);
           JbpmConfiguration configuration = null;
          
           /* (non-Javadoc)
           * @see com.ccna.jmx.jbpm.management.JbpmManagement#removeAllProcessesBefore(java.util.Date)
           */
           public void removeAllProcessesBefore(Date date)
           {
           if (log.isDebugEnabled()) {
           log.debug("Removing all processes before: "+date);
           }
          
           if(configuration==null)
           {
           configuration = JbpmConfiguration.getInstance();
           }
           JbpmContext context = configuration.createJbpmContext();
          
           try{
           Session hibernateSession = context.getSession();
           Query processQuery = hibernateSession.createQuery("from org.jbpm.graph.exe.ProcessInstance pi where pi.start < :removalDate");
           processQuery.setParameter("removalDate", date);
          
           List<ProcessInstance> instances = processQuery.list();
          
           if(instances!=null&&instances.size()>0)
           {
           if (log.isDebugEnabled()) {
           log.debug("Total number of processes to remove: "+instances.size());
           }
           for(ProcessInstance instance : instances)
           {
           if (log.isDebugEnabled()) {
           log.debug("Removing process instance: "+instance.getId());
           }
           //Loop over the instances and remove them.
           context.getGraphSession().deleteProcessInstance(instance);
           }
           }
           else
           {
           log.debug("No instances found to remove.");
           }
           }
           finally {
           context.close();
           }
          
           log.debug("Completed removal process.");
          
           }
          
           /* (non-Javadoc)
           * @see com.ccna.jmx.jbpm.management.JbpmManagement#create()
           */
           public void create() throws Exception {
           log.debug("Creating Jbpm Management JMX bean.");
           }
          
           /* (non-Javadoc)
           * @see com.ccna.jmx.jbpm.management.JbpmManagement#destroy()
           */
           public void destroy() {
           log.debug("Destroying Jbpm Manangement JMX bean.");
           }
          
           public void removeAllProcessesThreeYearsOld() {
           //Get todays date.
           Calendar c = Calendar.getInstance();
           c.setTime(new Date());
           c.add(Calendar.YEAR, -3);
          
           Date threeYearsAgoFromToday = c.getTime();
          
           removeAllProcessesBefore(threeYearsAgoFromToday);
          
           }
          
           @Override
           public void removeAllProcessesBeforeToday() {
           //Get todays date.
           Calendar c = Calendar.getInstance();
           c.setTime(new Date());
           c.add(Calendar.DAY_OF_MONTH, -1);
          
           Date threeYearsAgoFromToday = c.getTime();
          
           removeAllProcessesBefore(threeYearsAgoFromToday);
           }
          
           @Override
           public void removeAllOldProcessDefinitions() {
           if (log.isDebugEnabled()) {
           log.debug("Removing all old versionned process definitions. Cascading.");
           }
          
           if(configuration==null)
           {
           configuration = JbpmConfiguration.getInstance();
           }
           JbpmContext context = configuration.createJbpmContext();
          
           try{
           Session hibernateSession = context.getSession();
          
           Query query = hibernateSession.createSQLQuery("select q2.id_ from (select name_,max(version_) as max_version from jbpm_processdefinition group by name_) q1, "+
           "(select id_,name_,version_ from jbpm_processdefinition) q2 "+
           "where q2.name_ = q1.name_ AND q2.version_ <> q1.max_version");
          
           List<BigDecimal> definitions = query.list();
          
           if(definitions!=null)
           {
           if (log.isDebugEnabled()) {
           log.debug("Found "+definitions.size()+" definitions to remove.");
          
           for(BigDecimal definitionId : definitions)
           {
           if (log.isDebugEnabled()) {
           log.debug("Removing definition: "+definitionId);
           }
           ProcessDefinition definition = context.getGraphSession().getProcessDefinition(definitionId.longValue());
          
           if (log.isDebugEnabled()) {
           log.debug(" + Definition Name: "+definition.getName()+", Version: "+definition.getVersion());
           }
          
           context.getGraphSession().deleteProcessDefinition(definition);
           }
           }
           }
           else
           {
           log.debug("No old process definitions found to remove.");
          
           }
          
           }
           finally {
           context.close();
           }
          
           log.debug("Completed removal process.");
          
           }
          
          }
          
          


          • 2. Re: Recommended way to purge finished process instances
            S Shetty Newbie

            Thanks Brad. Will give it a try.

            :)
            SShetty