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

Convert the Tour to new API and general improvements #348

Merged
merged 9 commits into from
Apr 18, 2024
Merged
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
11 changes: 11 additions & 0 deletions code_snippets/java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ java {
}
}

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")
}

// Configure test platform
tasks.withType<Test> {
useJUnitPlatform()
}

// Set main class
application {
mainClass.set("develop.Greeter")
Expand Down
32 changes: 32 additions & 0 deletions code_snippets/java/src/main/java/get_started/CheckoutRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package get_started;

import java.util.Set;

public class CheckoutRequest {
private String userId;
private Set<String> tickets;

public CheckoutRequest() {
}

public CheckoutRequest(String userId, Set<String> tickets) {
this.userId = userId;
this.tickets = tickets;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public Set<String> getTickets() {
return tickets;
}

public void setTickets(Set<String> tickets) {
this.tickets = tickets;
}
}
15 changes: 15 additions & 0 deletions code_snippets/java/src/main/java/get_started/TicketObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package get_started;

import dev.restate.sdk.Context;
import dev.restate.sdk.ObjectContext;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.annotation.VirtualObject;

@VirtualObject
public class TicketObject {
@Handler
public void unreserve(ObjectContext ctx) {
return;
}
}
110 changes: 75 additions & 35 deletions code_snippets/java/src/main/java/get_started/Tour.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,75 @@
//package develop;
//
//import com.google.protobuf.Empty;
//import dev.restate.sdk.ObjectContext;
//import static greeter.generated.GreeterProto.*;
//
//import static example.generated.ExampleProto.*;
//import static example.generated.TicketServiceRestate.*;
//
//import dev.restate.sdk.common.TerminalException;
//import example.generated.TicketServiceRestate;
//import greeter.generated.GreeterRestate;
//
//import java.time.Duration;
//
//public class Tour extends TicketServiceRestateImplBase {
//
// @Override
// public void unreserve(ObjectContext ctx, Ticket request) throws TerminalException {
//
// // <start_sleep_and_send>
// ctx.sleep(Duration.ofMinutes(15));
// TicketServiceRestateClient ticketClnt = TicketServiceRestate.newClient(ctx);
// ticketClnt.oneWay().unreserve(Ticket.newBuilder().setTicketId(request.getTicketId()).build());
// // <end_sleep_and_send>
//
// // <start_idempotency_key_retry>
// String idempotencyKey = ctx.random().nextUUID().toString();
// System.out.println("My idempotency key: " + idempotencyKey);
// throw new IllegalStateException("Something went wrong.");
// // <end_idempotency_key_retry>
// }
//
// private String doDbRequest(){ return ""; }
//}
package get_started;

import dev.restate.sdk.Context;
import dev.restate.sdk.ObjectContext;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.annotation.VirtualObject;
import dev.restate.sdk.common.CoreSerdes;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;

import java.time.Duration;

@Service
public class Tour {

@Handler
public void myFn(ObjectContext ctx) {
String ticketId = "123";

// <start_sleep>
ctx.sleep(Duration.ofMinutes(15));
// <end_sleep>

// <start_sleep_and_send>
ctx.sleep(Duration.ofMinutes(15));
TicketObjectClient.fromContext(ctx, ticketId).send().unreserve();
// <end_sleep_and_send>
}

// <start_uuid>
@Handler
public boolean handle(Context ctx, CheckoutRequest request) {
// withClass(1,2,4) highlight-line
String idempotencyKey = ctx.random().nextUUID().toString();
System.out.println("My idempotency key: " + idempotencyKey);

throw new IllegalStateException("The handler failed");
}
// <end_uuid>

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder().bind(new Tour()).bind(new TicketObject()).buildAndListen();
}
}

@Service
class CheckoutService {
// <start_checkout>
@Handler
public boolean handle(Context ctx, CheckoutRequest request) {
// withClass highlight-line
double totalPrice = request.getTickets().size() * 40.0;

String idempotencyKey = ctx.random().nextUUID().toString();

// withClass highlight-line
boolean success = ctx.sideEffect(CoreSerdes.JSON_BOOLEAN, () ->
PaymentClient.get().call(idempotencyKey, totalPrice));

return success;
}
// <end_checkout>


}

class PaymentClient {

public static PaymentClient get() {
return new PaymentClient();
}
public boolean call(String idempotencyKey, double totalPrice) {
return true;
}
}
Loading