Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions examples-boot-native/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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>
<artifactId>dapr-sdk-parent</artifactId>
<groupId>io.dapr</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>dapr-sdk-examples-boot-native</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<springboot.version>2.3.5.RELEASE</springboot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

<modules>
<module>subscriber-microservcie</module>
<module>publisher-microservice</module>
</modules>
</project>
93 changes: 93 additions & 0 deletions examples-boot-native/publisher-microservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
### Checking out the code

Clone this repository:

```shell
git clone https://github.com/dapr/java-sdk.git
cd java-sdk
```

Then build the Maven project:

```shell
# make sure you are in the `java-sdk` directory.
mvn install
```

Then get into the `examples-boot-native` directory:

```shell
cd examples-boot-native
```

The output jar is `publisher-microservice/target/publisher-microservice.jar`

### Running the publisher

**In this demo, you should run subscriber-microservice first.**

running this microservice in dapr, e.g. using port 9090,

```shell
dapr run -a publisher -p 9090 -- java -jar publisher-microservice/target/publisher-microservice.jar --server.port=9090 &
```

You can also create a file `application.properties` in `publisher-microservice/src/main/resources`, with content:

```properties
server.port=9090
```

then rebuild and exec dapr CLI with

```shell
dapr run -a publisher -p 9090 -- java -jar publisher-microservice/target/publisher-microservice.jar &
```

as `server.port` is the key used to set the port that spring boot applications listen on.

since subscriber ran first, you should get this in subscriber's terminal after publisher is running:

```
== APP == Published message: This is message #0

== APP == Subscriber got: {"id":"9abe67b7-6565-47bb-b2cc-234b8db40c0a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #0"}

== APP == Published message: This is message #1

== APP == Subscriber got: {"id":"a14b1426-86c9-4f4a-aa38-3ac49a40f2d8","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #1"}

== APP == Published message: This is message #2

== APP == Subscriber got: {"id":"be83bab7-7d69-4ff3-8537-087ba56b158a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #2"}

== APP == Published message: This is message #3

== APP == Subscriber got: {"id":"9e677a56-a3dd-4009-85f9-346ef39a4797","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #3"}

== APP == Published message: This is message #4

== APP == Subscriber got: {"id":"6c81e262-cc16-498f-928e-bed5ab2356a4","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #4"}

== APP == Published message: This is message #5

== APP == Subscriber got: {"id":"11eded9a-dd68-4a12-8560-6433124ffc68","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #5"}

== APP == Subscriber got: {"id":"c699e25b-1b02-48d0-8321-30a4cf11a972","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #6"}

== APP == Published message: This is message #6

== APP == Published message: This is message #7

== APP == Subscriber got: {"id":"01620c88-333d-4e25-ac4e-bb0a29b77e57","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #7"}

== APP == Subscriber got: {"id":"87bcfa69-2dbe-4e7f-b499-740ef381e8b1","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #8"}

== APP == Published message: This is message #8

== APP == Published message: This is message #9

== APP == Subscriber got: {"id":"9a1d3ba3-04a1-42bb-8d3b-8f753fc0a348","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #9"}
```

this demo uses dapr's default subpub.
48 changes: 48 additions & 0 deletions examples-boot-native/publisher-microservice/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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-examples-boot-native</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>publisher-microservice</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-spring-boot-starter</artifactId>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.examples.boot.pubsub.http;

import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ClientConfig {
@Bean
public DaprClient buildDaprClient() {
return new DaprClientBuilder().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.examples.boot.pubsub.http;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DaprExamplePublisherApplication {
public static void main(String[] args) {
SpringApplication.run(DaprExamplePublisherApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.examples.boot.pubsub.http;

import io.dapr.client.DaprClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static java.util.Collections.singletonMap;

@Component
public class PublisherRunner implements CommandLineRunner {
//Number of messages to be sent: 10
private static final int NUM_MESSAGES = 10;

//Time-to-live for messages published.
private static final String MESSAGE_TTL_IN_SECONDS = "1000";

//The name of topic
public static final String TOPIC_NAME = "testingtopic";

//The name of the pubsub
public static final String PUBSUB_NAME = "pubsub";

@Autowired
private DaprClient client;

@Override
public void run(String... args) throws Exception {
TimeUnit.SECONDS.sleep(5);

//Creating the DaprClient: Using the default builder client produces an HTTP Dapr Client
for (int i = 0; i < NUM_MESSAGES; i++) {
String message = String.format("This is message #%d", i);
//Publishing messages
client.publishEvent(PUBSUB_NAME, TOPIC_NAME, message,
singletonMap("ttlInSeconds", MESSAGE_TTL_IN_SECONDS)).block();
System.out.println("Published message: " + message);

try {
Thread.sleep((long) (1000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
return;
}
}
}
}
Loading