diff --git a/src/main/java/io/iworkflow/controller/CounterWorkflowController.java b/src/main/java/io/iworkflow/controller/CounterWorkflowController.java new file mode 100644 index 0000000..fa969a6 --- /dev/null +++ b/src/main/java/io/iworkflow/controller/CounterWorkflowController.java @@ -0,0 +1,37 @@ +package io.iworkflow.controller; + +import io.iworkflow.core.Client; +import io.iworkflow.workflow.update.CounterWorkflow; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +@RequestMapping("/counter") +public class CounterWorkflowController { + + private final Client client; + + public CounterWorkflowController( + final Client client + ) { + this.client = client; + } + + @GetMapping("/start") + public ResponseEntity start( + @RequestParam String id + ) { + client.startWorkflow(CounterWorkflow.class, id, 3600); + return ResponseEntity.ok("success"); + } + + @GetMapping("/inc") + ResponseEntity verify( + @RequestParam String id) { + final CounterWorkflow rpcStub = client.newRpcStub(CounterWorkflow.class, id); + return ResponseEntity.ok(client.invokeRPC(rpcStub::inc)); + } +} \ No newline at end of file diff --git a/src/main/java/io/iworkflow/workflow/update/CounterWorkflow.java b/src/main/java/io/iworkflow/workflow/update/CounterWorkflow.java new file mode 100644 index 0000000..bef47c8 --- /dev/null +++ b/src/main/java/io/iworkflow/workflow/update/CounterWorkflow.java @@ -0,0 +1,50 @@ +package io.iworkflow.workflow.update; + +import io.iworkflow.core.Context; +import io.iworkflow.core.ObjectWorkflow; +import io.iworkflow.core.RPC; +import io.iworkflow.core.StateDef; +import io.iworkflow.core.communication.Communication; +import io.iworkflow.core.persistence.DataAttributeDef; +import io.iworkflow.core.persistence.Persistence; +import io.iworkflow.core.persistence.PersistenceFieldDef; +import io.iworkflow.gen.models.PersistenceLoadingType; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@Component + +public class CounterWorkflow implements ObjectWorkflow { + + public static final String DA_COUNT = "COUNT"; + + @Override + public List getWorkflowStates() { + return new ArrayList<>(); + } + + @Override + public List getPersistenceSchema() { + return Arrays.asList( + DataAttributeDef.create(Integer.class, DA_COUNT) + ); + } + + // Atomically read/write data attributes in RPC will use Temporal sync update features + @RPC( + dataAttributesLoadingType = PersistenceLoadingType.PARTIAL_WITH_EXCLUSIVE_LOCK, + dataAttributesLockingKeys = {DA_COUNT} + ) + public int inc(Context context, Persistence persistence, Communication communication) { + Integer count = persistence.getDataAttribute(DA_COUNT, Integer.class); + if (count == null) { + count = 0; + } + count++; + persistence.setDataAttribute(DA_COUNT, count); + return count; + } +}