Created
December 11, 2013 08:57
-
-
Save derkork/7907114 to your computer and use it in GitHub Desktop.
The Activiti workflow engine keeps the metadata of all historic processes in it's database so you can query it later. Over time this meatdata can grow to quite significant amounts (several gigabytes). In many cases it is not required to save this metadata forever. This is an example how to clean up historic process metadata from the Activiti dat…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.janthomae.activiti; | |
import org.activiti.engine.HistoryService; | |
import org.activiti.engine.history.HistoricProcessInstance; | |
import org.joda.time.DateTime; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Required; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
/** | |
* Cleanup procedure which removes old activiti metadata. | |
*/ | |
public class ActivitiCleanupProcedure implements CleanupProcedure { | |
private static final Logger log = LoggerFactory.getLogger(ActivitiCleanupProcedure.class); | |
private static final Integer DEFAULT_RETENTION_SECONDS = 30 * 24 * 3600; // 30 days | |
private Integer historicRetentionInSeconds = DEFAULT_RETENTION_SECONDS; | |
private HistoryService historyService; | |
@Override | |
public void run() throws Exception { | |
Date date = new DateTime().minusSeconds(historicRetentionInSeconds).toDate(); | |
DateFormat df = new SimpleDateFormat(); | |
log.info("Removing Activiti history before the {}", df.format(date)); | |
List<HistoricProcessInstance> outdatedProcessInstances = | |
historyService.createHistoricProcessInstanceQuery().finishedBefore(date).list(); | |
log.info("Purging " + outdatedProcessInstances.size()+ " old process instances."); | |
for (HistoricProcessInstance outdatedProcessInstance : outdatedProcessInstances) { | |
// this will clean up all the foreign keys and references and whatnot as well. | |
historyService.deleteHistoricProcessInstance(outdatedProcessInstance.getId()); | |
} | |
} | |
@Required | |
public void setHistoryService(HistoryService historyService) { | |
this.historyService = historyService; | |
} | |
public void setHistoricRetentionInSeconds(Integer historicRetentionInSeconds) { | |
this.historicRetentionInSeconds = historicRetentionInSeconds; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi derkork,
I'm facing same issue at the moment. From your code, it seems Activiti exposed historic services which allow us to proceed clean up. But my question is from the code I feel the only Historic table that was removed is ACT_HI_PROCINST, are there more cleanup we can use? There are quite a few ACT_HI* tales in DB.
I did have to connect to mySQL DB to perform delete records by typing SQL queries.