通过AEC解读WF的核心原理(九)实现IEventActivity

说明

 

例子下载:https://files.cnblogs.com/wxwinter/aec9.rar

 本例同时实现了CallExternalMethod与HandleExternalEvent的功能

 本例主要演示了,WorkflowRuntimeService,WorkflowQueuingService,WorkflowQueue,实例.EnqueueItemOnIdle方法,IEventActivity,IActivityEventListener<QueueEventArgs>的使用技巧

IEventActivity

//实现IEventActivity的[事件的订阅]方法

public void Subscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)

//实现IEventActivity的[取消事件的订阅]方法

public void Unsubscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)

 

//实现IEventActivity的[WorkflowQueue名称]属性

public IComparable QueueName

 

  1. 将IEventActivity活动放入在ListenActivity中时,ListenActivity启动时首先调用Subscribe方法,表示开始订阅事件,可在这儿做初始化工作。
  2. 当ListenActivity活动从消息队列中收到一条消息时,它将调用Unsubscrible方法,表示已经收到消息,可在此方法中从消息队列中读取消息

ListenActivity中的IEventActivity活动的执行过程为

Subscribe()

Unsubscribe()

Execute()

不在ListenActivity中的活动的执行过程为

Execute()

等待 Queuing 事件

 

IActivityEventListener<QueueEventArgs>

//实现IActivityEventListener<QueueEventArgs> 的[发生订阅事件时的处理过程]事件方法

 

public void OnEvent(object sender, QueueEventArgs e)

 

 

 

例子

 

自定义功能类

public interface 通信接口

{

string Text { set; get; }

IComparable QueueName { set; get; }

void myTest(string v);

}

 

public class 通信类 : WorkflowRuntimeService , 通信接口

{

public string Text

{ set; get; }

 

public IComparable QueueName

{ set; get; }

 

public void myTest(string v)

{

System.Console.WriteLine("一个外部实现:" + v);

}

 

public void NextStep(Guid instanceID, object inputData)

{

WorkflowInstance instance = Runtime.GetWorkflow(instanceID);

instance.EnqueueItemOnIdle(QueueName, inputData, null, null);

}

}

 

自定义Activity

 

public class Activity1 : Activity, IActivityEventListener<QueueEventArgs> , IEventActivity

{

 

public string Text

{ set; get; }

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

if (myProcessAEC(executionContext))

{

return ActivityExecutionStatus.Closed;

}

else

{

Subscribe(executionContext, this);

return ActivityExecutionStatus.Executing;

}

}

 

private bool myProcessAEC(ActivityExecutionContext provider)

{

WorkflowQueuingService queueService = provider.GetService<WorkflowQueuingService>();

if (queueService.Exists(this.QueueName) && queueService.GetWorkflowQueue(this.QueueName).Count > 0)

{

object inputData = queueService.GetWorkflowQueue(this.QueueName).Dequeue();

System.Console.WriteLine("外部传入的数据:" + inputData.ToString());

 

通信接口 obj = provider.GetService<通信接口>();

 

this.Text = obj.Text;

return true;

}

return false;

}

 

//实现IActivityEventListener<QueueEventArgs> 的[发生订阅事件时的处理过程]事件方法

 

public void OnEvent(object sender, QueueEventArgs e)

{

ActivityExecutionContext aec = sender as ActivityExecutionContext;

if (myProcessAEC(aec))

{

Unsubscribe(aec, this);

aec.CloseActivity();

}

}

 

//实现IEventActivity的[WorkflowQueue名称]属性

public IComparable QueueName

{

get { return this.QualifiedName; }

}

 

//实现IEventActivity的[事件的订阅]方法

public void Subscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)

{

WorkflowQueuingService queueService = parentContext.GetService<WorkflowQueuingService>();

 

if (queueService != null && !queueService.Exists(this.QueueName))

{

WorkflowQueue queue = queueService.CreateWorkflowQueue(this.QueueName, false);

 

queue.RegisterForQueueItemAvailable(parentEventHandler);

 

通信接口 obj = parentContext.GetService<通信接口>();

if (obj != null)

{

obj.QueueName = this.QueueName;

obj.myTest(Text);

obj.Text=this.Text;

}

}

}

//实现IEventActivity的[取消事件的订阅]方法

public void Unsubscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)

{

WorkflowQueuingService queueService = parentContext.GetService<WorkflowQueuingService>();

if (queueService != null && queueService.Exists(this.QueueName))

{

queueService.GetWorkflowQueue(this.QueueName).UnregisterForQueueItemAvailable(parentEventHandler);

queueService.DeleteWorkflowQueue(this.QueueName);

}

}

}

 

测试用工作流

    public class Workflow1: SequentialWorkflowActivity

{

private CodeActivity codeActivity2;

private Activity1 activity12;

private CodeActivity codeActivity1;

private Activity1 activity11;

 

        public Workflow1()

        {

            InitializeComponent();

        }

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.codeActivity2 = new System.Workflow.Activities.CodeActivity();

this.activity12 = new wxwinterAecTest.Activity1();

this.codeActivity1 = new System.Workflow.Activities.CodeActivity();

this.activity11 = new wxwinterAecTest.Activity1();

//

// codeActivity2

//

this.codeActivity2.Name = "codeActivity2";

this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity2_ExecuteCode);

//

// activity12

//

this.activity12.Name = "activity12";

this.activity12.Text = "activity12";

//

// codeActivity1

//

this.codeActivity1.Name = "codeActivity1";

this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode);

//

// activity11

//

this.activity11.Name = "activity11";

this.activity11.Text = "activity11";

//

// Workflow1

//

this.Activities.Add(this.activity11);

this.Activities.Add(this.codeActivity1);

this.Activities.Add(this.activity12);

this.Activities.Add(this.codeActivity2);

this.Name = "Workflow1";

this.CanModifyActivities = false;

 

}

 

private void codeActivity1_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine("activity11收到:" + this.activity11.Text);

}

 

private void codeActivity2_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine("activity12收到:" + this.activity12.Text);

}

    }

 

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

通信类 obj = new 通信类();

workflowRuntime.AddService(obj);

 

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

 

String s;

s = System.Console.ReadLine();

obj.Text = s;

obj.NextStep(instance.InstanceId, DateTime.Now.ToString());

 

 

s = System.Console.ReadLine();

obj.Text = s;

obj.NextStep(instance.InstanceId,DateTime.Now.ToString());

 

System.Console.ReadLine();

}

 

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

System.Console.WriteLine("请输入字符:");

}

 

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

 

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

 

}

 

运行结果

 

 

posted @ 2008-06-11 13:56  WXWinter(冬)  阅读(2033)  评论(1编辑  收藏  举报