在Jbpm的userguide的第16.2.3节中,讲述了Delegation的四种配置机制。相对应的也就是config-type属性的四种可能的值。分别是“field,bean,constructor,configuration-property”。我建议读者先读一下userguide的第16.2.3节。
回忆一下,Delegation类有一个map成员instantiatorCache。它五个元素的key,除了null,其余四个正是我们上面列出的四个值。也就是说,在建模的时候,对于<action class=”dust.action.Action1Handler” config-type=”field”>元素, Action类的actionDelegation成员的instantiate()方法就会选择instantiatorCache中的FieldInstantiator对象来实例化dust.action.Action1Handler类。如果config-type=”bean”,那么就会选择BeanInstantiator。依次类推。
如果你不写config-type那么这个时候instantiatorCache的null就起作用了,所以默认还是FieldInstantiator。
那么FieldInstantiator,BeanInstantiator,ConfigurationPropertyInstantiator ,ConstructorInstantiator,四个类创建客户化对象有什么不同呢?
我们先写个类dust.action.Action1Handler,来看看。
public Action1Handler implements ActionHandler{
String name;
int count;
public Action1Handler(){}
public Action1Handler(String configuration){
System.out.println("==Action1Handler contstructor==");
System.out.println("==configuration is:"+configuration+"==");
}
public void setConfiguration(String configuration){
System.out.println("==Action1Handler setConfiguration==");
System.out.println("==configuration is:"+configuration+"==");
}
// getters and setters //////////////////////////////////////////////////////
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name ;
}
public void setCount(int count){
this.count = count;
}
public int getCount(){
return count;
}
public void execute(ExecutionContext executionContext) throws Exception {
// TODO Auto-generated method stub
}
}
下面我们假设流程定义为中有
<action class=”dust.action.Action1Handler” config-type=”field”>
<name>dust</name>
<count>5</count>
</action>
5.1. FieldInstantiator类
当config-type为field时,或者没有config-type属性,会使用FieldInstantiator类。
FieldInstantiator类使用反射机制直接对类的成员进行赋值。
也就是说FieldInstantiator会直接将dust付给name,5付给count。
5.2. BeanInstantiator类
当config-type为bean时,会使用BeanInstantiator类。
BeanInstantiator类必须使用setter属性,来对成员赋值。
即,BeanInstantiator使用setName()和setCount()。如果没有这两个方法,那么就会在日志中报错。
5.3. ConstructorInstantiator类
当config-type为constructor时,会使用ConstructorInstantiator类。
ConstructorInstantiator类会调用客户类的具有一个String类型参数的构造函数。并且将<action>元素的所有内容作为字符串类型的参数,传给构造函数。客户可以在这个构造函数中自己处理这段xml文档。
也就是说Jbpm会调用 public Action1Handler(String configuration){}。
5.4. ConfigurationPropertyInstantiator类
当config-type为configuration-property时,会使用ConfigurationPropertyInstantiator类。
ConfigurationPropertyInstantiator类会调用客户类的具有一个String类型参数的一个名叫setConfiguration()方法。并且将<action>元素的所有内容作为字符串类型的参数,传给setConfiguration方法。客户可以在这个方法中自己处理这段xml文档。
也就是说Jbpm会调用 public void setConfiguration(String configuration){}方法。
在Jbpm执行一个流程实例过程中,当遇到一个Action时,会首先实例化流程定义中的Action元素指定的客户化类。如果没有指定config-type的话,那么会调用客户化类的默认构造函数。然后调用execute()方法。
如果config-type为field或bean的话,那么就会在调用execute()前进行属性赋值;如果config-type为constructor,那么就会调用有参数的构造函数;如果config-type为configuration-property是,会先调用默认构造函数,然后调用setConfiguration()方法。
就像Jbpm的文档中自己写的那样,Jbpm使用Delegation机制是为了在流程执行中能够使用用户自己定制的代码(user’s custom code)。
在jPDL的schema定义中,有四个元素是可以添加用户自己定制的类。那就是action,assignment,controller和handler。这四个元素都有两个共同的属性,class和config-type。其中,action元素中class所指定的类必须实现org.jbpm.graph.def.ActionHandler接口。Assignment元素的class所指定的类必须实现org.jbpm.taskmgmt.def.AssignmentHandler接口。controller元素的class所指定的类必须实现org.jbpm.taskmgmt.def.TaskControllerHandler接口。handler元素的class所指定的类必须实现org.jbpm.graph.node.DecisionHandler接口。
在这四个元素的schema定义中还都有一个没有Name属性的成员“{content}”。Jbpm对它的解释是“the content of the handler can be used as configuration information for your custom handler implementations. This allows the creation of reusable delegation classes.”我会在Delegation的配置类型部分,详细讲解这个内容。
分享到:
相关推荐
Troubleshooting_Kerberos_Delegation
Delegation委托写法项目实例,自定义方法调用,在项目中将方法作为参数传递。
NFSv4-delegation-Noveck-color.pdf
剖析Java Event-Delegation Model,讲得很好,有深度!!
my-delegation-poker-源码.rar
php设计模式 Delegation 委托模式示例代码,需要的朋友可以参考下。
COM Delegation Using the COM Channel Hook Mechanism使用CCHM机制的COM对象
Router(config)#ipv6 dhcp pool v6 //创建DHCP池 Router(config-dhcp)# prefix-delegation pool v6pool Router(config-dhcp)# dns-server 2001:470:20::2 Router(config-dhcp)# domain-name ewao_cn Router(config-...
Spark streaming应用运行7天之后,自动退出,日志显示token for xxx(用户名): HDFS_DELEGATION_TOKEN owner=xxxx@xxxx.com, renewer=yarn, realUser=, issueDate=1581323654722, maxDate=1581928454722, sequence...
Protecting Data Privacy in Publicly Verifiable Delegation of Matrix and Polynomial Functions
presentation to Shanghai Delegation on Shipping Centre伦敦金融城、波罗的海交易所以及全球航运市场-报告资料.pdf
Protocol (DHCP) for IPv6 prefix delegation on your networking devices. General prefixes can be defined in several ways: manually, based on a 6to4 interface, and dynamically, from a prefix received by ...
JavaScript_面試_事件傳遞機制和事件委託_Event_Propagation_&_Event_Delegation_-
地形-dns-区域授权 用于委托DNS区域(特别是DNS区域)的Terraform模块
官方版本,亲测可用
Verifiable Outsourced Computation with Full Delegation
代表团 在 C++11 中编译时通过模板没有虚函数调用的委托
事件委托 浏览器DOM事件的事件委托。 灵活,跨浏览器兼容且以Typescript为重点。 快速链接 安装 CDN ...import EventDelegation from '@jjwesterkamp/event-delegation' EventDelegation命名空间对象
能力委托将使用受限API的功能转移到框架树中的另一个window 。介绍什么是能力委派? JS中以受限方式可以使用Web中的许多功能。 例如: 大多数浏览器仅在用户最近与页面进行交互或允许浏览器从页面来源打开弹出窗口时...
官方版本,亲测可用