Camunda External Task Client JS A Comprehensive Guide to Empower Your Workflow Automation

Introduction to Camunda External Task Client JS

The camunda-external-task-client-js library allows you to create powerful workflows by enabling interaction with Camunda BPMN engine through external tasks. It provides a highly scalable and efficient way to implement and automate business processes.

Setting Up

  
    npm install camunda-external-task-client-js --save
  

Client Configuration

The first step is to configure the client:

  
    const { Client, logger } = require('camunda-external-task-client-js');

    const config = {
      baseUrl: 'http://localhost:8080/engine-rest',
      use: logger
    };

    const client = new Client(config);
  

Subscribing to a Topic

Next, you need to subscribe to a topic to work on tasks:

  
    client.subscribe('example-topic', async function({ task, taskService }) {
      // Put your business logic here
      console.log('Task received:', task);
      await taskService.complete(task);
    });
  

Completing a Task with Variables

You can also complete a task with process variables:

  
    client.subscribe('payment-service', async function({ task, taskService }) {
      const amount = task.variables.get('amount');
      
      if (isPaymentSuccessful(amount)) {
        await taskService.complete(task, {
          paid: true
        });
      }
    });
  

Fetching and Locking External Tasks

To fetch and lock external tasks manually:

  
    const tasks = await client.fetchAndLock({
      workerId: 'worker-id',
      maxTasks: 5,
      topics: [{ topicName: 'some-topic', lockDuration: 10000 }]
    });
  

Handling BPMN Errors

Handle BPMN errors within your worker:

  
    client.subscribe('error-handling-service', async function({ task, taskService }) {
      try {
        // business logic
      } catch (e) {
        await taskService.handleBpmnError(task, 'BPMNErrorCode', 'Error message', { errorDetail: e.message });
      }
    });
  

Sample Application

Here’s a simple application that demonstrates the use of the above APIs:

  
    const { Client, logger } = require('camunda-external-task-client-js');

    const config = {
      baseUrl: 'http://localhost:8080/engine-rest',
      use: logger
    };

    const client = new Client(config);

    client.subscribe('order-processing', async function({ task, taskService }) {
      const orderId = task.variables.get('orderId');
      console.log(`Processing order ${orderId}`);

      try {
        // Simulate processing
        await processOrder(orderId);
        await taskService.complete(task);
      } catch (error) {
        await taskService.handleFailure(task, {
          errorMessage: error.message,
          retries: 0,
          retryTimeout: 1000
        });
      }
    });

    async function processOrder(orderId) {
      // Simulated processing logic
      console.log(`Order ${orderId} processed successfully.`);
    }
  

The above example subscribes to an ‘order-processing’ topic, simulates order processing, and handles errors if any.

By leveraging camunda-external-task-client-js, you can create robust, scalable workflow automation solutions effortlessly.

Hash: 88fd1e99e54fb92942aa5543658e904f981a6cf02a5652a7ce6f5f6e39ef4aa0

Leave a Reply

Your email address will not be published. Required fields are marked *