Skip to content

Commit 1e2f3f2

Browse files
committed
Scratchpad
1 parent ec1c615 commit 1e2f3f2

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.temporal.samples.hello;
2+
3+
import io.temporal.activity.ActivityInterface;
4+
import io.temporal.activity.ActivityOptions;
5+
import io.temporal.api.enums.v1.WorkflowIdReusePolicy;
6+
import io.temporal.client.WorkflowClient;
7+
import io.temporal.client.WorkflowOptions;
8+
import io.temporal.serviceclient.WorkflowServiceStubs;
9+
import io.temporal.worker.Worker;
10+
import io.temporal.worker.WorkerFactory;
11+
import io.temporal.workflow.Workflow;
12+
import io.temporal.workflow.WorkflowInterface;
13+
import io.temporal.workflow.WorkflowMethod;
14+
import java.time.Duration;
15+
16+
public class Scratchpad {
17+
static final String TASK_QUEUE = "ScratchpadTaskQueue";
18+
static final String WORKFLOW_ID = "ScratchpadWorkflow";
19+
20+
@WorkflowInterface
21+
public interface MyWorkflow {
22+
@WorkflowMethod
23+
String getGreeting(String name);
24+
}
25+
26+
@ActivityInterface
27+
public interface MyActivities {
28+
String composeGreeting(String greeting, String name);
29+
}
30+
31+
public static class MyWorkflowImpl implements MyWorkflow {
32+
33+
private final MyActivities activities =
34+
Workflow.newActivityStub(
35+
MyActivities.class,
36+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build());
37+
38+
@Override
39+
public String getGreeting(String name) {
40+
return activities.composeGreeting("Hello", name);
41+
}
42+
}
43+
44+
static class MyActivitiesImpl implements MyActivities {
45+
@Override
46+
public String composeGreeting(String greeting, String name) {
47+
return greeting + " " + name + "!";
48+
}
49+
}
50+
51+
public static void main(String[] args) {
52+
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
53+
WorkflowClient client = WorkflowClient.newInstance(service);
54+
WorkerFactory factory = WorkerFactory.newInstance(client);
55+
Worker worker = factory.newWorker(TASK_QUEUE);
56+
worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class);
57+
worker.registerActivitiesImplementations(new MyActivitiesImpl());
58+
factory.start();
59+
MyWorkflow workflow =
60+
client.newWorkflowStub(
61+
MyWorkflow.class,
62+
WorkflowOptions.newBuilder()
63+
.setWorkflowId(WORKFLOW_ID)
64+
.setTaskQueue(TASK_QUEUE)
65+
.setWorkflowIdReusePolicy(
66+
WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_TERMINATE_IF_RUNNING)
67+
.build());
68+
69+
String greeting = workflow.getGreeting("World");
70+
71+
System.out.println(greeting);
72+
System.exit(0);
73+
}
74+
}

0 commit comments

Comments
 (0)