-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add Kotlin event processing examples #248
Open
gvdongen
wants to merge
4
commits into
main
Choose a base branch
from
extra_examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c93c7b0
Add event processing patterns for Kotlin
gvdongen b57fefc
Add Kotlin event processing examples to the readmes
gvdongen 2a3d67e
Add Kotlin event processing examples to the readmes
gvdongen c819360
Fix kotlin side effect run to runBlock
gvdongen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,39 @@ | ||
version: '3' | ||
services: | ||
broker: | ||
image: confluentinc/cp-kafka:7.5.0 | ||
container_name: broker | ||
ports: | ||
- "9092:9092" | ||
- "9101:9101" | ||
environment: | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT | ||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092 | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 | ||
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 | ||
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 | ||
KAFKA_PROCESS_ROLES: broker,controller | ||
KAFKA_NODE_ID: 1 | ||
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@broker:29093 | ||
KAFKA_LISTENERS: PLAINTEXT://broker:29092,CONTROLLER://broker:29093,PLAINTEXT_HOST://0.0.0.0:9092 | ||
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT | ||
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER | ||
KAFKA_LOG_DIRS: /tmp/kraft-combined-logs | ||
CLUSTER_ID: MkU3OEVBNTcwNTJENDM2Qk | ||
|
||
init-kafka: | ||
image: confluentinc/cp-kafka:7.5.0 | ||
depends_on: | ||
- broker | ||
entrypoint: [ '/bin/sh', '-c' ] | ||
command: | | ||
"# blocks until kafka is reachable | ||
kafka-topics --bootstrap-server broker:29092 --list | ||
echo -e 'Creating kafka topics' | ||
kafka-topics --bootstrap-server broker:29092 --create --if-not-exists --topic social-media-posts --replication-factor 1 --partitions 1 | ||
kafka-topics --bootstrap-server broker:29092 --create --if-not-exists --topic package-location-updates --replication-factor 1 --partitions 1 | ||
|
||
echo -e 'Successfully created the following topics:' | ||
kafka-topics --bootstrap-server broker:29092 --list" |
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,3 @@ | ||
[[ingress.kafka-clusters]] | ||
name = "my-cluster" | ||
brokers = ["PLAINTEXT://localhost:9092"] |
49 changes: 49 additions & 0 deletions
49
kotlin/patterns-use-cases/src/main/kotlin/my/example/eventenrichment/PackageTracker.kt
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,49 @@ | ||
package my.example.eventenrichment | ||
|
||
import dev.restate.sdk.annotation.Handler | ||
import dev.restate.sdk.annotation.Shared | ||
import dev.restate.sdk.annotation.VirtualObject | ||
import dev.restate.sdk.common.StateKey | ||
import dev.restate.sdk.common.TerminalException | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder | ||
import dev.restate.sdk.kotlin.KtSerdes | ||
import dev.restate.sdk.kotlin.ObjectContext | ||
import dev.restate.sdk.kotlin.SharedObjectContext | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class PackageInfo(val finalDestination: String, val locations: MutableList<LocationUpdate> = mutableListOf()) | ||
@Serializable | ||
data class LocationUpdate(val timestamp: String, val location: String) | ||
|
||
@VirtualObject | ||
class PackageTracker { | ||
|
||
companion object { | ||
private val PACKAGE_INFO = StateKey.of("package-info", KtSerdes.json<PackageInfo>()) | ||
} | ||
|
||
@Handler | ||
suspend fun registerPackage(ctx: ObjectContext, packageInfo: PackageInfo) { | ||
ctx.set(PACKAGE_INFO, packageInfo) | ||
} | ||
|
||
@Handler | ||
suspend fun updateLocation(ctx: ObjectContext, locationUpdate: LocationUpdate) { | ||
val packageInfo = ctx.get(PACKAGE_INFO) | ||
?: throw TerminalException("Package not found") | ||
|
||
packageInfo.locations.add(locationUpdate) | ||
ctx.set(PACKAGE_INFO, packageInfo) | ||
} | ||
|
||
@Shared | ||
suspend fun getPackageInfo(ctx: SharedObjectContext): PackageInfo { | ||
return ctx.get(PACKAGE_INFO) | ||
?: throw TerminalException("Package not found") | ||
} | ||
} | ||
|
||
fun main() { | ||
RestateHttpEndpointBuilder.builder().bind(PackageTracker()).buildAndListen() | ||
} |
37 changes: 37 additions & 0 deletions
37
kotlin/patterns-use-cases/src/main/kotlin/my/example/eventtransactions/UserFeed.kt
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,37 @@ | ||
package my.example.eventtransactions | ||
|
||
import dev.restate.sdk.annotation.Handler | ||
import dev.restate.sdk.annotation.VirtualObject | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder | ||
import dev.restate.sdk.kotlin.ObjectContext | ||
import dev.restate.sdk.kotlin.runBlock | ||
import kotlinx.serialization.Serializable | ||
import my.example.eventtransactions.utils.createPost | ||
import my.example.eventtransactions.utils.getPostStatus | ||
import my.example.eventtransactions.utils.updateUserFeed | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
@VirtualObject | ||
class UserFeed { | ||
|
||
@Serializable | ||
data class SocialMediaPost(val content: String, val metadata: String) | ||
|
||
@Handler | ||
suspend fun processPost(ctx: ObjectContext, post: SocialMediaPost) { | ||
val userId = ctx.key() | ||
|
||
val postId = ctx.runBlock { createPost(userId, post) } | ||
|
||
while (ctx.runBlock { getPostStatus(postId) } == "PENDING") { | ||
ctx.sleep(5000.milliseconds) | ||
} | ||
|
||
ctx.runBlock { updateUserFeed(userId, postId) } | ||
} | ||
} | ||
|
||
fun main() { | ||
RestateHttpEndpointBuilder.builder().bind(UserFeed()).buildAndListen() | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
kotlin/patterns-use-cases/src/main/kotlin/my/example/eventtransactions/utils/stubs.kt
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,28 @@ | ||
package my.example.eventtransactions.utils | ||
|
||
import my.example.eventtransactions.UserFeed | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Logger | ||
import java.util.UUID | ||
|
||
private val logger: Logger = LogManager.getLogger("UserFeed") | ||
|
||
fun createPost(userId: String, post: UserFeed.SocialMediaPost): String { | ||
val postId = UUID.randomUUID().toString() | ||
logger.info("Creating post {} for user {}", postId, userId) | ||
return postId | ||
} | ||
|
||
fun getPostStatus(postId: String): String { | ||
return if (Math.random() < 0.8) { | ||
logger.info("Content moderation for post {} is still pending... Will check again in 5 seconds", postId) | ||
"PENDING" | ||
} else { | ||
logger.info("Content moderation for post {} is done", postId) | ||
"DONE" | ||
} | ||
} | ||
|
||
fun updateUserFeed(userId: String, postId: String) { | ||
logger.info("Updating user feed for user {} with post {}", userId, postId) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be just
KtStateKey.json<PackageInfo>("package-info")