Skip to content

Commit 918327a

Browse files
author
Maxim Fateev
committed
Added HelloWorld and HelloSignal
1 parent ffac3cc commit 918327a

4 files changed

Lines changed: 218 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ After cadence-client library is available just run
2121

2222
to build the samples. Verify that they actually can run:
2323

24-
mvn exec:java -Dexec.mainClass=com.uber.cadence.samples.helloworld.HelloWorld
24+
mvn exec:java -Dexec.mainClass=com.uber.cadence.samples.helloworld.HelloActivity
2525

2626
## Overview of the Samples
2727

@@ -84,7 +84,7 @@ you've built them using the preceding instructions.
8484

8585
To run hello world:
8686

87-
mvn exec:java -Dexec.mainClass=com.uber.cadence.samples.helloworld.HelloWorld
87+
mvn exec:java -Dexec.mainClass=com.uber.cadence.samples.helloworld.HelloActivity
8888

8989
### File Processing
9090

src/main/java/com/uber/cadence/samples/helloworld/HelloWorld.java renamed to src/main/java/com/uber/cadence/samples/helloworld/HelloActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
* Hello World Cadence workflow that executes a single activity.
3030
* Requires a local instance of Cadence server running.
3131
*/
32-
public class HelloWorld {
32+
public class HelloActivity {
3333

34-
private static final String TASK_LIST = "HelloWorld";
34+
private static final String TASK_LIST = "HelloActivity";
3535

3636
/**
3737
* Workflow interface has to have at least one method annotated with @WorkflowMethod.
@@ -58,14 +58,15 @@ public static class GreetingWorkflowImpl implements GreetingWorkflow {
5858

5959
/**
6060
* Activity stub implements activity interface and proxies calls to it to Cadence activity invocations.
61+
* As activities are reentrant only a single stub can be used for multiple activity invocations.
6162
*/
62-
private final HelloWorld.GreetingActivities activities = Workflow.newActivityStub(
63+
private final GreetingActivities activities = Workflow.newActivityStub(
6364
GreetingActivities.class,
6465
new ActivityOptions.Builder().setScheduleToCloseTimeoutSeconds(10).build());
6566

6667
@Override
6768
public String getGreeting(String name) {
68-
// This is blocking call that returns only after
69+
// This is blocking call that returns only after activity is completed.
6970
return activities.composeGreeting("Hello", name );
7071
}
7172
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
package com.uber.cadence.samples.helloworld;
18+
19+
import com.uber.cadence.client.CadenceClient;
20+
import com.uber.cadence.client.WorkflowOptions;
21+
import com.uber.cadence.worker.Worker;
22+
import com.uber.cadence.workflow.Workflow;
23+
import com.uber.cadence.workflow.WorkflowMethod;
24+
import org.apache.log4j.BasicConfigurator;
25+
import org.apache.log4j.Level;
26+
import org.apache.log4j.Logger;
27+
28+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
29+
30+
/**
31+
* Hello World Cadence workflow that executes a single child workflow.
32+
* Requires a local instance of Cadence server running.
33+
*/
34+
public class HelloChild {
35+
36+
private static final String TASK_LIST = "HelloChild";
37+
38+
/**
39+
* Workflow interface has to have at least one method annotated with @WorkflowMethod.
40+
*/
41+
public interface GreetingWorkflow {
42+
/**
43+
* @return greeting string
44+
*/
45+
@WorkflowMethod
46+
String getGreeting(String name);
47+
}
48+
49+
/**
50+
* Activity interface is just a POJI
51+
*/
52+
public interface GreetingChild {
53+
@WorkflowMethod
54+
String composeGreeting(String greeting, String name);
55+
}
56+
57+
/**
58+
* GreetingWorkflow implementation that calls GreetingsActivities#printIt.
59+
*/
60+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
61+
62+
@Override
63+
public String getGreeting(String name) {
64+
// Workflows are stateful. So new stub must be created for each new child.
65+
GreetingChild child = Workflow.newChildWorkflowStub(GreetingChild.class);
66+
67+
// This is blocking call that returns only after child is completed.
68+
return child.composeGreeting("Hello", name );
69+
}
70+
}
71+
72+
/**
73+
* Child workflow implementation.
74+
* Workflow implementation must always be public for the Cadence to be able to create instances.
75+
*/
76+
public static class GreetingChildImpl implements GreetingChild {
77+
@Override
78+
public String composeGreeting(String greeting, String name) {
79+
return greeting + " " + name + "!";
80+
}
81+
}
82+
83+
public static void main(String[] args) {
84+
BasicConfigurator.configure();
85+
Logger.getRootLogger().setLevel(Level.WARN);
86+
87+
// Start a worker that hosts both parent and child workflow implementations.
88+
Worker worker = new Worker(DOMAIN, TASK_LIST);
89+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class, GreetingChildImpl.class);
90+
// Start listening to the workflow task list.
91+
worker.start();
92+
93+
// Start a workflow execution. Usually it is done from another program.
94+
CadenceClient cadenceClient = CadenceClient.newInstance(DOMAIN);
95+
// Get a workflow stub using the same task list the worker uses.
96+
WorkflowOptions workflowOptions = new WorkflowOptions.Builder()
97+
.setTaskList(TASK_LIST)
98+
.setExecutionStartToCloseTimeoutSeconds(30)
99+
.build();
100+
GreetingWorkflow workflow = cadenceClient.newWorkflowStub(GreetingWorkflow.class,
101+
workflowOptions);
102+
// Execute a workflow waiting for it complete.
103+
String greeting = workflow.getGreeting("World");
104+
System.out.println(greeting);
105+
System.exit(0);
106+
}
107+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
package com.uber.cadence.samples.helloworld;
18+
19+
import com.uber.cadence.WorkflowExecution;
20+
import com.uber.cadence.WorkflowIdReusePolicy;
21+
import com.uber.cadence.client.CadenceClient;
22+
import com.uber.cadence.client.WorkflowOptions;
23+
import com.uber.cadence.worker.Worker;
24+
import com.uber.cadence.workflow.ActivityOptions;
25+
import com.uber.cadence.workflow.CompletablePromise;
26+
import com.uber.cadence.workflow.Promise;
27+
import com.uber.cadence.workflow.SignalMethod;
28+
import com.uber.cadence.workflow.Workflow;
29+
import com.uber.cadence.workflow.WorkflowException;
30+
import com.uber.cadence.workflow.WorkflowMethod;
31+
32+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
33+
34+
/**
35+
* Hello World Cadence workflow that blocks until a signal is received.
36+
* Requires a local instance of Cadence server running.
37+
*/
38+
public class HelloSignal {
39+
40+
private static final String TASK_LIST = "HelloSignal";
41+
42+
/**
43+
* Workflow interface has to have at least one method annotated with @WorkflowMethod.
44+
*/
45+
public interface GreetingWorkflow {
46+
/**
47+
* @return greeting string
48+
*/
49+
@WorkflowMethod
50+
String getGreeting();
51+
52+
/**
53+
* Receives name through an external signal.
54+
*/
55+
@SignalMethod
56+
void waitForName(String name);
57+
}
58+
59+
/**
60+
* GreetingWorkflow implementation that calls GreetingsActivities#printIt.
61+
*/
62+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
63+
64+
private final CompletablePromise<String> name = Workflow.newCompletablePromise();
65+
66+
@Override
67+
public String getGreeting() {
68+
return "Hello " + name.get() + "!";
69+
}
70+
71+
@Override
72+
public void waitForName(String name) {
73+
this.name.complete(name);
74+
}
75+
}
76+
77+
public static void main(String[] args) {
78+
// Start a worker that hosts the workflow implementation
79+
Worker worker = new Worker(DOMAIN, TASK_LIST);
80+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
81+
worker.start();
82+
83+
// Start a workflow execution. Usually it is done from another program.
84+
CadenceClient cadenceClient = CadenceClient.newInstance(DOMAIN);
85+
// Get a workflow stub using the same task list the worker uses.
86+
WorkflowOptions workflowOptions = new WorkflowOptions.Builder()
87+
.setTaskList(TASK_LIST)
88+
.setExecutionStartToCloseTimeoutSeconds(30)
89+
.build();
90+
GreetingWorkflow workflow = cadenceClient.newWorkflowStub(GreetingWorkflow.class,
91+
workflowOptions);
92+
// Start workflow asynchronously to not use another thread to signal.
93+
WorkflowExecution execution = CadenceClient.asyncStart(workflow::getGreeting);
94+
// After asyncStart for getGreeting returns the workflow is guaranteed to be started.
95+
// So we can send signal to it using workflow stub.
96+
workflow.waitForName("World");
97+
// Calling synchronous getGreeting after workflow has started reconnects to the existing workflow and
98+
// blocks until result is available. Note this behavior assumes that WorkflowOptions are not configured
99+
// with WorkflowIdReusePolicy.AllowDuplicate. In that case the call would fail with IllegalStateException.
100+
String greeting = workflow.getGreeting();
101+
System.out.println(greeting);
102+
System.exit(0);
103+
}
104+
}

0 commit comments

Comments
 (0)