JBPM流程部署之部署解析器相关对象扩展
流程部署解析器是流程引擎很重要的部分,负责xml流程定义的解析并转化成运行时的流程定义实体对象,它在很大的程度上决定了流程定义xml的书写形式,当然了也可以说流程定义xml在很大的程度上决定了部署解析器的业务逻辑实现,只是因为站的角度不同;总之怎么强调部署解析器的重要性都不过分,可以说流程部署解析器就是流程定义描述语言的编译器,当我们发布流程的时候,它负责一次性编译好我们的xml流程语言,并最终形成我们流程引擎运行时使用的“语言”(流程定义运行时);流程部署相关对象主要涉及DeployManager、JpdlDeployer等对象。今天我们也简单的来看下这块的扩展
DeployerManager对象扩展
从名字上我们可以大概猜测到其作用,其是流程部署总的管理和执行者,其从大局角度出发,总揽流程部署的一切大事;但是其实现很简单,一把手吗,只需要根据外部传入的信息(发布新的流程、更新现有的流程),其只负责调用相关的部署解析器;所以很多的时候我们不需要新增DeployManager对外提供的接口,而往往只要通过扩展其现有的扩展就可以实现我们的业务需求;例如我们可能每天发布很多的业务流程,我们不需要流程发布时就缓存业务流程实体对象,只有在我们真正有业务流程流转时才缓存;
新增WFTHDeployerManager,并继承DeployerManager
import java.util.ArrayList;
import java.util.List;
import org.jbpm.api.JbpmException;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.xml.Problem;
/**
* @author 无风听海
*/
public class WFTHDeployerManager extends DeployerManager{
private static Log LOG = Log.getLog(DeployerManager.class.getName());
List<Deployer> deployers;
//新增是否需要缓存流程定义
Boolean isCacheProcessDefinition;
public void deploy(DeploymentImpl deployment) {
deployment.setProblems(new ArrayList<Problem>());
for (Deployer deployer: deployers) {
deployer.deploy(deployment);
}
if (deployment.hasErrors()) {
JbpmException jbpmException = deployment.getJbpmException();
LOG.info("errors during deployment of "+deployment+": "+jbpmException.getMessage());
throw jbpmException;
}
if(isCacheProcessDefinition)
{
RepositoryCache repositoryCache = EnvironmentImpl.getFromCurrent(RepositoryCache.class);
if (repositoryCache != null) {
// If there are no objects after deploying, then there is something wrong
if (deployment.getObjects() == null || deployment.getObjects().isEmpty()) {
if (LOG.isWarnEnabled()) {
LOG.warn("WARNING: no objects were deployed! Check if you have configured a correct deployer "
+"in your jbpm.cfg.xml file for the type of deployment you want to do.");
}
} else {
repositoryCache.set(deployment.getId(), deployment.getObjects());
}
}
}
}
public void updateResource(DeploymentImpl deployment, String resourceName, byte[] bytes) {
for (Deployer deployer: deployers) {
deployer.updateResource(deployment, resourceName, bytes);
}
if(isCacheProcessDefinition)
{
RepositoryCache repositoryCache = EnvironmentImpl.getFromCurrent(RepositoryCache.class);
repositoryCache.remove(deployment.getId());
}
}
}
新建WFTHRepositoryManagerBinding,完成我们新定义的属性isCacheProcessDefinition的绑定
import org.jbpm.pvm.internal.repository.WFTHDeployerManager;
import org.jbpm.pvm.internal.wire.descriptor.FalseDescriptor;
import org.jbpm.pvm.internal.wire.descriptor.ObjectDescriptor;
import org.jbpm.pvm.internal.wire.descriptor.TrueDescriptor;
import org.jbpm.pvm.internal.xml.Parse;
import org.jbpm.pvm.internal.xml.Parser;
import org.w3c.dom.Element;
/**
* @author 无风听海
*/
public class WFTHDeployerManagerBinding extends DeployerManagerBinding{
public WFTHDeployerManagerBinding() {
super();
}
public Object parse(Element element, Parse parse, Parser parser) {
ObjectDescriptor objectDescriptor=(ObjectDescriptor)super.parse(element,parse,parser);
objectDescriptor.setClassName(WFTHDeployerManager.class.getName());
if(element.hasAttribute("isCacheProcessDefinition"))
{
String isCacheProcessDefinition=element.getAttribute("isCacheProcessDefinition");
if(isCacheProcessDefinition.toLowerCase().equals("true"))
{
TrueDescriptor trueDescriptor=new TrueDescriptor();
objectDescriptor.addInjection("isCacheProcessDefinition", trueDescriptor);
}
else
{
FalseDescriptor falseDescriptor=new FalseDescriptor();
objectDescriptor.addInjection("isCacheProcessDefinition", falseDescriptor);
}
}
return objectDescriptor;
}
}
修改jbpm.wire.bindings.xml加载我们新建的WFTHDeployManagerBinding
<!-- mod by 无风听海 -->
<!-- <binding class="org.jbpm.pvm.internal.wire.binding.DeployerManagerBinding" />-->
<binding class="org.jbpm.pvm.internal.wire.binding.WFTHDeployerManagerBinding" />
<!-- end-->
这样我们就可以通过在jbpm.jpdl.cfg.xml通过配置属性isCacheProcessDefinition类灵活的控制发布流程后时候缓存流程定义实体对象
<!--mod by 无风听海-->
<deployer-manager isCacheProcessDefinition="true">
<jpdl-deployer />
<object class="org.jbpm.pvm.internal.repository.RulesDeployer" />
</deployer-manager>
</process-engine-context>
对Deployer解析器的扩展
流程引擎中现在实现了JpdlDeployer、ProcessDeployer、RulesDeployer,他们具体负责对流程定义xml的解析,对于对现有解析器的扩展,基本与前边相同,这里不再赘述;我们有时候可能扩展流程引擎支持一些新的功能比如引入完全不同的新的规则引擎,这时我们往往需要新增新的解析器
新增WFTHRulesDeployer类,负责解析业务规则
/**
* @author 无风听海
*/
public class WFTHRulesDeployer {
//实现我们自定义的规则解析
}
修改jbpm.jpdl.cfg.xml加载我们的规则解析器
<process-engine-context>
<!--mod by 无风听海-->
<deployer-manager isCacheProcessDefinition="true">
<jpdl-deployer />
<object class="org.jbpm.pvm.internal.repository.WFTHRulesDeployer" />
<!--<object class="org.jbpm.pvm.internal.repository.RulesDeployer" />-->
</deployer-manager>
</process-engine-context>
这样我们就可以使用我们新的业务规则解析器进行解析业务规则了