Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New quickstart #214

Merged
merged 23 commits into from
Nov 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
new greeter template for Kotlin templates
gvdongen committed Nov 20, 2024
commit dd8bfe4766b5381db58ec9f384926d9953d3a7a7
16 changes: 10 additions & 6 deletions templates/kotlin-gradle/src/main/kotlin/my/example/Greeter.kt
Original file line number Diff line number Diff line change
@@ -4,18 +4,22 @@ import dev.restate.sdk.annotation.Handler
import dev.restate.sdk.annotation.Service
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
import dev.restate.sdk.kotlin.Context
import dev.restate.sdk.kotlin.runBlock
import kotlin.time.Duration.Companion.seconds

/**
* Template of a Restate service and handler
* Have a look at the Kotlin QuickStart to learn how to run this: https://docs.restate.dev/get_started/quickstart?sdk=kotlin
*/
@Service
class Greeter {

@Handler
suspend fun greet(ctx: Context, greeting: String): String {
suspend fun greet(ctx: Context, name: String): String {
// Durably execute a set of steps; resilient against failures
val greetingId = ctx.random().nextUUID().toString()
ctx.runBlock { sendNotification(greetingId, name) }
ctx.sleep(1.seconds)
ctx.runBlock { sendReminder(greetingId) }

return greeting
// Respond to caller
return "You said hi to $name!";
}
}

17 changes: 17 additions & 0 deletions templates/kotlin-gradle/src/main/kotlin/my/example/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package my.example

fun sendNotification(greetingId: String, name: String) {
if (Math.random() < 0.5) { // 30% chance of failure
println("Failed to send notification: $greetingId - $name")
throw Error("Failed to send notification: $greetingId - $name")
}
println("Notification sent: $greetingId - $name")
}

fun sendReminder(greetingId: String) {
if (Math.random() < 0.5) { // 30% chance of failure
println("Failed to send reminder: $greetingId")
throw Error("Failed to send reminder: $greetingId")
}
println("Reminder sent: $greetingId")
}