现在我们编写几个客户端,实现下面的完整流程:
首先是流程部署,这个在前面已经提到了,这里再重复一下:
using NLog.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
using Zeebe.Client;
namespace ZeebeCSharp
{
class Program
{
private static readonly string ZeebeUrl = "127.0.0.1:26500";
private static readonly string DemoProcessPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "leaveapply.bpmn");
static async Task Main(string[] args)
{
// create zeebe client
var client = ZeebeClient.Builder()
.UseLoggerFactory(new NLogLoggerFactory())
.UseGatewayAddress(ZeebeUrl)
.UsePlainText()
.Build();
var topology = await client.TopologyRequest()
.Send();
Console.WriteLine(topology);
// deploy
var deployResponse = await client.NewDeployCommand()
.AddResourceFile(DemoProcessPath)
.Send();
Console.WriteLine(deployResponse.Workflows[0].BpmnProcessId);
Console.WriteLine(deployResponse.Workflows[0].Version);
Console.WriteLine(deployResponse.Workflows[0].WorkflowKey);
}
}
}
在第六部分,我们讨论了发送起始消息的代码:
using NLog.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Zeebe.Client;
namespace SendApplyMessage
{
class Program
{
private static readonly string ZeebeUrl = "127.0.0.1:26500";
private static readonly string WorkflowInstanceVariables = "{\"ApplyId\":\"123\",\"Name\":\"张三\",\"Days\":3}";
static async Task Main(string[] args)
{
// create zeebe client
var client = ZeebeClient.Builder()
.UseLoggerFactory(new NLogLoggerFactory())
.UseGatewayAddress(ZeebeUrl)
.UsePlainText()
.Build();
var resp = await client.NewPublishMessageCommand()
.MessageName("Message_Apply")
.CorrelationKey(string.Empty)
.Variables(WorkflowInstanceVariables)
.Send();
Console.WriteLine(resp);
}
}
}
这部分代码运行后,可以打开Operator查看一下运行进度:
这里我们启动了两个流程。
然后是模拟发送邮件的客户端,代码如下:
using NLog.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using Zeebe.Client;
using Zeebe.Client.Api.Responses;
using Zeebe.Client.Api.Worker;
namespace JobWorker
{
class Program
{
private static readonly string ZeebeUrl = "127.0.0.1:26500";
private static readonly string JobType = "sendemail";
private static readonly string WorkerName = Environment.MachineName;
static async Task Main(string[] args)
{
// create zeebe client
var client = ZeebeClient.Builder()
.UseLoggerFactory(new NLogLoggerFactory())
.UseGatewayAddress(ZeebeUrl)
.UsePlainText()
.Build();
var topology = await client.TopologyRequest()
.Send();
Console.WriteLine(topology);
using (var signal = new EventWaitHandle(false, EventResetMode.AutoReset))
{
client.NewWorker()
.JobType(JobType)
.Handler(HandleJob)
.MaxJobsActive(5)
.Name(WorkerName)
.AutoCompletion()
.PollInterval(TimeSpan.FromSeconds(1))
.Timeout(TimeSpan.FromSeconds(10))
.Open();
// blocks main thread, so that worker can run
signal.WaitOne();
}
}
private static void HandleJob(IJobClient jobClient, IJob job)
{
// business logic
var jobKey = job.Key;
Console.WriteLine("Handling job: " + job);
}
}
}
需要注意的是,这个的JobType需要与流程定义中的类型相同。运行这段代码,再看一下流程的进展:
流程已经移动到部门经理审批。现在编写部门经理审批的代码:
using NLog.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
using Zeebe.Client;
namespace ZeebeCSharp
{
class Program
{
private static readonly string ZeebeUrl = "127.0.0.1:26500";
private static readonly string WorkflowInstanceVariables = "{\"IsApprove\":true}";
static async Task Main(string[] args)
{
// create zeebe client
var client = ZeebeClient.Builder()
.UseLoggerFactory(new NLogLoggerFactory())
.UseGatewayAddress(ZeebeUrl)
.UsePlainText()
.Build();
var resp=await client.NewPublishMessageCommand()
.MessageName("Message_Approve")
.CorrelationKey("123")
.Variables(WorkflowInstanceVariables)
.Send();
Console.WriteLine(resp);
}
}
}
这里审批的是ApplyId为123的申请,IsApprove为true,运行这段代码,看一下结果,我们会发现相关流程已经结束:
看一下执行的具体过程:
修改一下代码,审批编号为345的申请,IsApprove为false,运行后看一下结果: