Skip to content

Commit

Permalink
Formatting Java code snippets (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen authored Jun 21, 2024
1 parent 1fe5999 commit d84e0d4
Show file tree
Hide file tree
Showing 65 changed files with 1,298 additions and 1,520 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ Code snippets will be compiled and build on PRs and releases.

Details on how code snippets are parsed and inserted can be found in the [code-loader.js](src/plugins/code-loader.js) file.

### Code formatting
We use Spotless for formatting the Java code snippets.
However, Spotless is quite eager on merging lines together into longer lines.
This doesn't work well for the documentation sometimes because it requires horizontal scrolling.
If you don't want Spotless to merge lines on formatting use `// break` in between the lines.
The code loader will filter these out and the formatter will respect the breaks:
```java
RestateHttpEndpointBuilder.builder()
// break
.bind(new TicketObject())
// break
.buildAndListen();
```

## Releasing the documentation

Before releasing the documentation, update schemas and version of Restate artifacts, either:
Expand Down
66 changes: 39 additions & 27 deletions code_snippets/java/build.gradle.kts
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")
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
@VirtualObject
public class DeliveryManager {

@Handler
public void startDelivery(ObjectContext ctx, OrderRequest order) {

}
@Handler
public void startDelivery(ObjectContext ctx, OrderRequest order) {}
}
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>

This file was deleted.

This file was deleted.

Loading

0 comments on commit d84e0d4

Please sign in to comment.