Use of WF Message Queue

Source: Internet
Author: User

Workflow queue is used in the hostProgramOr send messages between the external service of the workflow and the activity in the workflow.WFThe description in this article is vague. The following example can be used to understand the use of workflow queues.

The main function of this example isReadlineAt the activity, wait for the user's input in the console. If the user does not enter the content, the process will be deactivated, and there will be a bookmark (in fact, the event processing method) added to the workflow queue.QueueitemavailableEvent.

Next, let's take a lookReadlineFor more information about this activity, see annotations.

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 ){

//When the activity is initialized, it is created with the activity name. You don't have to worry about the duplicate names between multiple instances, because the message queue follows the process instance.

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> ();

 

//Try to get user input. If user input exists,DequeueMethod To obtain

//And set the activity execution status to end.

Workflowqueue queue = qservice. getworkflowqueue (name );

If (queue. Count> 0 ){

This. Text = (string) queue. dequeue ();

Return activityexecutionstatus. closed;

}

 

//The attempt failed.QueueitemavailableEvent (this event will be triggered when the message is sent to the queue) plus Processing Method

//And set the activity execution status to running

Queue. queueitemavailable + = This. continueat;

Return activityexecutionstatus. Executing;

}

 

//I have understood it well below. I will not talk about it anymore.

 

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 );

}

}

}

 

Let's look at how our host ProgramReadlineActivities.

 

String S = console. Readline ();

instance. enqueueitem ("rl1", S, null, null); // Code in this line adds a message to the queue. rl1 is the name of the activity, S is the data information of the message. The last two parameters are used to receive feedback notifications after passing the message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.