Skip to content

Commit 15415ef

Browse files
authored
SpringBoot - Apache Camel Sample (#581)
Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent aac2301 commit 15415ef

File tree

18 files changed

+534
-3
lines changed

18 files changed

+534
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ More info on each sample:
160160
- [**Synchronous Update**](/springboot/src/main/java/io/temporal/samples/springboot/update): Learn how to use Synchronous Update feature with this purchase sample
161161
- [**Kafka Request / Reply**](/springboot/src/main/java/io/temporal/samples/springboot/kafka): Sample showing possible integration with event streaming platforms such as Kafka
162162
- [**Customize Options**](/springboot/src/main/java/io/temporal/samples/springboot/customize): Sample showing how to customize options such as WorkerOptions, WorkerFactoryOptions, etc (see options config [here](springboot/src/main/java/io/temporal/samples/springboot/customize/TemporalOptionsConfig.java))
163-
163+
- [**Apache Camel Route**](/springboot/src/main/java/io/temporal/samples/springboot/camel): Sample showing how to start Workflow execution from a Camel Route
164164

165165
#### Temporal Cloud
166166
To run any of the SpringBoot samples in your Temporal Cloud namespace:

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ subprojects {
2929
otelVersion = '1.30.1'
3030
otelVersionAlpha = "${otelVersion}-alpha"
3131
javaSDKVersion = '1.22.3'
32+
camelVersion = '3.22.1'
3233
jarVersion = '1.0.0'
3334
}
3435

springboot/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ dependencies {
99
// we set this as impl depends to use embedded kafka in samples not just tests
1010
implementation "org.springframework.kafka:spring-kafka-test"
1111
implementation "io.temporal:temporal-spring-boot-starter-alpha:$javaSDKVersion"
12+
implementation "org.apache.camel.springboot:camel-spring-boot-starter:$camelVersion"
13+
implementation "org.apache.camel.springboot:camel-servlet-starter:$camelVersion"
1214
runtimeOnly "io.micrometer:micrometer-registry-prometheus"
1315
runtimeOnly "com.h2database:h2"
1416
testImplementation "org.springframework.boot:spring-boot-starter-test"

springboot/src/main/java/io/temporal/samples/springboot/SamplesController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ResponseEntity purchase(@RequestBody Purchase purchase) {
127127
}
128128

129129
@GetMapping("/kafka")
130-
public String afka(Model model) {
130+
public String kafka(Model model) {
131131
model.addAttribute("sample", "Kafka Request / Reply");
132132
return "kafka";
133133
}
@@ -176,4 +176,10 @@ ResponseEntity customizeSample() {
176176
// bypass thymeleaf, don't return template name just result
177177
return new ResponseEntity<>("\"" + workflow.execute() + "\"", HttpStatus.OK);
178178
}
179+
180+
@GetMapping("/camel")
181+
public String camel(Model model) {
182+
model.addAttribute("sample", "Camel Route");
183+
return "camel";
184+
}
179185
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.camel;
21+
22+
import org.apache.camel.CamelContext;
23+
import org.apache.camel.ConsumerTemplate;
24+
import org.apache.camel.ProducerTemplate;
25+
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Profile;
31+
32+
@Configuration()
33+
@Profile("!test")
34+
public class CamelConfig {
35+
@Autowired private CamelContext camelContext;
36+
37+
@Bean
38+
ServletRegistrationBean servletRegistrationBean() {
39+
String contextPath = "/temporalapp";
40+
ServletRegistrationBean servlet =
41+
new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath + "/*");
42+
servlet.setName("CamelServlet");
43+
return servlet;
44+
}
45+
46+
@Bean
47+
ProducerTemplate producerTemplate() {
48+
return camelContext.createProducerTemplate();
49+
}
50+
51+
@Bean
52+
ConsumerTemplate consumerTemplate() {
53+
return camelContext.createConsumerTemplate();
54+
}
55+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.camel;
21+
22+
import java.util.List;
23+
import org.apache.camel.ProducerTemplate;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.ResponseBody;
27+
import org.springframework.web.bind.annotation.RestController;
28+
29+
@RestController
30+
public class CamelResource {
31+
@Autowired private ProducerTemplate producerTemplate;
32+
33+
@GetMapping("/orders")
34+
@ResponseBody
35+
public List<OfficeOrder> getProductsByCategory() {
36+
producerTemplate.start();
37+
List<OfficeOrder> orders = producerTemplate.requestBody("direct:getOrders", null, List.class);
38+
producerTemplate.stop();
39+
return orders;
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.camel;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.client.WorkflowOptions;
24+
import org.apache.camel.builder.RouteBuilder;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.stereotype.Component;
27+
28+
@Component
29+
public class CamelRoutes extends RouteBuilder {
30+
31+
@Autowired private WorkflowClient workflowClient;
32+
33+
@Override
34+
public void configure() {
35+
from("direct:getOrders")
36+
.routeId("direct-getOrders")
37+
.tracing()
38+
.process(
39+
exchange -> {
40+
OrderWorkflow workflow =
41+
workflowClient.newWorkflowStub(
42+
OrderWorkflow.class,
43+
WorkflowOptions.newBuilder()
44+
.setWorkflowId("CamelSampleWorkflow")
45+
.setTaskQueue("CamelSampleTaskQueue")
46+
.build());
47+
exchange.getIn().setBody(workflow.start());
48+
})
49+
.end();
50+
}
51+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.camel;
21+
22+
import javax.persistence.*;
23+
24+
@Entity
25+
@Table(name = "officeorder")
26+
public class OfficeOrder {
27+
@Id
28+
@GeneratedValue(strategy = GenerationType.IDENTITY)
29+
private Integer id;
30+
31+
@Column(nullable = false)
32+
private String number;
33+
34+
@Column(nullable = false)
35+
private String desc;
36+
37+
@Column(nullable = false)
38+
private String date;
39+
40+
@Column(nullable = false)
41+
private double price;
42+
43+
public OfficeOrder() {}
44+
45+
public OfficeOrder(Integer id, String number, String desc, String date, double price) {
46+
this.id = id;
47+
this.number = number;
48+
this.desc = desc;
49+
this.date = date;
50+
this.price = price;
51+
}
52+
53+
public String getDesc() {
54+
return desc;
55+
}
56+
57+
public void setDesc(String desc) {
58+
this.desc = desc;
59+
}
60+
61+
public String getNumber() {
62+
return number;
63+
}
64+
65+
public Integer getId() {
66+
return id;
67+
}
68+
69+
public void setId(Integer id) {
70+
this.id = id;
71+
}
72+
73+
public void setNumber(String number) {
74+
this.number = number;
75+
}
76+
77+
public String getDate() {
78+
return date;
79+
}
80+
81+
public void setDate(String date) {
82+
this.date = date;
83+
}
84+
85+
public double getPrice() {
86+
return price;
87+
}
88+
89+
public void setPrice(double price) {
90+
this.price = price;
91+
}
92+
}
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.camel;
21+
22+
import io.temporal.activity.ActivityInterface;
23+
import java.util.List;
24+
25+
@ActivityInterface
26+
public interface OrderActivity {
27+
List<OfficeOrder> getOrders();
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.camel;
21+
22+
import io.temporal.spring.boot.ActivityImpl;
23+
import java.util.List;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.stereotype.Component;
26+
27+
@Component
28+
@ActivityImpl(taskQueues = "CamelSampleTaskQueue")
29+
public class OrderActivityImpl implements OrderActivity {
30+
31+
@Autowired OrderRepository repository;
32+
33+
@Override
34+
public List<OfficeOrder> getOrders() {
35+
return repository.findAll();
36+
}
37+
}

0 commit comments

Comments
 (0)