Skip to content

Commit 6a1dbea

Browse files
committed
wip
1 parent 3b83278 commit 6a1dbea

8 files changed

Lines changed: 369 additions & 58 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ subprojects {
1212
apply plugin: 'com.diffplug.spotless'
1313

1414
compileJava {
15-
options.compilerArgs << "-Werror"
15+
// options.compilerArgs << "-Werror"
1616
}
1717

1818
java {

core/src/main/java/io/temporal/samples/hello/HelloActivity.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.temporal.activity.ActivityMethod;
2424
import io.temporal.activity.ActivityOptions;
2525
import io.temporal.client.WorkflowClient;
26-
import io.temporal.client.WorkflowOptions;
2726
import io.temporal.serviceclient.WorkflowServiceStubs;
2827
import io.temporal.worker.Worker;
2928
import io.temporal.worker.WorkerFactory;
@@ -79,7 +78,7 @@ public interface GreetingActivities {
7978

8079
// Define your activity method which can be called during workflow execution
8180
@ActivityMethod(name = "greet")
82-
String composeGreeting(String greeting, String name);
81+
int composeGreeting(String greeting, String name);
8382
}
8483

8584
// Define the workflow implementation which implements our getGreeting workflow method.
@@ -103,7 +102,12 @@ public static class GreetingWorkflowImpl implements GreetingWorkflow {
103102
@Override
104103
public String getGreeting(String name) {
105104
// This is a blocking call that returns only after the activity has completed.
106-
return activities.composeGreeting("Hello", name);
105+
106+
activities.composeGreeting("Hello", name);
107+
108+
Workflow.sleep(Duration.ofSeconds(20));
109+
110+
return "hello";
107111
}
108112
}
109113

@@ -112,9 +116,10 @@ static class GreetingActivitiesImpl implements GreetingActivities {
112116
private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class);
113117

114118
@Override
115-
public String composeGreeting(String greeting, String name) {
119+
public int composeGreeting(String greeting, String name) {
116120
log.info("Composing greeting...");
117-
return greeting + " " + name + "!";
121+
122+
return 1;
118123
}
119124
}
120125

@@ -161,27 +166,5 @@ public static void main(String[] args) {
161166
* The started workers then start polling for workflows and activities.
162167
*/
163168
factory.start();
164-
165-
// Create the workflow client stub. It is used to start our workflow execution.
166-
GreetingWorkflow workflow =
167-
client.newWorkflowStub(
168-
GreetingWorkflow.class,
169-
WorkflowOptions.newBuilder()
170-
.setWorkflowId(WORKFLOW_ID)
171-
.setTaskQueue(TASK_QUEUE)
172-
.build());
173-
174-
/*
175-
* Execute our workflow and wait for it to complete. The call to our getGreeting method is
176-
* synchronous.
177-
*
178-
* See {@link io.temporal.samples.hello.HelloSignal} for an example of starting workflow
179-
* without waiting synchronously for its result.
180-
*/
181-
String greeting = workflow.getGreeting("World");
182-
183-
// Display workflow execution results
184-
System.out.println(greeting);
185-
System.exit(0);
186169
}
187170
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.hello;
21+
22+
import io.temporal.activity.*;
23+
import io.temporal.client.WorkflowClient;
24+
import io.temporal.client.WorkflowOptions;
25+
import io.temporal.common.RetryOptions;
26+
import io.temporal.serviceclient.WorkflowServiceStubs;
27+
import io.temporal.worker.Worker;
28+
import io.temporal.worker.WorkerFactory;
29+
import io.temporal.workflow.*;
30+
import java.time.Duration;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
36+
public class HelloActivityCancel {
37+
38+
static final String TASK_QUEUE = "HelloActivityTaskQueue";
39+
40+
static final String WORKFLOW_ID = "HelloActivityWorkflow";
41+
42+
@WorkflowInterface
43+
public interface GreetingWorkflow {
44+
45+
@WorkflowMethod
46+
String getGreeting(String name);
47+
}
48+
49+
@WorkflowInterface
50+
public interface ChildGreetingWorkflow {
51+
52+
@WorkflowMethod
53+
String getGreeting(String name);
54+
}
55+
56+
public static class ChildGreetingWorkflowImpl implements ChildGreetingWorkflow {
57+
58+
@Override
59+
public String getGreeting(final String name) {
60+
61+
Workflow.sleep(Duration.ofSeconds(30));
62+
63+
return null;
64+
}
65+
}
66+
67+
@ActivityInterface
68+
public interface GreetingActivities {
69+
70+
@ActivityMethod(name = "greet")
71+
String composeGreeting(String greeting, String name);
72+
}
73+
74+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
75+
76+
private final GreetingActivities activities =
77+
Workflow.newActivityStub(
78+
GreetingActivities.class,
79+
ActivityOptions.newBuilder()
80+
.setCancellationType(ActivityCancellationType.WAIT_CANCELLATION_COMPLETED)
81+
.setStartToCloseTimeout(Duration.ofSeconds(20))
82+
.setHeartbeatTimeout(Duration.ofSeconds(3))
83+
.setRetryOptions(
84+
RetryOptions.newBuilder().setInitialInterval(Duration.ofSeconds(10)).build())
85+
.build());
86+
87+
@Override
88+
public String getGreeting(String name) {
89+
String hello = null;
90+
91+
try {
92+
93+
final List<Promise<String>> promises = new ArrayList<>();
94+
95+
ChildWorkflowStub child =
96+
Workflow.newUntypedChildWorkflowStub(
97+
ChildGreetingWorkflow.class.getSimpleName(),
98+
ChildWorkflowOptions.newBuilder()
99+
//
100+
// .setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON)
101+
//
102+
// .setCancellationType(ChildWorkflowCancellationType.WAIT_CANCELLATION_REQUESTED)
103+
.setWorkflowId("Child_of_" + WORKFLOW_ID)
104+
.build());
105+
promises.add(child.executeAsync(String.class, "Hello", name));
106+
107+
// Wait for the child workflow to start before returning the result
108+
// Promise<WorkflowExecution> childExecution = child.getExecution();
109+
// WorkflowExecution childWorkflowExecution = childExecution.get();
110+
111+
promises.add(Async.function(activities::composeGreeting, "Hello", name));
112+
113+
for (int i = promises.size() - 1; i >= 0; i--) {
114+
115+
try {
116+
promises.get(i).get();
117+
} catch (Exception e) {
118+
System.out.println("In for promise >>>>>>> " + e);
119+
}
120+
}
121+
122+
} catch (Exception e) {
123+
System.out.println("Something cancelled " + e);
124+
// throw e;
125+
}
126+
127+
return hello;
128+
}
129+
}
130+
131+
static class GreetingActivitiesImpl implements GreetingActivities {
132+
private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class);
133+
134+
@Override
135+
public String composeGreeting(String greeting, String name) {
136+
log.info("Composing greeting...");
137+
138+
// int i = 0;
139+
140+
try {
141+
142+
for (int i = 0; i < 15; i++) {
143+
144+
System.out.println(">>>>>>> heartbeat " + i);
145+
try {
146+
Activity.getExecutionContext().heartbeat("" + i);
147+
} catch (Exception e) {
148+
System.out.println(">>>>>>> heartbeat " + e);
149+
150+
// return greeting + " " + name + "!";
151+
throw e;
152+
}
153+
154+
try {
155+
Thread.sleep(1000);
156+
} catch (InterruptedException e) {
157+
throw new RuntimeException(e);
158+
}
159+
}
160+
} catch (Exception e) {
161+
System.out.println(">>>>>>> " + e);
162+
163+
// return greeting + " " + name + "!";
164+
throw e;
165+
}
166+
return greeting + " " + name + "!";
167+
}
168+
}
169+
170+
/**
171+
* With our Workflow and Activities defined, we can now start execution. The main method starts
172+
* the worker and then the workflow.
173+
*/
174+
public static void main(String[] args) {
175+
176+
// Get a Workflow service stub.
177+
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
178+
179+
/*
180+
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
181+
*/
182+
WorkflowClient client = WorkflowClient.newInstance(service);
183+
184+
/*
185+
* Define the workflow factory. It is used to create workflow workers for a specific task queue.
186+
*/
187+
WorkerFactory factory = WorkerFactory.newInstance(client);
188+
189+
/*
190+
* Define the workflow worker. Workflow workers listen to a defined task queue and process
191+
* workflows and activities.
192+
*/
193+
Worker worker = factory.newWorker(TASK_QUEUE);
194+
195+
/*
196+
* Register our workflow implementation with the worker.
197+
* Workflow implementations must be known to the worker at runtime in
198+
* order to dispatch workflow tasks.
199+
*/
200+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
201+
worker.registerWorkflowImplementationTypes(ChildGreetingWorkflowImpl.class);
202+
203+
/*
204+
* Register our Activity Types with the Worker. Since Activities are stateless and thread-safe,
205+
* the Activity Type is a shared instance.
206+
*/
207+
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
208+
209+
/*
210+
* Start all the workers registered for a specific task queue.
211+
* The started workers then start polling for workflows and activities.
212+
*/
213+
factory.start();
214+
215+
// Create the workflow client stub. It is used to start our workflow execution.
216+
GreetingWorkflow workflow =
217+
client.newWorkflowStub(
218+
GreetingWorkflow.class,
219+
WorkflowOptions.newBuilder()
220+
.setWorkflowId(WORKFLOW_ID)
221+
.setTaskQueue(TASK_QUEUE)
222+
.build());
223+
224+
WorkflowClient.start(workflow::getGreeting, "World");
225+
226+
try {
227+
Thread.sleep(2000);
228+
} catch (InterruptedException e) {
229+
throw new RuntimeException(e);
230+
}
231+
232+
System.out.println("About to cancel.. ");
233+
client.newUntypedWorkflowStub(WORKFLOW_ID).cancel();
234+
System.out.println("cancellation request sent .. ");
235+
236+
// System.exit(0);
237+
}
238+
}

0 commit comments

Comments
 (0)