Skip to content

Commit 82c5f38

Browse files
Add basic SpringBoot sample (temporalio#574)
1 parent a3c19c5 commit 82c5f38

26 files changed

Lines changed: 762 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ target
88
/build
99
/core/build
1010
/springboot/build
11+
/springboot-basic/build
1112
/out
1213
/core/out
1314
/springboot/out
15+
/springboot-basic/out
1416
.classpath
1517
.project
1618
.settings/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Temporal using the [Java SDK](https://github.com/temporalio/sdk-java).
66
It contains two modules:
77
* [Core](/core): showcases many different SDK features.
88
* [SpringBoot](/springboot): showcases SpringBoot autoconfig integration.
9+
* [SpringBoot Basic](/springboot-basic): Minimal sample showing SpringBoot autoconfig integration without any extra external dependencies.
910

1011
## Learn more about Temporal and Java SDK
1112

@@ -141,7 +142,12 @@ and follow simple instructions there.
141142

142143
1. Start SpringBoot from main repo dir:
143144

144-
./gradlew bootRun
145+
./gradlew :springboot:bootRun
146+
147+
To run the basic sample run
148+
149+
./gradlew :springboot-basic:bootRun
150+
145151

146152
2. Navigate to [localhost:3030](http://localhost:3030)
147153

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
rootProject.name = 'temporal-java-samples'
22
include 'core'
33
include 'springboot'
4-
4+
include 'springboot-basic'

springboot-basic/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'org.springframework.boot'
2+
3+
dependencies {
4+
implementation "org.springframework.boot:spring-boot-starter-web"
5+
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
6+
implementation "org.springframework.boot:spring-boot-starter-actuator"
7+
implementation "io.temporal:temporal-spring-boot-starter-alpha:$javaSDKVersion"
8+
testImplementation "org.springframework.boot:spring-boot-starter-test"
9+
runtimeOnly "io.micrometer:micrometer-registry-prometheus"
10+
dependencies {
11+
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')
12+
if (JavaVersion.current().isJava11Compatible()) {
13+
errorprone('com.google.errorprone:error_prone_core:2.22.0')
14+
} else {
15+
errorprone('com.google.errorprone:error_prone_core:2.22.0')
16+
}
17+
}
18+
}
19+
20+
bootJar {
21+
enabled = false
22+
}
23+
24+
jar {
25+
enabled = true
26+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.springboot;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.client.WorkflowOptions;
24+
import io.temporal.samples.springboot.hello.HelloWorkflow;
25+
import io.temporal.samples.springboot.hello.model.Person;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.http.HttpStatus;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.ResponseEntity;
30+
import org.springframework.stereotype.Controller;
31+
import org.springframework.ui.Model;
32+
import org.springframework.web.bind.annotation.*;
33+
34+
@Controller
35+
public class SamplesController {
36+
37+
@Autowired WorkflowClient client;
38+
39+
@GetMapping("/hello")
40+
public String hello(Model model) {
41+
model.addAttribute("sample", "Say Hello");
42+
return "hello";
43+
}
44+
45+
@PostMapping(
46+
value = "/hello",
47+
consumes = {MediaType.APPLICATION_JSON_VALUE},
48+
produces = {MediaType.TEXT_HTML_VALUE})
49+
ResponseEntity<String> helloSample(@RequestBody Person person) {
50+
HelloWorkflow workflow =
51+
client.newWorkflowStub(
52+
HelloWorkflow.class,
53+
WorkflowOptions.newBuilder()
54+
.setTaskQueue("HelloSampleTaskQueue")
55+
.setWorkflowId("HelloSample")
56+
.build());
57+
58+
// bypass thymeleaf, don't return template name just result
59+
return new ResponseEntity<>("\"" + workflow.sayHello(person) + "\"", HttpStatus.OK);
60+
}
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.springboot;
21+
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
@SpringBootApplication
26+
public class TemporalSpringbootSamplesApplication {
27+
public static void main(String[] args) {
28+
SpringApplication.run(TemporalSpringbootSamplesApplication.class, args).start();
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.springboot.hello;
21+
22+
import io.temporal.activity.ActivityInterface;
23+
import io.temporal.samples.springboot.hello.model.Person;
24+
25+
@ActivityInterface
26+
public interface HelloActivity {
27+
String hello(Person person);
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.springboot.hello;
21+
22+
import io.temporal.samples.springboot.hello.model.Person;
23+
import io.temporal.spring.boot.ActivityImpl;
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.stereotype.Component;
26+
27+
@Component
28+
@ActivityImpl(taskQueues = "HelloSampleTaskQueue")
29+
public class HelloActivityImpl implements HelloActivity {
30+
@Value("${samples.data.language}")
31+
private String language;
32+
33+
@Override
34+
public String hello(Person person) {
35+
String greeting = language.equals("spanish") ? "Hola " : "Hello ";
36+
return greeting + person.getFirstName() + " " + person.getLastName() + "!";
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.springboot.hello;
21+
22+
import io.temporal.samples.springboot.hello.model.Person;
23+
import io.temporal.workflow.WorkflowInterface;
24+
import io.temporal.workflow.WorkflowMethod;
25+
26+
@WorkflowInterface
27+
public interface HelloWorkflow {
28+
@WorkflowMethod
29+
String sayHello(Person person);
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.springboot.hello;
21+
22+
import io.temporal.activity.ActivityOptions;
23+
import io.temporal.samples.springboot.hello.model.Person;
24+
import io.temporal.spring.boot.WorkflowImpl;
25+
import io.temporal.workflow.Workflow;
26+
import java.time.Duration;
27+
28+
@WorkflowImpl(taskQueues = "HelloSampleTaskQueue")
29+
public class HelloWorkflowImpl implements HelloWorkflow {
30+
31+
private HelloActivity activity =
32+
Workflow.newActivityStub(
33+
HelloActivity.class,
34+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build());
35+
36+
@Override
37+
public String sayHello(Person person) {
38+
return activity.hello(person);
39+
}
40+
}

0 commit comments

Comments
 (0)