Skip to content

Commit 28daaa6

Browse files
committed
workflow timeout and update
1 parent 98fac0c commit 28daaa6

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
worker:
2+
ls *.java | entr -r \
3+
sh -c 'cd $$(git rev-parse --show-toplevel) && ./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyWorker'
4+
5+
run:
6+
cd $$(git rev-parse --show-toplevel) && \
7+
./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyStarter
8+
9+
.PHONY: run worker
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.temporal.samples.basic;
2+
3+
import io.temporal.activity.ActivityInterface;
4+
import io.temporal.activity.ActivityMethod;
5+
6+
@ActivityInterface
7+
public interface MyActivity {
8+
9+
@ActivityMethod
10+
int myActivityMethod();
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.temporal.samples.basic;
2+
3+
public class MyActivityImpl implements MyActivity {
4+
@Override
5+
public int myActivityMethod() {
6+
return 7;
7+
}
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.temporal.samples.basic;
2+
3+
import io.temporal.api.enums.v1.WorkflowIdConflictPolicy;
4+
import io.temporal.client.WorkflowClient;
5+
import io.temporal.client.WorkflowOptions;
6+
import io.temporal.serviceclient.WorkflowServiceStubs;
7+
8+
public class MyStarter {
9+
10+
static final String WORKFLOW_ID = "wid";
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
15+
16+
WorkflowClient client = WorkflowClient.newInstance(service);
17+
18+
WorkflowOptions workflowOptions =
19+
WorkflowOptions.newBuilder()
20+
.setTaskQueue(MyWorker.TASK_QUEUE)
21+
.setWorkflowId(WORKFLOW_ID)
22+
.setWorkflowIdConflictPolicy(
23+
WorkflowIdConflictPolicy.WORKFLOW_ID_CONFLICT_POLICY_TERMINATE_EXISTING)
24+
.setWorkflowExecutionTimeout(java.time.Duration.ofSeconds(2))
25+
.build();
26+
MyWorkflow workflow = client.newWorkflowStub(MyWorkflow.class, workflowOptions);
27+
28+
WorkflowClient.start(workflow::run);
29+
30+
int upResult = workflow.myUpdate();
31+
System.out.println("upResult: " + upResult);
32+
33+
int wfResult = workflow.run();
34+
System.out.println("wfResult: " + wfResult);
35+
36+
System.exit(0);
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.temporal.samples.basic;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.serviceclient.WorkflowServiceStubs;
5+
import io.temporal.worker.Worker;
6+
import io.temporal.worker.WorkerFactory;
7+
8+
public class MyWorker {
9+
static final String TASK_QUEUE = "tq";
10+
11+
public static void main(String[] args) throws Exception {
12+
13+
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
14+
15+
WorkflowClient client = WorkflowClient.newInstance(service);
16+
17+
WorkerFactory factory = WorkerFactory.newInstance(client);
18+
Worker worker = factory.newWorker(TASK_QUEUE);
19+
worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class);
20+
worker.registerActivitiesImplementations(new MyActivityImpl());
21+
factory.start();
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.temporal.samples.basic;
2+
3+
import io.temporal.workflow.UpdateMethod;
4+
import io.temporal.workflow.WorkflowInterface;
5+
import io.temporal.workflow.WorkflowMethod;
6+
7+
@WorkflowInterface
8+
public interface MyWorkflow {
9+
@WorkflowMethod
10+
int run();
11+
12+
@UpdateMethod
13+
int myUpdate();
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.temporal.samples.basic;
2+
3+
import io.temporal.activity.ActivityOptions;
4+
import io.temporal.common.RetryOptions;
5+
import io.temporal.workflow.Workflow;
6+
import java.time.Duration;
7+
8+
public class MyWorkflowImpl implements MyWorkflow {
9+
10+
private final MyActivity activity =
11+
Workflow.newActivityStub(
12+
MyActivity.class,
13+
ActivityOptions.newBuilder()
14+
.setStartToCloseTimeout(Duration.ofSeconds(2))
15+
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build())
16+
.build());
17+
18+
// private boolean done;
19+
20+
@Override
21+
public int run() {
22+
Workflow.sleep(Duration.ofSeconds(3));
23+
// Workflow.await(() -> done);
24+
return 8;
25+
}
26+
27+
@Override
28+
public int myUpdate() {
29+
Workflow.sleep(Duration.ofSeconds(2));
30+
int result = activity.myActivityMethod();
31+
// done = true;
32+
return result;
33+
}
34+
}

0 commit comments

Comments
 (0)