Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Zeebe Job handlers are automaticly recognized and boostrapped via a .Net HostedService.

License

Notifications You must be signed in to change notification settings

camunda-community-hub/zeebe-client-csharp-bootstrap

Repository files navigation

BUILD ANALYZE Compatible with: Camunda Platform 8

Bootstrap extension for the C# Zeebe client

This project is not maintained anymore and is deprecated. The reason for this decision is the resent fork of this project by zeebe-client-csharp-accelerator. Please switch to the zeebe-client-csharp-accelerator which is a copy of this project with some small functional changes in the interface. Because of this switching should be relatively easy.

Apologies for the inconvenience but for the long term it will be better for the community to have one code base which is actively supported.

This project is an extension of the C# Zeebe client project. Zeebe Job handlers are automaticly recognized and bootstrapped via a .Net HostedService.

Read the Zeebe documentation for more information about the Zeebe project.

Requirements

How to use

The Zeebe C# client bootstrap extension is available via nuget (https://www.nuget.org/packages/zb-client-bootstrap/).

See examples and blog post for more information.

Quick start

All classes which implement IJobHandler<TJob>, IJobHandler<TJob, TResponse>, IAsyncJobHandler<TJob> or IAsyncJobHandler<TJob, TResponse> are automaticly found, added to the service collection and autowired to Zeebe when you register this bootstrap project with the IServiceCollection.BootstrapZeebe() extension method.

The BootstrapZeebe method has two parameters:

  1. ZeebeBootstrapOptions via configuration, action delegate or both.
  2. An array with assemblies which will be scanned for job handlers.
ConfigureServices((hostContext, services) => {
    services.BootstrapZeebe(
        hostContext.Configuration.GetSection("ZeebeBootstrap"),
        this.GetType().Assembly
    );
})

Job

The job is an implementation of AbstractJob. By default the simple name of the job is mapped to BPMN task job type. Job types must be unique. The default job configuration can be overwritten with AbstractJobAttribute implementations, see attributes for more information.

public class SimpleJob : AbstractJob
{
    public SimpleJob(IJob job) 
        : base(job)
    { 
        //Variable mapping logic can be added here.
    }
}

There is also a generic version AbstractJob<TState> which will automaticly deserialize job variables into a typed object. Each property is automaticly added to the FetchVariables collection when the FetchVariablesAttribute is not used.

public class SimpleJob : AbstractJob<SimpleJobState>
{
    public SimpleJob(IJob job, SimpleJobState state) 
        : base(job, state)
    {  }
}

public class SimpleJobState
{
    public bool Test { get; set; }
}

Job handler

The job handler is an implementation of IJobHandler<TJob>, IJobHandler<TJob, TResponse>, IAsyncJobHandler<TJob> or IAsyncJobHandler<TJob, TResponse>. Job handlers are automaticly added to the DI container, therefore you can use dependency injection inside the job handlers. The default job handler configuration can be overwritten with AbstractJobHandlerAttribute implementations, see attributes for more information.

public class SimpleJobHandler : IAsyncJobHandler<SimpleJob>
{
    public async Task HandleJob(SimpleJob job, CancellationToken cancellationToken)
    {  
        //TODO: make the handling idempotent.
        await Usecase.ExecuteAsync(cancellationToken);
    }
}

A handled job has three outcomes:

  1. The job has been handled without exceptions: this will automaticly result in a JobCompletedCommand beeing send to the broker. The TResponse is automaticly serialized and added to the JobCompletedCommand.
  2. An exception has been thrown while handling the job and the exception implements AbstractJobException: this wil automaticly result in a ThrowErrorCommand beeing send to the broker;
  3. Any other exception will automaticly result in a FailCommand beeing send to the broker;

The JobCompletedCommand accepts variables which are added to process instance. For this use case the job handler can be use with a second generic parameter IJobHandler<TJob, TResponse>. The response is automaticly serialized.

public class SimpleJobHandler : IAsyncJobHandler<SimpleJob, SimpleResponse>
{
    public async Task<SimpleResponse> HandleJob(SimpleJob job, CancellationToken cancellationToken)
    {
        //TODO: make the handling idempotent.
        var result = await Usecase.ExecuteAsync(cancellationToken);
        return new SimpleResponse(result);
    }
}

Extensions

  1. IPublishMessageCommandStep3, ICreateProcessInstanceCommandStep3 and ISetVariablesCommandStep1 are extended with the State(object state) method which uses the registered IZeebeVariablesSerializer service to automaticly serialize state and pass the result to the Variables(string variables) method.

Conventions

This project uses the following conventions:

  1. By default the simple name of the AbstractJob implementation is used to match the Type which is specified in the BPMN model. This can be overriden by adding the JobTypeAttribute to the AbstractJob implementation, see attributes for more information.
  2. By default the assembly name which contains the job handler is used as the Worker name. This can be overriden by adding the WorkerNameAttribute to the AbstractJob implementation, see attributes for more information.
  3. By default the job handlers are added to de DI container with a Transient service lifetime. This can be overriden by adding the ServiceLifetimeAttribute to the job handler, see attributes for more information.
  4. By default the ZeebeVariablesSerializer is registered as the implementation for IZeebeVariablesSerializer which uses System.Text.Json.JsonSerializer. You can override this registration by registering your service after the BootstrapZeebe method or you can register System.Text.Json.JsonSerializerOptions to configure the System.Text.Json.JsonSerializer.

How to build

Run dotnet build Zeebe.Client.Bootstrap.sln

How to test

Run dotnet test Zeebe.Client.Bootstrap.sln