Skip to content

Commit

Permalink
adding spring boot producer
Browse files Browse the repository at this point in the history
  • Loading branch information
salaboy committed Feb 4, 2025
1 parent 58d6218 commit 412eb36
Show file tree
Hide file tree
Showing 22 changed files with 792 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@
<module>examples</module>
<!-- We are following test containers artifact convention on purpose, don't rename -->
<module>testcontainers-dapr</module>
<module>spring-boot-examples</module>
</modules>

<profiles>
Expand Down
Empty file added spring-boot-examples/README.md
Empty file.
20 changes: 20 additions & 0 deletions spring-boot-examples/pom.xml
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>
86 changes: 86 additions & 0 deletions spring-boot-examples/producer-app/pom.xml
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>
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 + "]";
}


}
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();
}

}
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();
}


}

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;
}
}
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);
}
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);
}


}

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);
}

}
Loading

0 comments on commit 412eb36

Please sign in to comment.