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

Update Java/Kotlin examples #261

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
kotlin-hello-world-lambda-cdk.zip
kotlin-patterns-use-cases.zip
kotlin-hello-world-gradle.zip
kotlin-hello-world-gradle-spring-boot.zip
typescript-basics.zip
typescript-food-ordering.zip
typescript-chat-bot.zip
Expand Down
1 change: 1 addition & 0 deletions .tools/prepare_release_zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ create_release_zip kotlin/end-to-end-applications/kmp-android-todo-app kotlin-km
create_release_zip kotlin/integrations/kotlin-gradle-lambda-cdk kotlin-hello-world-lambda-cdk
create_release_zip kotlin/patterns-use-cases kotlin-patterns-use-cases
create_release_zip kotlin/templates/kotlin-gradle kotlin-hello-world-gradle
create_release_zip kotlin/templates/kotlin-gradle-spring-boot kotlin-hello-world-gradle-spring-boot

create_release_zip typescript/basics typescript-basics
create_release_zip typescript/end-to-end-applications/food-ordering typescript-food-ordering
Expand Down
1 change: 1 addition & 0 deletions .tools/run_jvm_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pushd $PROJECT_ROOT/java/templates/java-maven && mvn verify && popd
pushd $PROJECT_ROOT/java/templates/java-maven-quarkus && mvn verify && popd
pushd $PROJECT_ROOT/java/templates/java-maven-spring-boot && mvn verify && popd
pushd $PROJECT_ROOT/kotlin/templates/kotlin-gradle && ./gradlew --console=plain check && popd
pushd $PROJECT_ROOT/kotlin/templates/kotlin-gradle-spring-boot && ./gradlew --console=plain check && popd

pushd $PROJECT_ROOT/java/basics && ./gradlew --console=plain check && popd
pushd $PROJECT_ROOT/kotlin/basics && ./gradlew --console=plain check && popd
Expand Down
3 changes: 2 additions & 1 deletion .tools/update_jvm_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ search_and_replace_version_maven $PROJECT_ROOT/java/templates/java-maven
search_and_replace_version_maven $PROJECT_ROOT/java/templates/java-maven-quarkus
search_and_replace_version_maven $PROJECT_ROOT/java/templates/java-maven-spring-boot
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/templates/kotlin-gradle
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/templates/kotlin-gradle-spring-boot

search_and_replace_version_gradle $PROJECT_ROOT/java/integrations/java-spring
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/integrations/kotlin-gradle-lambda-cdk/lambda
Expand All @@ -32,7 +33,7 @@ search_and_replace_version_gradle $PROJECT_ROOT/java/tutorials/tour-of-restate-j
search_and_replace_version_gradle $PROJECT_ROOT/java/end-to-end-applications/subway-fare-calculator
search_and_replace_version_gradle $PROJECT_ROOT/java/end-to-end-applications/food-ordering/app/restate-app
search_and_replace_version_gradle $PROJECT_ROOT/java/end-to-end-applications/food-ordering/app/restaurant
search_and_replace_version_gradle $PROJECT_ROOT/java/end-to-end-applications/workflow-interpreter
search_and_replace_version_maven $PROJECT_ROOT/java/end-to-end-applications/workflow-interpreter
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/end-to-end-applications/food-ordering/app/restate-app
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/end-to-end-applications/food-ordering/app/restaurant
search_and_replace_version_gradle $PROJECT_ROOT/kotlin/end-to-end-applications/kmp-android-todo-app/server
10 changes: 2 additions & 8 deletions java/basics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ repositories {
mavenCentral()
}

val restateVersion = "1.2.0"
val restateVersion = "2.0.0"

