/camunda-worker-akka

Worker for ExternalTask based on Akka - running with Camunda BPM

Primary LanguageScalaApache License 2.0Apache-2.0

camunda-worker-akka

Implement workers for external tasks in Camunda BPM in Scala, based on Akka.

Alternative Versions:

Summary

This tool provides a Scala template to external tasks exposed by the process engine. You can build a scala worker based on the template.

Getting started

Requirements

  • SBT to build the application

Test the application local with

sbt "test:runMain org.camunda.worker.akka.Main"

Build the application with

sbt assembly

Deploy the application with

sbt publishLocal

See example of using the akka worker in Order Processing Microservices example.

Using the template

Write a launch class:

object Main extends App {
 
  // create actor system
  val system = ActorSystem("MyActorSystem")
  
  // create worker
  val worker = system.actorOf(Props[PaymentWorker], name = "payment-worker")
  
  // start polling
  val pollActor = system.actorOf(PollActor.props(hostAddress = "http://localhost:8080/engine-rest", maxTasks = 5, waitTime= 100, lockTime = 600))
  pollActor ! Poll(topicName = "payment", worker, variableNames = List("order"))
  
}

Write a worker:

class PaymentWorker extends Worker {

  def work(task: LockedTask): Map[String, VariableValue] = {
    // resolve variables from process instance
    val orderId: String = task.variable[JValue]("order") match {
      case Some(json) => (json \ "orderId").extract[String]
      case None       => throw new IllegalArgumentException("order is not available")
    }
  
    // do the work
    val payment = calculatePayment(orderId)
    // return the result which will set as variable of the process instance
    Map("payment" -> payment)
  }
  
}