Skip to content

Commit b87a927

Browse files
committed
removing code dependency between examples
Signed-off-by: salaboy <Salaboy@gmail.com>
1 parent 8e62431 commit b87a927

67 files changed

Lines changed: 2570 additions & 72 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

spring-boot-4-examples/consumer-app/pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
<description>Spring Boot 4, Testcontainers and Dapr Integration Examples :: Consumer App</description>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>io.dapr</groupId>
19-
<artifactId>consumer-app</artifactId>
20-
<version>${project.version}</version>
21-
</dependency>
2217
<dependency>
2318
<groupId>org.springframework.boot</groupId>
2419
<artifactId>spring-boot-starter-web</artifactId>
@@ -33,7 +28,7 @@
3328
</dependency>
3429
<dependency>
3530
<groupId>io.dapr.spring</groupId>
36-
<artifactId>dapr-spring-boot-starter-test</artifactId>
31+
<artifactId>dapr-spring-boot-4-starter-test</artifactId>
3732
<scope>test</scope>
3833
</dependency>
3934
<dependency>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.consumer;
15+
16+
import org.springframework.boot.SpringApplication;
17+
import org.springframework.boot.autoconfigure.SpringBootApplication;
18+
19+
@SpringBootApplication
20+
public class ConsumerApplication {
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(ConsumerApplication.class, args);
24+
}
25+
26+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.consumer;
15+
16+
public class Order {
17+
private String id;
18+
private String item;
19+
private Integer amount;
20+
21+
public Order() {
22+
}
23+
24+
/**
25+
* Creates a new Order.
26+
*
27+
* @param id order id
28+
* @param item item reference
29+
* @param amount of items in the order
30+
*/
31+
public Order(String id, String item, Integer amount) {
32+
this.id = id;
33+
this.item = item;
34+
this.amount = amount;
35+
}
36+
37+
public String getId() {
38+
return id;
39+
}
40+
41+
public void setId(String id) {
42+
this.id = id;
43+
}
44+
45+
public String getItem() {
46+
return item;
47+
}
48+
49+
public void setItem(String item) {
50+
this.item = item;
51+
}
52+
53+
public Integer getAmount() {
54+
return amount;
55+
}
56+
57+
public void setAmount(Integer amount) {
58+
this.amount = amount;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "Order{" + "id='" + id + '\'' + ", item='" + item + '\'' + ", amount=" + amount + '}';
64+
}
65+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.consumer;
15+
16+
import io.dapr.Topic;
17+
import io.dapr.client.domain.CloudEvent;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PostMapping;
22+
import org.springframework.web.bind.annotation.RequestBody;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
@RestController
29+
public class SubscriberRestController {
30+
31+
private final Logger logger = LoggerFactory.getLogger(SubscriberRestController.class);
32+
33+
private List<CloudEvent> events = new ArrayList<>();
34+
35+
/**
36+
* Subscribe to cloud events.
37+
* @param cloudEvent payload
38+
*/
39+
@PostMapping("subscribe")
40+
@Topic(pubsubName = "pubsub", name = "topic")
41+
public void subscribe(@RequestBody CloudEvent<Order> cloudEvent) {
42+
logger.info("Order Event Received: " + cloudEvent.getData());
43+
events.add(cloudEvent);
44+
}
45+
46+
@GetMapping("events")
47+
public List<CloudEvent> getAllEvents() {
48+
return events;
49+
}
50+
51+
}
52+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dapr.pubsub.name=pubsub
2+
spring.application.name=consumer-app
3+
server.port=8081
4+

spring-boot-4-examples/producer-app/pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
<description>Spring Boot 4, Testcontainers and Dapr Integration Examples :: Producer App</description>
1616

1717
<dependencies>
18-
<dependency>
19-
<groupId>io.dapr</groupId>
20-
<artifactId>producer-app</artifactId>
21-
<version>${project.version}</version>
22-
</dependency>
2318
<dependency>
2419
<groupId>org.springframework.boot</groupId>
2520
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -39,7 +34,7 @@
3934
</dependency>
4035
<dependency>
4136
<groupId>io.dapr.spring</groupId>
42-
<artifactId>dapr-spring-boot-starter-test</artifactId>
37+
<artifactId>dapr-spring-boot-4-starter-test</artifactId>
4338
<scope>test</scope>
4439
</dependency>
4540
<dependency>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.producer;
15+
16+
public class Customer {
17+
private String customerName;
18+
private String workflowId;
19+
private boolean inCustomerDB = false;
20+
private boolean followUp = false;
21+
22+
public boolean isFollowUp() {
23+
return followUp;
24+
}
25+
26+
public void setFollowUp(boolean followUp) {
27+
this.followUp = followUp;
28+
}
29+
30+
public boolean isInCustomerDB() {
31+
return inCustomerDB;
32+
}
33+
34+
public void setInCustomerDB(boolean inCustomerDB) {
35+
this.inCustomerDB = inCustomerDB;
36+
}
37+
38+
public String getWorkflowId() {
39+
return workflowId;
40+
}
41+
42+
public void setWorkflowId(String workflowId) {
43+
this.workflowId = workflowId;
44+
}
45+
46+
public String getCustomerName() {
47+
return customerName;
48+
}
49+
50+
public void setCustomerName(String customerName) {
51+
this.customerName = customerName;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Customer [customerName=" + customerName + ", workflowId=" + workflowId + ", inCustomerDB="
57+
+ inCustomerDB + ", followUp=" + followUp + "]";
58+
}
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.producer;
15+
16+
import org.springframework.stereotype.Component;
17+
18+
import java.util.Collection;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
@Component
23+
public class CustomerStore {
24+
private Map<String, Customer> customers = new HashMap<>();
25+
26+
public void addCustomer(Customer customer) {
27+
customers.put(customer.getCustomerName(), customer);
28+
}
29+
30+
public Customer getCustomer(String customerName) {
31+
return customers.get(customerName);
32+
}
33+
34+
public Collection<Customer> getCustomers() {
35+
return customers.values();
36+
}
37+
38+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.springboot4.examples.producer;
15+
16+
import io.dapr.spring.workflows.config.EnableDaprWorkflows;
17+
import io.dapr.springboot.examples.producer.workflow.CustomerWorkflow;
18+
import io.dapr.workflows.client.DaprWorkflowClient;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.PostMapping;
24+
import org.springframework.web.bind.annotation.RequestBody;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
import java.util.Collection;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
@RestController
32+
@EnableDaprWorkflows
33+
public class CustomersRestController {
34+
35+
36+
private final Logger logger = LoggerFactory.getLogger(CustomersRestController.class);
37+
38+
@Autowired
39+
private DaprWorkflowClient daprWorkflowClient;
40+
41+
@Autowired
42+
private CustomerStore customerStore;
43+
44+
@GetMapping("/")
45+
public String root() {
46+
return "OK";
47+
}
48+
49+
private Map<String, String> customersWorkflows = new HashMap<>();
50+
51+
/**
52+
* Track customer endpoint.
53+
*
54+
* @param customer provided customer to track
55+
* @return confirmation that the workflow instance was created for a given customer
56+
*/
57+
@PostMapping("/customers")
58+
public String trackCustomer(@RequestBody Customer customer) {
59+
String instanceId = daprWorkflowClient.scheduleNewWorkflow(CustomerWorkflow.class, customer);
60+
logger.info("Workflow instance " + instanceId + " started");
61+
customersWorkflows.put(customer.getCustomerName(), instanceId);
62+
return "New Workflow Instance created for Customer: " + customer.getCustomerName();
63+
}
64+
65+
/**
66+
* Request customer follow-up.
67+
* @param customer associated with a workflow instance
68+
* @return confirmation that the follow-up was requested
69+
*/
70+
@PostMapping("/customers/followup")
71+
public String customerNotification(@RequestBody Customer customer) {
72+
logger.info("Customer follow-up requested: " + customer.getCustomerName());
73+
String workflowIdForCustomer = customersWorkflows.get(customer.getCustomerName());
74+
if (workflowIdForCustomer == null || workflowIdForCustomer.isEmpty()) {
75+
return "There is no workflow associated with customer: " + customer.getCustomerName();
76+
} else {
77+
daprWorkflowClient.raiseEvent(workflowIdForCustomer, "CustomerReachOut", customer);
78+
return "Customer Follow-up requested";
79+
}
80+
}
81+
82+
83+
public Collection<Customer> getCustomers() {
84+
return customerStore.getCustomers();
85+
}
86+
87+
88+
}
89+

0 commit comments

Comments
 (0)