惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
J
Java Code Geeks
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
L
LangChain Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
Martin Fowler
Martin Fowler
月光博客
月光博客
Y
Y Combinator Blog
U
Unit 42
D
Docker
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 栖息的熊

Mark一些关于线程同步的信息 【转载】安卓巴士Android开发神贴整理 关于Join 异常诡异的mysql连接异常:Fatal error encountered during command execution. 误人子弟的面试题答案 .Net中的安全机制 用定制特性规范构件调用 用重载操作符实现用户数据类型的转换 override和new关键词的差别 你必须知道的值传递和引用传递 windows用户配置文件的复制 求职:本人从事3年工作流引擎开发,熟悉XPDL,.net3.5中WF,Biztalk,开源工作流引擎OBE,期望工作地点上海 对Activity副本的再理解 Windows Workflow Foundation中实现人工活动的demo,按照XPDL规范的实现 查数据库死锁的实际例子 喜欢使用VMware的.net程序员要注意呀(Visual Studio启动不了的问题) 编译器错误信息: CS1595: 已在多处定义“ ”;一种可能出现的情况 BPM业务场景分析(1)——内部交易合并开票 高级工作流模式深入业务场景分析(1)——多路合并
WF消息队列的使用
栖息的熊 · 2008-07-17 · via 博客园 - 栖息的熊

工作流队列用于在宿主程序或工作流外部服务与工作流中的活动之间传递消息。WF本质论里面写的很含糊,通过下面的例子可以很好的理解工作流队列的使用。

例子主要功能是,一流程运行到ReadLine活动处,等待用户在控制台的输入,如果用户没有输入,则流程会钝化,有一个书签(其实就是对事件的处理方法)会加到工作流队列的QueueItemAvailable事件上。

下面首先看ReadLine这个活动,解释看注释。

using System;

using System.Workflow.ComponentModel;

using System.Workflow.Runtime;

namespace EssentialWF.Activities {

  public class ReadLine : Activity {

    private string text;

    public string Text {

      get { return this.text; }

    }

protected override void Initialize(IServiceProvider provider) {

//活动初始化时就用活动名字创建,不用担心多个实例间的重名情况,因为消息队列是跟着流程实例走的

      WorkflowQueuingService qService =(WorkflowQueuingService)provider.GetService(typeof(WorkflowQueuingService));

      if (!qService.Exists(this.Name))

        qService.CreateWorkflowQueue(this.Name, true);

    }

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext context) {

      WorkflowQueuingService qService = context.GetService<WorkflowQueuingService>();

      //尝试执行获取用户输入,如果有用户输入,Dequeue方法就能获取的到

          //并将活动的执行状态置为结束

 WorkflowQueue queue = qService.GetWorkflowQueue(Name);

      if (queue.Count > 0) {

        this.text = (string)queue.Dequeue();

        return ActivityExecutionStatus.Closed;

      }

           //尝试执行没有成功,给队列的QueueItemAvailable事件(此事件会在消息入队列时触发)加上处理方法

           //并将活动的执行状态置为运行中

      queue.QueueItemAvailable += this.ContinueAt;

      return ActivityExecutionStatus.Executing;

    }

         //下面都很好理解了,不说了

    void ContinueAt(object sender, QueueEventArgs e) {

      ActivityExecutionContext context = sender as ActivityExecutionContext;

      WorkflowQueuingService qService = context.GetService<WorkflowQueuingService>();

      WorkflowQueue queue = qService.GetWorkflowQueue(Name);

      this.text = (string)queue.Dequeue();

      context.CloseActivity();

    }

    protected override void Uninitialize(IServiceProvider provider) {

      WorkflowQueuingService qService = (WorkflowQueuingService)provider.GetService(typeof(WorkflowQueuingService));

      if (qService.Exists(this.Name))

        qService.DeleteWorkflowQueue(this.Name);

    }

  }

}

再看我们的宿主程序怎么和ReadLine活动配合。

string s = Console.ReadLine();

instance.EnqueueItem(“rl1”,s,null,null);//这行代码就是向队列中加入消息,rl1是活动的名称,s是消息的数据信息,后两个参数是用于传递消息后接受反馈的通知。