Skip to content

Commit

Permalink
Add basics-kotlin (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Sep 4, 2024
1 parent 25d23f3 commit 37ece2c
Show file tree
Hide file tree
Showing 20 changed files with 1,084 additions and 6 deletions.
1 change: 1 addition & 0 deletions .tools/run_jvm_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pushd $PROJECT_ROOT/templates/kotlin-gradle && ./gradlew check && popd
pushd $PROJECT_ROOT/templates/kotlin-gradle-lambda-cdk/lambda && ./gradlew check && popd

pushd $PROJECT_ROOT/basics/basics-java && ./gradlew check && popd
pushd $PROJECT_ROOT/basics/basics-kotlin && ./gradlew check && popd

pushd $PROJECT_ROOT/patterns-use-cases/sagas/sagas-java && ./gradlew check && popd
pushd $PROJECT_ROOT/patterns-use-cases/payment-state-machine/payment-state-machine-java && ./gradlew check && popd
Expand Down
1 change: 1 addition & 0 deletions .tools/update_jvm_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ search_and_replace_version_gradle $PROJECT_ROOT/templates/kotlin-gradle
search_and_replace_version_gradle $PROJECT_ROOT/templates/kotlin-gradle-lambda-cdk/lambda

search_and_replace_version_gradle $PROJECT_ROOT/basics/basics-java
search_and_replace_version_gradle $PROJECT_ROOT/basics/basics-kotlin

search_and_replace_version_gradle $PROJECT_ROOT/patterns-use-cases/sagas/sagas-java
search_and_replace_version_gradle $PROJECT_ROOT/patterns-use-cases/async-signals-payment/async-signals-payment-java
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ challenges.

### Kotlin

| Type | Name / Link |
|------------|-------------------------------------------------------------------|
| Templates | [Template using Gradle](templates/kotlin-gradle) |
| Use Cases | [Sagas](patterns-use-cases/sagas/sagas-kotlin) |
| End-to-End | [Food Ordering App](end-to-end-applications/kotlin/food-ordering) |
| Type | Name / Link |
|------------|-------------------------------------------------------------------------|
| Templates | [Template using Gradle](templates/kotlin-gradle) |
| Basics | [Durable Execution, Event-processing, Virtual Objects](basics/basics-kotlin) |
| Use Cases | [Sagas](patterns-use-cases/sagas/sagas-kotlin) |
| End-to-End | [Food Ordering App](end-to-end-applications/kotlin/food-ordering) |


### Python
Expand Down
2 changes: 1 addition & 1 deletion basics/basics-java/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Examples of the basic concepts for Restate in TypeScript / JavaScript
# Examples of the basic concepts for Restate in Java

The examples here showcase the most basic building blocks of Restate. **Durable Execution**,
**Durable Promises**, and **Virtual Objects**, and the **Workflows** abstraction built on top
Expand Down
35 changes: 35 additions & 0 deletions basics/basics-kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build

.idea
*.iml

# Unignore the gradle wrapper
!gradle/wrapper/gradle-wrapper.jar
66 changes: 66 additions & 0 deletions basics/basics-kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Examples of the basic concepts for Restate in Kotlin

The examples here showcase the most basic building blocks of Restate. **Durable Execution**,
**Durable Promises**, and **Virtual Objects**, and the **Workflows** abstraction built on top
of them.

The individual example files contain code snippets with comments and a brief descriptions
about how they work and how they can be run.

### Examples

* **[Basic Durable Execution:](durable_execution/RoleUpdateService.java):** Running code cleanly
to the end in the presence of failures. Automatic retries and recovery of previously
finished actions. The example applies a series of updates and permission setting changes
to user's profile.
```shell
./gradlew -PmainClass=durable_execution.RoleUpdateServiceKt run
```

* **[Durable Execution with Compensations](durable_execution_compensation/RoleUpdateService.java):**
Reliably compensating / undoing previous actions upon unrecoverable errors halfway
through multi-step change. This is the same example as above, extended for cases where
a part of the change cannot be applied (conflict) and everything has to roll back.
```shell
./gradlew -PmainClass=durable_execution_compensation.RoleUpdateServiceKt run
```

* **[Workflows](workflows/SignupWorkflow.java):** Workflows are durable execution tasks that can
be submitted and awaited. They have an identity and can be signaled and queried
through durable promises. The example is a user-signup flow that takes multiple
operations, including verifying the email address.

* **[Virtual Objects](virtual_objects/GreeterObject.java):** Stateful serverless objects
to manage durable consistent state and state-manipulating logic.
```shell
./gradlew -PmainClass=virtual_objects.GreeterObjectKt run
```

* **[Kafka Event-processing](events_processing/UserUpdatesService.java):** Processing events to
update various downstream systems with durable event handlers, event-delaying,
in a strict-per-key order.
```shell
./gradlew -PmainClass=events_processing.UserUpdatesServiceKt run
```

* **[Stateful Event-processing](events_state/ProfileService.java):** Populating state from
events and making is queryable via RPC handlers.
```shell
./gradlew -PmainClass=events_state.ProfileServiceKt run
```


### Running the examples

1. Start Restate Server in a separate shell: `npx restate-server`

2. Start the relevant example. The commands are listed above for each example.

3. Register the example at Restate server by calling
`npx restate -y deployment register --force localhost:9080`.

_Note: the '--force' flag here is to circumvent all checks related to graceful upgrades, because it is only a playground, not a production setup._

4. Check the comments in the example for how to interact with the example.

**NOTE:** When you get an error of the type `{"code":"not_found","message":"Service 'greeter' not found. ...}`, then you forgot step (3) for that example.
45 changes: 45 additions & 0 deletions basics/basics-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
application
kotlin("jvm") version "2.0.0"
// Kotlinx serialization (optional)
kotlin("plugin.serialization") version "2.0.0"

id("com.google.devtools.ksp") version "2.0.0-1.0.21"
}

repositories {
mavenCentral()
}

val restateVersion = "1.0.1"

dependencies {
// Annotation processor
ksp("dev.restate:sdk-api-kotlin-gen:$restateVersion")

// Restate SDK
implementation("dev.restate:sdk-api-kotlin:$restateVersion")
implementation("dev.restate:sdk-http-vertx:$restateVersion")

// Logging (optional)
implementation("org.apache.logging.log4j:log4j-core:2.23.0")

// Kotlinx serialization (optional)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
}

// Setup Java/Kotlin compiler target
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

// Set main class
application {
if (project.hasProperty("mainClass")) {
mainClass.set(project.property("mainClass") as String)
} else {
mainClass.set("durable_execution.RoleUpdateServiceKt")
}
}
Binary file not shown.
7 changes: 7 additions & 0 deletions basics/basics-kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 37ece2c

Please sign in to comment.