卸载JBoss工作流引擎Jbpm的工作流定义
上次写了一个部署Jboss的工作流引擎Jbpm的工作流定义的助手类,并写了一篇文章《编程方式部署jBPM工作流》。有部署,就必然要有卸载。今天,我又写了一个卸载Jbpm工作流定义及其相关实例的助手类。下面是源代码:
接口:
package com.withub.common.util;
publicinterface IUninstallProcessDefinition {
/**
*删除给定业务程序定义名称的所有版本的业务程序定义,以及业务程序实例
*@paramname
*/
publicabstractvoid uninstallJbpmProcessDefinitionsTransaction(String name);
/**
*删除给定版本的业务程序定义及其所有实例。
*@paramname
*@paramversion
*/
publicabstractvoid uninstallJbpmProcessDefinitionTransaction(String name,
int version);
/**
*删除最新版本的业务程序定义及其实例。
*@paramname
*/
publicabstractvoid uninstallLatestJbpmProcessDefinitionTransaction(
String name);
}
实现类:
/**
*
*/
package com.withub.common.util;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
/**
* @author 沈东良 shendl_s@hotmail.com
* 10:50:42 AM
* 这个类,用来卸载已部署的业务程序定义
* *Transaction 这样命名的方法名,表示可以直接在前台调用。已经处理了事务和数据库连接。
*
* *Service 是助手方法。 可以被任意调用。
*
* *Dao 是使用数据库、事务的Service助手方法。 不能直接被前台调用。
* 否则,会出现数据库连接、事务未创建和管理的问题。 数据库访问和事务处理可能会失败。
*/
public class UninstallJbpmProcessDefinition implements IUninstallProcessDefinition {
private Log log=LogFactory.getLog(this.getClass());
private static IUninstallProcessDefinition instance;
public static IUninstallProcessDefinition getInstance(){
if(UninstallJbpmProcessDefinition.instance==null){
UninstallJbpmProcessDefinition.instance=new UninstallJbpmProcessDefinition();
}
return UninstallJbpmProcessDefinition.instance;
}
/* (non-Javadoc)
* @see com.withub.common.util.IUninstallProcessDefinition#uninstallJbpmProcessDefinitionsTransaction(java.lang.String)
*/
public void uninstallJbpmProcessDefinitionsTransaction(String name){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
try {
this.uninstallJbpmProcessDefinitionsDao(name);
}finally{
jbpmContext.close();
}
}
/**
*
* @param name
*/
private void uninstallJbpmProcessDefinitionsDao(String name){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext();
GraphSession graphSession=jbpmContext.getGraphSession();
List processDefinitions=graphSession.findAllProcessDefinitionVersions(name);
if(processDefinitions!=null){
Iterator iterator=processDefinitions.iterator();
while(iterator.hasNext()){
graphSession.deleteProcessDefinition((ProcessDefinition)iterator.next());
}
}
log.info("卸载工作流:"+name);
}
/* (non-Javadoc)
* @see com.withub.common.util.IUninstallProcessDefinition#uninstallJbpmProcessDefinitionTransaction(java.lang.String, int)
*/
public void uninstallJbpmProcessDefinitionTransaction(String name,int version){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
try {
this.uninstallJbpmProcessDefinitionDao(name, version);
}finally{
jbpmContext.close();
}
}
private void uninstallJbpmProcessDefinitionDao(String name,int version){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext();
GraphSession graphSession=jbpmContext.getGraphSession();
graphSession.deleteProcessDefinition(graphSession.findProcessDefinition(name, version));
log.info("卸载工作流:"+name+"第"+new Integer(version)+"版");
}
/* (non-Javadoc)
* @see com.withub.common.util.IUninstallProcessDefinition#uninstallLatestJbpmProcessDefinitionTransaction(java.lang.String)
*/
public void uninstallLatestJbpmProcessDefinitionTransaction(String name){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
try {
this.uninstallLatestJbpmProcessDefinitionDao(name);
}finally{
jbpmContext.close();
}
}
/**
*
* @param name
*/
private void uninstallLatestJbpmProcessDefinitionDao(String name){
JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext();
GraphSession graphSession=jbpmContext.getGraphSession();
ProcessDefinition processDefinition=graphSession.findLatestProcessDefinition(name);
graphSession.deleteProcessDefinition(processDefinition);
log.info("卸载工作流最新版:"+name);
}
/**
* @param args
* 测试
*/
public static void main(String[] args) {
/**
*1,一个参数,全部
*2,二个参数 名字,latest 最新版本
* 版本号
*/
IUninstallProcessDefinition uninstallJbpmProcessDefinition=UninstallJbpmProcessDefinition.getInstance();
if(args.length==1){
uninstallJbpmProcessDefinition.uninstallJbpmProcessDefinitionsTransaction(args[0]);
}else if(args.length==2){
if(args[1].equalsIgnoreCase("latest")){
uninstallJbpmProcessDefinition.uninstallLatestJbpmProcessDefinitionTransaction(args[0]);
}else{
uninstallJbpmProcessDefinition.uninstallJbpmProcessDefinitionTransaction(args[0], Integer.parseInt(args[1]));
}
}else{
throw new RuntimeException("参数数目不正确! 参数:1,一个参数,提供要卸载的工作流的名称; 2,两个参数,第一个是要卸载的最新版本的工作流名称,第二个参数是latest; 3,两个参数," +
"第一个是要卸载的工作流名称,第二个参数是要修改的工作流的版本号。");
}
}
}