dependencies {
annotationProcessor("dev.restate:sdk-api-gen:$restateVersion")

// Restate SDK
implementation("dev.restate:sdk-api:$restateVersion")
implementation("dev.restate:sdk-http-vertx:$restateVersion")
// To use Jackson to read/write state entries (optional)
implementation("dev.restate:sdk-serde-jackson:$restateVersion")
implementation("dev.restate:sdk-http-java:$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")

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

// Set main class
Expand Down
12 changes: 5 additions & 7 deletions java/basics/src/main/java/building_blocks/MyService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package building_blocks;

import dev.restate.sdk.Context;
import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import virtual_objects.GreeterObjectClient;

import java.time.Duration;
Expand Down Expand Up @@ -45,12 +45,12 @@ public void run(Context ctx) throws Exception {

// 4. DURABLE PROMISES: tracked by Restate, can be moved between processes and survive failures
// Awakeables: block the workflow until notified by another handler
var awakeable = ctx.awakeable(JsonSerdes.STRING);
var awakeable = ctx.awakeable(String.class);
// Wait on the promise
// If the process crashes while waiting, Restate will recover the promise somewhere else
String greeting = awakeable.await();
// Another process can resolve the awakeable via its ID
ctx.awakeableHandle(awakeable.id()).resolve(JsonSerdes.STRING, "hello");
ctx.awakeableHandle(awakeable.id()).resolve(String.class, "hello");

// 5. DURABLE TIMERS: sleep or wait for a timeout, tracked by Restate and recoverable
// When this runs on FaaS, the handler suspends and the timer is tracked by Restate
Expand All @@ -71,8 +71,6 @@ public void run(Context ctx) throws Exception {
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new MyService())
.buildAndListen();
RestateHttpServer.listen(Endpoint.bind(new MyService()));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package durable_execution;

import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.Context;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import utils.SubscriptionRequest;

import static utils.ExampleStubs.*;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void add(Context ctx, SubscriptionRequest req) {

// ctx.run persists results of successful actions and skips execution on retries
// Failed actions (timeouts, API downtime, etc.) get retried
var payRef = ctx.run(JsonSerdes.STRING, () ->
var payRef = ctx.run(String.class, () ->
createRecurringPayment(req.creditCard(), paymentId));

for (String subscription : req.subscriptions()) {
Expand All @@ -50,9 +50,7 @@ public void add(Context ctx, SubscriptionRequest req) {

public static void main(String[] args) {
// Create an HTTP endpoint to serve your services
RestateHttpEndpointBuilder.builder()
.bind(new SubscriptionService())
.buildAndListen();
RestateHttpServer.listen(Endpoint.bind(new SubscriptionService()));
}
}

Expand Down
1 change: 0 additions & 1 deletion java/basics/src/main/java/utils/ExampleStubs.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package utils;

import dev.restate.sdk.common.TerminalException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down
12 changes: 5 additions & 7 deletions java/basics/src/main/java/virtual_objects/GreeterObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
*/
package virtual_objects;

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.StateKey;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import dev.restate.sdk.types.StateKey;

// Virtual Objects are services that hold K/V state. Its handlers interact with the object state.
// An object is identified by a unique id - only one object exists per id.
Expand All @@ -35,7 +35,7 @@ public class GreeterObject {

// Reference to the K/V state stored in Restate
private static final StateKey<Integer> COUNT =
StateKey.of("count", JsonSerdes.INT);
StateKey.of("count", Integer.TYPE);

@Handler
public String greet(ObjectContext ctx, String greeting) {
Expand All @@ -61,9 +61,7 @@ public String ungreet(ObjectContext ctx) {
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new GreeterObject())
.buildAndListen();
RestateHttpServer.listen(Endpoint.bind(new GreeterObject()));
}
}

Expand Down
15 changes: 6 additions & 9 deletions java/basics/src/main/java/workflows/SignupWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
*/
package workflows;

import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.SharedWorkflowContext;
import dev.restate.sdk.WorkflowContext;
import dev.restate.sdk.annotation.Shared;
import dev.restate.sdk.annotation.Workflow;
import dev.restate.sdk.common.DurablePromiseKey;
import dev.restate.sdk.common.StateKey;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import dev.restate.sdk.types.DurablePromiseKey;
import utils.User;

import static utils.ExampleStubs.createUserEntry;
Expand All @@ -38,7 +37,7 @@ public class SignupWorkflow {

// References to K/V state and promises stored in Restate
private static final DurablePromiseKey<String> EMAIL_CLICKED =
DurablePromiseKey.of("email_clicked", JsonSerdes.STRING);
DurablePromiseKey.of("email_clicked", String.class);

// --- The workflow logic ---
@Workflow
Expand All @@ -57,7 +56,7 @@ public boolean run(WorkflowContext ctx, User user) {
// Promise gets resolved or rejected by the other handlers
String clickSecret =
ctx.promise(EMAIL_CLICKED)
.awaitable()
.future()
.await();

return clickSecret.equals(secret);
Expand All @@ -72,9 +71,7 @@ public void click(SharedWorkflowContext ctx, String secret) {
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new SignupWorkflow())
.buildAndListen();
RestateHttpServer.listen(Endpoint.bind(new SignupWorkflow()));
}
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
22 changes: 13 additions & 9 deletions java/end-to-end-applications/food-ordering/app/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -83,7 +85,9 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -144,15 +148,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -201,11 +205,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions java/end-to-end-applications/food-ordering/app/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ repositories {
mavenCentral()
}

val restateVersion = "1.2.0"
val restateVersion = "2.0.0"

dependencies {
// Restate SDK
annotationProcessor("dev.restate:sdk-api-gen:$restateVersion")
implementation("dev.restate:sdk-api:$restateVersion")
implementation("dev.restate:sdk-http-vertx:$restateVersion")
implementation("dev.restate:sdk-serde-jackson:$restateVersion")

implementation("dev.restate:sdk-java-http:$restateVersion")

// Jackson parameter names
// https://github.com/FasterXML/jackson-modules-java8/tree/2.14/parameter-names
Expand All @@ -30,8 +29,8 @@ dependencies {
// Kafka
implementation("org.apache.kafka:kafka-clients:3.6.1")

// Logging (optional)
implementation("org.apache.logging.log4j:log4j-core:2.24.1")
// Logging
implementation("org.apache.logging.log4j:log4j-api:2.24.1")
}

// Set main class
Expand Down
Loading
Loading