Skip to content

Commit c91276b

Browse files
committed
Added Spring application example for cadence worker service
1 parent 7f2ff7e commit c91276b

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,14 @@ To run:
129129

130130
./gradlew -q execute -PmainClass=com.uber.cadence.samples.bookingsaga.TripBookingSaga
131131

132+
### Sprint Boot Application
132133

134+
Example of how to start a cadence worker service using Spring Boot Framework
135+
136+
To run:
137+
138+
# Start Cadence Server
139+
# see https://github.com/uber/cadence/tree/master/docker
140+
# register domain
141+
./gradlew -q execute -PmainClass=com.uber.cadence.samples.common.RegisterDomain
142+
./gradlew -q execute -PmainClass=com.uber.cadence.samples.spring.CadenceSamplesApplication

build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ plugins {
22
id 'net.minecrell.licenser' version '0.4.1'
33
id "com.github.sherter.google-java-format" version "0.9"
44
id "net.ltgt.errorprone" version "1.3.0"
5+
id 'org.springframework.boot' version '2.7.15'
6+
57
}
68

79
apply plugin: 'java'
810
apply plugin: 'maven'
911
apply plugin: 'com.github.sherter.google-java-format'
12+
apply plugin: 'io.spring.dependency-management'
1013

1114
googleJavaFormat {
1215
toolVersion '1.5'
@@ -46,6 +49,7 @@ dependencies {
4649
testCompile group: 'junit', name: 'junit', version: '4.12'
4750
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
4851
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.3'
52+
implementation 'org.springframework.boot:spring-boot-starter-web'
4953
}
5054

5155
compileJava {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.uber.cadence.samples.spring;
2+
3+
import com.uber.cadence.client.WorkflowClient;
4+
import com.uber.cadence.samples.spring.models.SampleMessage;
5+
import com.uber.cadence.samples.spring.workflows.HelloWorldWorkflow;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.annotation.Bean;
11+
12+
@SpringBootApplication
13+
public class CadenceSamplesApplication {
14+
public static void main(String[] args) {
15+
SpringApplication.run(CadenceSamplesApplication.class, args);
16+
}
17+
18+
// Example to start a workflow
19+
@Bean
20+
public CommandLineRunner startHelloWorkflow(ApplicationContext ctx) {
21+
return args -> {
22+
System.out.println("Start one synchronous HelloWorld workflow");
23+
24+
WorkflowClient workflowClient = ctx.getBean(WorkflowClient.class);
25+
HelloWorldWorkflow stub = workflowClient.newWorkflowStub(HelloWorldWorkflow.class);
26+
stub.sayHello(new SampleMessage("hello"));
27+
28+
System.out.println("Synchronous HelloWorld workflow finished");
29+
System.exit(SpringApplication.exit(ctx, () -> 0));
30+
};
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.uber.cadence.samples.spring.cadence;
2+
3+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
4+
import static com.uber.cadence.samples.spring.common.Constant.TASK_LIST;
5+
6+
import com.uber.cadence.client.WorkflowClient;
7+
import com.uber.cadence.client.WorkflowClientOptions;
8+
import com.uber.cadence.samples.spring.workflows.HelloWorldWorkflowImpl;
9+
import com.uber.cadence.serviceclient.ClientOptions;
10+
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
11+
import com.uber.cadence.worker.Worker;
12+
import com.uber.cadence.worker.WorkerFactory;
13+
import org.springframework.boot.context.event.ApplicationStartedEvent;
14+
import org.springframework.context.ApplicationContext;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
17+
import org.springframework.context.event.EventListener;
18+
19+
@Configuration
20+
public class CadenceAutoConfiguration {
21+
@Bean
22+
public WorkflowClient workflowClient() {
23+
return WorkflowClient.newInstance(
24+
new WorkflowServiceTChannel(ClientOptions.defaultInstance()),
25+
WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
26+
}
27+
28+
@EventListener(ApplicationStartedEvent.class)
29+
public void startWorker(ApplicationStartedEvent event) {
30+
System.out.println("Starting workers");
31+
32+
ApplicationContext context = event.getApplicationContext();
33+
WorkflowClient workflowClient = context.getBean(WorkflowClient.class);
34+
WorkerFactory factory = WorkerFactory.newInstance(workflowClient);
35+
Worker worker = factory.newWorker(TASK_LIST);
36+
worker.registerWorkflowImplementationTypes(HelloWorldWorkflowImpl.class);
37+
factory.start();
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.uber.cadence.samples.spring.common;
2+
3+
public class Constant {
4+
public static final String TASK_LIST = "cadence-samples-worker";
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.uber.cadence.samples.spring.models;
2+
3+
public class SampleMessage {
4+
String message;
5+
6+
public SampleMessage(String message) {
7+
this.message = message;
8+
}
9+
10+
public String GetMessage() {
11+
return this.message;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.uber.cadence.samples.spring.workflows;
2+
3+
import static com.uber.cadence.samples.spring.common.Constant.TASK_LIST;
4+
5+
import com.uber.cadence.samples.spring.models.SampleMessage;
6+
import com.uber.cadence.workflow.WorkflowMethod;
7+
8+
public interface HelloWorldWorkflow {
9+
@WorkflowMethod(
10+
executionStartToCloseTimeoutSeconds = 10,
11+
taskStartToCloseTimeoutSeconds = 10,
12+
taskList = TASK_LIST
13+
)
14+
String sayHello(SampleMessage message);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.uber.cadence.samples.spring.workflows;
2+
3+
import com.uber.cadence.samples.spring.models.SampleMessage;
4+
import com.uber.cadence.workflow.Workflow;
5+
import org.slf4j.Logger;
6+
7+
public class HelloWorldWorkflowImpl implements HelloWorldWorkflow {
8+
private final Logger logger = Workflow.getLogger(HelloWorldWorkflowImpl.class);
9+
10+
@Override
11+
public String sayHello(SampleMessage message) {
12+
logger.info("executing HelloWorldWorkflow::sayHello");
13+
14+
String result = "Hello, " + message.GetMessage();
15+
logger.info("output: " + result);
16+
return result;
17+
}
18+
}

0 commit comments

Comments
 (0)