-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
792 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.dapr</groupId> | ||
<artifactId>dapr-sdk-parent</artifactId> | ||
<version>1.14.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>spring-boot-examples</artifactId> | ||
<version>0.14.0-SNAPSHOT</version> | ||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>producer-app</module> | ||
</modules> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.dapr</groupId> | ||
<artifactId>spring-boot-examples</artifactId> | ||
<version>0.14.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>producer-app</artifactId> | ||
<name>producer-app</name> | ||
<description>Spring Boot, Testcontainers and Dapr Integration Examples :: Producer App</description> | ||
|
||
<properties> | ||
<springboot.version>3.2.6</springboot.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${springboot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.dapr.spring</groupId> | ||
<artifactId>dapr-spring-boot-starter</artifactId> | ||
<version>${dapr.sdk.alpha.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.dapr.spring</groupId> | ||
<artifactId>dapr-spring-boot-starter-test</artifactId> | ||
<version>${dapr.sdk.alpha.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>1.20.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>rabbitmq</artifactId> | ||
<version>1.20.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
48 changes: 48 additions & 0 deletions
48
...ot-examples/producer-app/src/main/java/io/dapr/springboot/examples/producer/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
public class Customer { | ||
private String customerName; | ||
private String workflowId; | ||
private boolean inCustomerDB = false; | ||
private boolean followUp = false; | ||
|
||
public boolean isFollowUp() { | ||
return followUp; | ||
} | ||
|
||
public void setFollowUp(boolean followUp) { | ||
this.followUp = followUp; | ||
} | ||
|
||
public boolean isInCustomerDB() { | ||
return inCustomerDB; | ||
} | ||
|
||
public void setInCustomerDB(boolean inCustomerDB) { | ||
this.inCustomerDB = inCustomerDB; | ||
} | ||
|
||
public String getWorkflowId() { | ||
return workflowId; | ||
} | ||
|
||
public void setWorkflowId(String workflowId) { | ||
this.workflowId = workflowId; | ||
} | ||
|
||
public String getCustomerName() { | ||
return customerName; | ||
} | ||
|
||
public void setCustomerName(String customerName) { | ||
this.customerName = customerName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Customer [customerName=" + customerName + ", workflowId=" + workflowId + ", inCustomerDB=" | ||
+ inCustomerDB + ", followUp=" + followUp + "]"; | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...amples/producer-app/src/main/java/io/dapr/springboot/examples/producer/CustomerStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class CustomerStore { | ||
private Map<String, Customer> customers = new HashMap<>(); | ||
|
||
public void addCustomer(Customer customer) { | ||
customers.put(customer.getCustomerName(), customer); | ||
} | ||
|
||
public Customer getCustomer(String customerName) { | ||
return customers.get(customerName); | ||
} | ||
|
||
public Collection<Customer> getCustomers() { | ||
return customers.values(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...ducer-app/src/main/java/io/dapr/springboot/examples/producer/CustomersRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import io.dapr.spring.workflows.config.EnableDaprWorkflows; | ||
import io.dapr.springboot.examples.producer.workflow.CustomerWorkflow; | ||
import io.dapr.workflows.client.DaprWorkflowClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
@RestController | ||
@EnableDaprWorkflows | ||
public class CustomersRestController { | ||
|
||
@Autowired | ||
private DaprWorkflowClient daprWorkflowClient; | ||
|
||
@Autowired | ||
private CustomerStore customerStore; | ||
|
||
/** | ||
* Track customer endpoint. | ||
* @param customer provided customer to track | ||
*/ | ||
@PostMapping("/customers/") | ||
public void trackCustomer(@RequestBody Customer customer) { | ||
String instanceId = daprWorkflowClient.scheduleNewWorkflow(CustomerWorkflow.class, customer); | ||
try { | ||
daprWorkflowClient.waitForInstanceStart(instanceId, Duration.ofSeconds(10), false); | ||
System.out.printf("workflow instance %s started%n", instanceId); | ||
} catch (TimeoutException e) { | ||
System.out.printf("workflow instance %s did not start within 10 seconds%n", instanceId); | ||
|
||
} | ||
} | ||
|
||
@PostMapping("/customers/followup") | ||
public void customerNotification(@RequestBody Customer customer) { | ||
daprWorkflowClient.raiseEvent(customer.getWorkflowId(), "CustomerReachOut", customer); | ||
} | ||
|
||
|
||
public Collection<Customer> getCustomers() { | ||
return customerStore.getCustomers(); | ||
} | ||
|
||
|
||
} | ||
|
38 changes: 38 additions & 0 deletions
38
...-boot-examples/producer-app/src/main/java/io/dapr/springboot/examples/producer/Order.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import org.springframework.data.annotation.Id; | ||
|
||
public class Order { | ||
|
||
@Id | ||
private String id; | ||
private String item; | ||
private Integer amount; | ||
|
||
public Order() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getItem() { | ||
return item; | ||
} | ||
|
||
public void setItem(String item) { | ||
this.item = item; | ||
} | ||
|
||
public Integer getAmount() { | ||
return amount; | ||
} | ||
|
||
public void setAmount(Integer amount) { | ||
this.amount = amount; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ples/producer-app/src/main/java/io/dapr/springboot/examples/producer/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface OrderRepository extends CrudRepository<Order, String> { | ||
|
||
List<Order> findByItem(String item); | ||
|
||
List<Order> findByAmount(Integer amount); | ||
} |
44 changes: 44 additions & 0 deletions
44
...producer-app/src/main/java/io/dapr/springboot/examples/producer/OrdersRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import io.dapr.spring.data.repository.config.EnableDaprRepositories; | ||
import io.dapr.spring.messaging.DaprMessagingTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@EnableDaprRepositories | ||
public class OrdersRestController { | ||
@Autowired | ||
private OrderRepository repository; | ||
|
||
@Autowired | ||
private DaprMessagingTemplate<Order> messagingTemplate; | ||
|
||
@PostMapping("/orders") | ||
public void storeOrder(@RequestBody Order order) { | ||
repository.save(order); | ||
messagingTemplate.send("topic", order); | ||
} | ||
|
||
@GetMapping("/orders") | ||
public Iterable<Order> getAll() { | ||
return repository.findAll(); | ||
} | ||
|
||
@GetMapping("/orders/byItem/") | ||
public Iterable<Order> getAllByItem(@RequestParam("item") String item) { | ||
return repository.findByItem(item); | ||
} | ||
|
||
@GetMapping("/orders/byAmount/") | ||
public Iterable<Order> getAllByItem(@RequestParam("amount") Integer amount) { | ||
return repository.findByAmount(amount); | ||
} | ||
|
||
|
||
} | ||
|
51 changes: 51 additions & 0 deletions
51
...ucer-app/src/main/java/io/dapr/springboot/examples/producer/ProducerAppConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.dapr.springboot.examples.producer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.dapr.client.DaprClient; | ||
import io.dapr.spring.boot.autoconfigure.pubsub.DaprPubSubProperties; | ||
import io.dapr.spring.boot.autoconfigure.statestore.DaprStateStoreProperties; | ||
import io.dapr.spring.data.DaprKeyValueAdapterResolver; | ||
import io.dapr.spring.data.DaprKeyValueTemplate; | ||
import io.dapr.spring.data.KeyValueAdapterResolver; | ||
import io.dapr.spring.messaging.DaprMessagingTemplate; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties({DaprPubSubProperties.class, DaprStateStoreProperties.class}) | ||
public class ProducerAppConfiguration { | ||
@Bean | ||
public ObjectMapper mapper() { | ||
return new ObjectMapper(); | ||
} | ||
|
||
|
||
/** | ||
* Produce a KeyValueAdapterResolver for Dapr. | ||
* @param daprClient dapr client | ||
* @param mapper object mapper | ||
* @param daprStatestoreProperties properties to configure state store | ||
* @return KeyValueAdapterResolver | ||
*/ | ||
@Bean | ||
public KeyValueAdapterResolver keyValueAdapterResolver(DaprClient daprClient, ObjectMapper mapper, | ||
DaprStateStoreProperties daprStatestoreProperties) { | ||
String storeName = daprStatestoreProperties.getName(); | ||
String bindingName = daprStatestoreProperties.getBinding(); | ||
|
||
return new DaprKeyValueAdapterResolver(daprClient, mapper, storeName, bindingName); | ||
} | ||
|
||
@Bean | ||
public DaprKeyValueTemplate daprKeyValueTemplate(KeyValueAdapterResolver keyValueAdapterResolver) { | ||
return new DaprKeyValueTemplate(keyValueAdapterResolver); | ||
} | ||
|
||
@Bean | ||
public DaprMessagingTemplate<Order> messagingTemplate(DaprClient daprClient, | ||
DaprPubSubProperties daprPubSubProperties) { | ||
return new DaprMessagingTemplate<>(daprClient, daprPubSubProperties.getName(), false); | ||
} | ||
|
||
} |
Oops, something went wrong.