-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatting Java code snippets (#424)
- Loading branch information
Showing
65 changed files
with
1,298 additions
and
1,520 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,49 +1,61 @@ | ||
import java.net.URI | ||
|
||
plugins { | ||
java | ||
application | ||
java | ||
application | ||
id("com.diffplug.spotless") version "6.25.0" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenCentral() | ||
} | ||
|
||
val restateVersion = "1.0.1" | ||
|
||
dependencies { | ||
// Restate SDK | ||
annotationProcessor("dev.restate:sdk-api-gen:$restateVersion") | ||
implementation("dev.restate:sdk-api:$restateVersion") | ||
implementation("dev.restate:sdk-common:$restateVersion") | ||
implementation("dev.restate:sdk-http-vertx:$restateVersion") | ||
implementation("dev.restate:sdk-lambda:$restateVersion") | ||
implementation("dev.restate:sdk-serde-jackson:$restateVersion") | ||
|
||
// Jackson parameter names | ||
// https://github.com/FasterXML/jackson-modules-java8/tree/2.14/parameter-names | ||
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:2.16.1") | ||
// Jackson java8 types | ||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.1") | ||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.1") | ||
|
||
implementation("dev.restate:sdk-serde-protobuf:$restateVersion") | ||
implementation("org.apache.logging.log4j:log4j-core:2.20.0") | ||
// Restate SDK | ||
annotationProcessor("dev.restate:sdk-api-gen:$restateVersion") | ||
implementation("dev.restate:sdk-api:$restateVersion") | ||
implementation("dev.restate:sdk-common:$restateVersion") | ||
implementation("dev.restate:sdk-http-vertx:$restateVersion") | ||
implementation("dev.restate:sdk-lambda:$restateVersion") | ||
implementation("dev.restate:sdk-serde-jackson:$restateVersion") | ||
|
||
// Jackson parameter names | ||
// https://github.com/FasterXML/jackson-modules-java8/tree/2.14/parameter-names | ||
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names:2.16.1") | ||
// Jackson java8 types | ||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.1") | ||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.1") | ||
|
||
implementation("dev.restate:sdk-serde-protobuf:$restateVersion") | ||
implementation("org.apache.logging.log4j:log4j-core:2.20.0") | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(21)) | ||
} | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(21)) | ||
} | ||
} | ||
|
||
tasks.withType<JavaCompile> { | ||
// Using -parameters allows to use Jackson ParameterName feature | ||
// https://github.com/FasterXML/jackson-modules-java8/tree/2.14/parameter-names | ||
options.compilerArgs.add("-parameters") | ||
// Using -parameters allows to use Jackson ParameterName feature | ||
// https://github.com/FasterXML/jackson-modules-java8/tree/2.14/parameter-names | ||
options.compilerArgs.add("-parameters") | ||
} | ||
|
||
// Set main class | ||
application { | ||
mainClass.set("develop.Greeter") | ||
mainClass.set("develop.Greeter") | ||
} | ||
|
||
|
||
spotless { | ||
java { | ||
googleJavaFormat() | ||
importOrder() | ||
removeUnusedImports() | ||
formatAnnotations() | ||
toggleOffOn("//", "/n") | ||
} | ||
} |
10 changes: 6 additions & 4 deletions
10
code_snippets/java/src/main/java/concepts/buildingblocks/AppMain.java
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package concepts.buildingblocks; | ||
|
||
import concepts.buildingblocks.part0.OrderWorkflow; | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder; | ||
|
||
public class AppMain { | ||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder().bind(new OrderWorkflow()).bind(new DeliveryManager()).buildAndListen(); | ||
} | ||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder() | ||
.bind(new OrderWorkflow()) | ||
.bind(new DeliveryManager()) | ||
.buildAndListen(); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
code_snippets/java/src/main/java/concepts/buildingblocks/OrderWorkflow.java
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,81 @@ | ||
package concepts.buildingblocks; | ||
|
||
import concepts.buildingblocks.types.OrderRequest; | ||
import concepts.buildingblocks.types.StatusEnum; | ||
import concepts.buildingblocks.utils.PaymentClient; | ||
import concepts.buildingblocks.utils.RestaurantClient; | ||
import dev.restate.sdk.JsonSerdes; | ||
import dev.restate.sdk.ObjectContext; | ||
import dev.restate.sdk.annotation.Handler; | ||
import dev.restate.sdk.annotation.VirtualObject; | ||
import dev.restate.sdk.common.Serde; | ||
import dev.restate.sdk.common.StateKey; | ||
import dev.restate.sdk.serde.jackson.JacksonSerdes; | ||
import java.time.Duration; | ||
|
||
// <start_here> | ||
@VirtualObject | ||
public class OrderWorkflow { | ||
public static final StateKey<StatusEnum> STATUS = | ||
StateKey.of("status", JacksonSerdes.of(StatusEnum.class)); | ||
|
||
// <mark_1> | ||
@Handler | ||
public void process(ObjectContext ctx, OrderRequest order) { | ||
// </mark_1> | ||
String id = order.getOrderId(); | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.CREATED); | ||
// </mark_4> | ||
|
||
// 2. Handle payment | ||
// <mark_5> | ||
String token = ctx.random().nextUUID().toString(); | ||
boolean paid = | ||
ctx.run( | ||
JsonSerdes.BOOLEAN, | ||
// break | ||
() -> PaymentClient.charge(id, token, order.getTotalCost())); | ||
// </mark_5> | ||
|
||
if (!paid) { | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.REJECTED); | ||
// </mark_4> | ||
return; | ||
} | ||
|
||
// 3. Schedule preparation | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.SCHEDULED); | ||
// </mark_4> | ||
ctx.sleep(Duration.ofMillis(order.getDeliveryDelay())); | ||
|
||
// 4. Trigger preparation | ||
// <mark_3> | ||
var awakeable = ctx.awakeable(Serde.VOID); | ||
// <mark_5> | ||
ctx.run(() -> RestaurantClient.prepare(id, awakeable.id())); | ||
// </mark_5> | ||
// </mark_3> | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.IN_PREPARATION); | ||
// </mark_4> | ||
|
||
// <mark_3> | ||
awakeable.await(); | ||
// </mark_3> | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.SCHEDULING_DELIVERY); | ||
// </mark_4> | ||
|
||
// 5. Find a driver and start delivery | ||
// <mark_2> | ||
DeliveryManagerClient.fromContext(ctx, id).startDelivery(order).await(); | ||
// </mark_2> | ||
// <mark_4> | ||
ctx.set(STATUS, StatusEnum.DELIVERED); | ||
// </mark_4> | ||
} | ||
} | ||
// <end_here> |
59 changes: 0 additions & 59 deletions
59
code_snippets/java/src/main/java/concepts/buildingblocks/part0/OrderWorkflow.java
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
code_snippets/java/src/main/java/concepts/buildingblocks/part1_functions/OrderWorkflow.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.