Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> start(
@RequestParam String id
) {
client.startWorkflow(CounterWorkflow.class, id, 3600);
return ResponseEntity.ok("success");
}

@GetMapping("/inc")
ResponseEntity<Integer> verify(
@RequestParam String id) {
final CounterWorkflow rpcStub = client.newRpcStub(CounterWorkflow.class, id);
return ResponseEntity.ok(client.invokeRPC(rpcStub::inc));
}
}
50 changes: 50 additions & 0 deletions src/main/java/io/iworkflow/workflow/update/CounterWorkflow.java
Original file line number Diff line number Diff line change
@@ -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<StateDef> getWorkflowStates() {
return new ArrayList<>();
}

@Override
public List<PersistenceFieldDef> 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;
}
}