-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
1,723 additions
and
96 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
java/patterns-use-cases/async-tasks-parallelize-work/README.md
This file was deleted.
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
35 changes: 0 additions & 35 deletions
35
...-use-cases/async-tasks-payment-signals/src/main/java/my/example/types/PaymentRequest.java
This file was deleted.
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
3 changes: 0 additions & 3 deletions
3
...ent-processing-transactional-handlers/src/main/java/my/example/types/SocialMediaPost.java
This file was deleted.
Oops, something went wrong.
Empty file.
15 changes: 0 additions & 15 deletions
15
java/patterns-use-cases/microservices-sagas/src/main/java/dev/restate/patterns/AppMain.java
This file was deleted.
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
File renamed without changes.
12 changes: 0 additions & 12 deletions
12
...se-cases/microservices-sagas/src/main/java/dev/restate/patterns/types/BookingRequest.java
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions
39
...s-use-cases/patterns-use-cases/src/main/java/my/example/dataupload/DataUploadService.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,39 @@ | ||
package my.example.dataupload; | ||
|
||
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.http.vertx.RestateHttpEndpointBuilder; | ||
import my.example.dataupload.utils.EmailClient; | ||
|
||
import static my.example.dataupload.utils.DataOperations.createS3Bucket; | ||
import static my.example.dataupload.utils.DataOperations.uploadData; | ||
|
||
@Workflow | ||
public class DataUploadService { | ||
|
||
private static final DurablePromiseKey<String> URL_PROMISE = | ||
DurablePromiseKey.of("url", JsonSerdes.STRING); | ||
|
||
@Workflow | ||
public String run(WorkflowContext ctx) { | ||
String url = ctx.run(JsonSerdes.STRING, () -> createS3Bucket()); | ||
ctx.run(() -> uploadData(url)); | ||
|
||
ctx.promiseHandle(URL_PROMISE).resolve(url); | ||
return url; | ||
} | ||
|
||
@Shared | ||
public void resultAsEmail(SharedWorkflowContext ctx, String email) { | ||
String url = ctx.promise(URL_PROMISE).awaitable().await(); | ||
ctx.run(() -> EmailClient.send(url, email)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder().bind(new DataUploadService()).buildAndListen(9082); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...tterns-use-cases/patterns-use-cases/src/main/java/my/example/dataupload/UploadClient.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,56 @@ | ||
package my.example.dataupload; | ||
|
||
import dev.restate.sdk.client.Client; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
// The upload client calls the data upload workflow and awaits the result for 5 seconds. | ||
// If the workflow doesn't complete within that time, it asks the | ||
// workflow to send the upload url via email instead. | ||
public class UploadClient { | ||
private static final Logger logger = LogManager.getLogger(UploadClient.class); | ||
|
||
private static final String RESTATE_URL = "http://localhost:8080"; | ||
|
||
public void uploadData(String userId, String email) { | ||
logger.info("Uploading data for user {}", userId); | ||
|
||
// Submit the workflow | ||
Client restateClient = Client.connect(RESTATE_URL); | ||
var uploadClient = DataUploadServiceClient.fromClient(restateClient, userId); | ||
uploadClient.submit(); | ||
|
||
String url; | ||
try { | ||
// Wait for the workflow to complete or timeout | ||
url = uploadClient.workflowHandle().attachAsync() | ||
.orTimeout(5, TimeUnit.SECONDS) | ||
.join(); | ||
} catch (Exception e) { | ||
if (e.getCause() instanceof TimeoutException) { | ||
logger.info("Slow upload... Mail the link later"); | ||
uploadClient.resultAsEmail(email); | ||
return; | ||
} | ||
throw e; | ||
} | ||
|
||
// ... process directly ... | ||
logger.info("Fast upload... URL was {}", url); | ||
} | ||
|
||
//-------------------------------------------------------------------------------- | ||
// This client would be used in some other part of the system. | ||
// For the sake of this example, we are calling it here from the main method, so you can test the example. | ||
public static void main(String[] args) { | ||
if (args.length < 1) { | ||
System.err.println("Specify the userId as the argument: " + | ||
"./gradlew run -PmainClass=my.example.UploadClient --args=\"userId123\""); | ||
System.exit(1); | ||
} | ||
new UploadClient().uploadData(args[0], args[0] + "@example.com"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...se-cases/patterns-use-cases/src/main/java/my/example/dataupload/utils/DataOperations.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,25 @@ | ||
package my.example.dataupload.utils; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class DataOperations { | ||
private static final Logger logger = LogManager.getLogger(DataOperations.class); | ||
|
||
public static String createS3Bucket(){ | ||
String bucket = String.valueOf((long) (Math.random() * 1_000_000_000)); | ||
String bucketUrl = "https://s3-eu-central-1.amazonaws.com/" + bucket + "/"; | ||
logger.info("Creating bucket with URL {}", bucketUrl); | ||
return bucketUrl; | ||
} | ||
|
||
public static void uploadData(String url){ | ||
long timeRemaining = Math.random() < 0.5 ? 1500 : 10000; | ||
logger.info("Uploading data to target {}. ETA: {} ms", url, timeRemaining); | ||
try { | ||
Thread.sleep(timeRemaining); | ||
} catch (InterruptedException e) { | ||
logger.error("Upload failed: {}", e.getMessage()); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s-use-cases/patterns-use-cases/src/main/java/my/example/dataupload/utils/EmailClient.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,12 @@ | ||
package my.example.dataupload.utils; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class EmailClient { | ||
private static final Logger logger = LogManager.getLogger(EmailClient.class); | ||
|
||
public static void send(String email, String url){ | ||
logger.info("Sending email to {} with url {}", email, url); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
java/patterns-use-cases/patterns-use-cases/src/main/java/my/example/durablerpc/MyClient.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,21 @@ | ||
package my.example.durablerpc; | ||
|
||
import dev.restate.sdk.client.CallRequestOptions; | ||
import dev.restate.sdk.client.Client; | ||
|
||
public class MyClient { | ||
|
||
public static String RESTATE_URL = "http://localhost:8080"; | ||
|
||
public boolean reserveProduct(String productId, String reservationId) { | ||
Client restateClient = Client.connect(RESTATE_URL); | ||
|
||
// Durable RPC call to the product service | ||
// Restate registers the request and makes sure runs to completion exactly once | ||
boolean reserved = ProductServiceClient.fromClient(restateClient, productId) | ||
// Restate deduplicates requests with the same idempotency key | ||
.reserve(CallRequestOptions.DEFAULT.withIdempotency(reservationId)); | ||
|
||
return reserved; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...erns-use-cases/patterns-use-cases/src/main/java/my/example/durablerpc/ProductService.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,39 @@ | ||
package my.example.durablerpc; | ||
|
||
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 org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/* | ||
* Have a look at the ProductServiceClient class to see how to call Restate handlers | ||
* programmatically from another process. | ||
*/ | ||
@VirtualObject | ||
public class ProductService { | ||
private static final Logger logger = LogManager.getLogger(ProductService.class); | ||
|
||
private static final StateKey<Boolean> RESERVED = StateKey.of("reserved", JsonSerdes.BOOLEAN); | ||
|
||
|
||
@Handler | ||
public boolean reserve(ObjectContext ctx) { | ||
if (ctx.get(RESERVED).orElse(false)){ | ||
logger.info("Product already reserved"); | ||
return false; | ||
} | ||
logger.info("Reserving product"); | ||
ctx.set(RESERVED, true); | ||
return true; | ||
} | ||
|
||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder() | ||
.bind(new ProductService()) | ||
.buildAndListen(); | ||
} | ||
} |
Oops, something went wrong.