[jboss-user] [JBoss jBPM] - Re: Recommended way to purge finished process instances

bradsdavis do-not-reply at jboss.com
Wed Mar 25 15:14:29 EDT 2009


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.");
  | 		
  | 	}
  | 
  | }
  | 
  | 

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4221078#4221078

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4221078



More information about the jboss-user mailing list