You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Camunda BPM中配置服务任务(Service Task)为外部任务(External Task)

How to Configure a Service Task as an External Task in Camunda BPM

Hey there! Let's walk through exactly how to turn a regular Service Task into an External Task in Camunda BPM—this pattern is perfect for offloading work to external systems, running long-running jobs, or keeping your Camunda engine lightweight by decoupling business logic.


1. Design Your Process in Camunda Modeler

  • Open Camunda Modeler and create a new BPMN diagram (or open an existing one you want to modify).
  • Drag a Service Task from the left-hand palette onto your canvas.
  • Click the task to open the properties panel on the right, and under the General tab, give it a clear, descriptive name (e.g., "Process Customer Payment" or "Fetch Inventory Data").

2. Switch the Service Task to External Implementation

  • Navigate to the Implementation tab in the properties panel.
  • From the Implementation Type dropdown, select External.
  • In the Topic field, enter a unique, consistent identifier (like payment-processing or inventory-fetch). This topic is how your external worker will recognize and pick up the task.

    Pro tip: Use topic names that map directly to your business logic—this makes it way easier to manage multiple workers later.

  • Optional extras:
    • Set a Priority (higher numbers mean the task gets picked up first by competing workers).
    • Add Extension Properties to pass static metadata to your external worker (e.g., API endpoints or retry limits).

3. Deploy the Process to Your Camunda Engine

  • Save your BPMN diagram (make sure it ends with .bpmn).
  • Deploy it to your Camunda engine using one of these methods:
    • Use Camunda Modeler's built-in deploy tool: Go to Deploy > Configure Connection to link to your engine, then deploy directly.
    • Upload via the Camunda Web UI: Head to Deployments > Create Deployment and upload your BPMN file.
    • Use the Camunda REST API: Send a POST request to /deployment/create with your BPMN file attached.

4. Build an External Worker to Handle the Task

You need a worker application that polls the Camunda engine for tasks matching your topic. Here are quick examples for two popular stacks:

Java Example (Official Camunda SDK)

import org.camunda.bpm.client.ExternalTaskClient;

public class PaymentWorker {
    public static void main(String[] args) {
        // Initialize the client
        ExternalTaskClient client = ExternalTaskClient.create()
                .baseUrl("http://localhost:8080/engine-rest")
                .asyncResponseTimeout(10000) // Wait 10s for new tasks
                .build();

        // Subscribe to your topic
        client.subscribe("payment-processing")
                .lockDuration(5000) // Lock task for 5s to prevent duplicate processing
                .handler((externalTask, taskService) -> {
                    // Pull process variables
                    String customerId = externalTask.getVariable("customerId");
                    Double amount = externalTask.getVariable("paymentAmount");

                    // Your business logic here (e.g., call payment gateway API)
                    System.out.printf("Processing payment for customer %s: $%.2f%n", customerId, amount);

                    // Optional: Set output variables
                    taskService.complete(externalTask);
                })
                .open();
    }
}

Node.js Example (Camunda External Task Client)

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

// Client configuration
const config = { baseUrl: "http://localhost:8080/engine-rest", use: logger };
const client = new Client(config);

// Subscribe to the topic
client.subscribe("payment-processing", async ({ task, taskService }) => {
    // Fetch variables from the process
    const customerId = task.variables.get("customerId");
    const amount = task.variables.get("paymentAmount");

    // Run your external logic
    console.log(`Processing payment for ${customerId}: $${amount}`);

    // Mark the task as complete
    await taskService.complete(task);
});

5. Test Your Setup

  • Start a process instance that includes your external task (use the Camunda Web UI, REST API, or Modeler's "Start Instance" feature).
  • Run your external worker application—you should see it pick up the task and execute your logic.
  • Verify success via the Camunda Web UI: Check Tasklist > External Tasks to confirm the task is marked as completed.

Quick Troubleshooting Tips

  • If the worker isn't picking up tasks:
    • Double-check that the topic name in the worker matches exactly what's set in the service task.
    • Ensure your Camunda engine is running and the worker's base URL is correct.
    • Confirm the process was deployed successfully (check Deployments in the Web UI).
  • If tasks stay locked:
    • Adjust the lockDuration in your worker to give enough time for your logic to finish.
    • Make sure your worker calls complete() (or handleFailure()/handleBpmnError() for errors) to release the lock.

内容的提问来源于stack exchange,提问作者Rudson Rodrigues

火山引擎 最新活动