事件驱动在fireflow用的频率很高.比如:StartNodeInstance,TransitionInstance,ActivityInstance,LoopInstance,EndNodeInstance
1):定义事件监听器接口模型
package org.fireflow.kernel.event;
import org.fireflow.kernel.KernelException;
/**
*定义事件监听器模型
*
*/
public interface INodeInstanceEventListener {
/**
* 节点实例监听器
* @param e
* @throws KernelException
*/
public void onNodeInstanceEventFired(NodeInstanceEvent e)throws KernelException;
}
2):定义事件模型,它继承于EventObject
package org.fireflow.kernel.event;
import java.util.EventObject;
import org.fireflow.kernel.IToken;
/**
* 事件模型
*
* @author chennieyun
*
*/
@SuppressWarnings("serial")
public class NodeInstanceEvent extends EventObject {
// 事件类型
public static final int NODEINSTANCE_TOKEN_ENTERED = 1;
public static final int NODEINSTANCE_FIRED = 2;
public static final int NODEINSTANCE_COMPLETED = 3;
public static final int NODEINSTANCE_LEAVING = 4;
// 默认的事件类列
int eventType = -1;
private IToken token = null;
private NodeInstanceEvent() {
super(null);
}
// 注册事件源
public NodeInstanceEvent(Object source) {
super(source);
}
public IToken getToken() {
return token;
}
public void setToken(IToken tk) {
this.token = tk;
}
public int getEventType() {
return eventType;
}
public void setEventType(int eventType) {
this.eventType = eventType;
}
}
3:触发事件, 以ActivityInstance为例
//触发Activity Entered事件
//定义一个数据源
NodeInstanceEvent event1 = new NodeInstanceEvent(this);
//设置当前的令牌
event1.setToken(tk);
//设置当前的事件类型
event1.setEventType(NodeInstanceEvent.NODEINSTANCE_TOKEN_ENTERED);
//触发事件
fireNodeEvent(event1);
/**
* wangmj 20090914 增加统一的触发方法,实现类中根据事件的不同而进行触发
* @param event
* @throws KernelException
*/
public void fireNodeEvent(NodeInstanceEvent event) throws KernelException{
for (int i=0;i<this.eventListeners.size();i++){
INodeInstanceEventListener listener = this.eventListeners.get(i);
listener.onNodeInstanceEventFired(event);
}
}
4:其它说明, StartNodeInstance,TransitionInstance,ActivityInstance,LoopInstance,EndNodeInstance都有一些默认的事件监听器,具体在FireflowContext.xml中定义.
<!-- **************Kernel Manager Begin*************** -->
<bean id="kernelManager"
class="org.fireflow.kernel.KernelManager">
<property name="kernelExtensions">
<map>
<entry key="org.fireflow.kernel.StartNodeInstance">
<list>
<ref local="startNodeInstanceEventListenser"/>
</list>
</entry>
<entry key="org.fireflow.kernel.ActivityInstance">
<list>
<ref local="activityInstanceEventListener"/>
</list>
</entry>
<entry key="org.fireflow.kernel.SynchronizerInstance">
<list>
<ref local="synchronizerInstanceEventListenser"/>
</list>
</entry>
<entry key="org.fireflow.kernel.EndNodeInstance">
<list>
<ref local="endNodeInstanceEventListenser"/>
</list>
</entry>
<entry key="org.fireflow.kernel.TransitionInstance">
<list>
<ref local="transitionInstanceEventListenser"/>
</list>
</entry>
<entry key="org.fireflow.kernel.LoopInstance">
<list>
<ref local="loopInstanceEventListenser"/>
</list>
</entry>
</map>
</property>
</bean>
Fireflow的官方网站为:
http://www.fireflow.org/forum.php