-
Notifications
You must be signed in to change notification settings - Fork 227
feat: Add compensation workflow pattern to Spring Boot examples #1696
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
Open
siri-varma
wants to merge
2
commits into
dapr:master
Choose a base branch
from
siri-varma:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
.../patterns/src/main/java/io/dapr/springboot/examples/wfp/compensation/BookCarActivity.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class BookCarActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(BookCarActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| TimeUnit.SECONDS.sleep(2); | ||
| } catch (InterruptedException e) { | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| logger.info("Forcing Failure to trigger compensation for activity: " + ctx.getName()); | ||
| throw new RuntimeException("Failed to book car"); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...tterns/src/main/java/io/dapr/springboot/examples/wfp/compensation/BookFlightActivity.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Component | ||
| public class BookFlightActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(BookFlightActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| TimeUnit.SECONDS.sleep(2); | ||
| } catch (InterruptedException e) { | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| String result = "Flight booked successfully"; | ||
| logger.info("Activity completed with result: " + result); | ||
| return result; | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
...atterns/src/main/java/io/dapr/springboot/examples/wfp/compensation/BookHotelActivity.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class BookHotelActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(BookHotelActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.warn("Activity '{}' was interrupted.", ctx.getName(), e); | ||
| throw new RuntimeException("Activity was interrupted", e); | ||
| } | ||
|
|
||
| String result = "Hotel booked successfully"; | ||
| logger.info("Activity completed with result: " + result); | ||
| return result; | ||
| } | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
...patterns/src/main/java/io/dapr/springboot/examples/wfp/compensation/BookTripWorkflow.java
This file contains hidden or 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,97 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.durabletask.TaskFailedException; | ||
| import io.dapr.workflows.Workflow; | ||
| import io.dapr.workflows.WorkflowStub; | ||
| import io.dapr.workflows.WorkflowTaskOptions; | ||
| import io.dapr.workflows.WorkflowTaskRetryPolicy; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| @Component | ||
| public class BookTripWorkflow implements Workflow { | ||
| @Override | ||
| public WorkflowStub create() { | ||
| return ctx -> { | ||
| ctx.getLogger().info("Starting Workflow: " + ctx.getName()); | ||
| List<String> compensations = new ArrayList<>(); | ||
|
|
||
| WorkflowTaskRetryPolicy compensationRetryPolicy = WorkflowTaskRetryPolicy.newBuilder() | ||
| .setFirstRetryInterval(Duration.ofSeconds(1)) | ||
| .setMaxNumberOfAttempts(3) | ||
| .build(); | ||
|
|
||
| WorkflowTaskOptions compensationOptions = new WorkflowTaskOptions(compensationRetryPolicy); | ||
|
|
||
| try { | ||
| String flightResult = ctx.callActivity( | ||
| BookFlightActivity.class.getName(), null, String.class).await(); | ||
| ctx.getLogger().info("Flight booking completed: {}", flightResult); | ||
| compensations.add("CancelFlight"); | ||
|
|
||
| String hotelResult = ctx.callActivity( | ||
| BookHotelActivity.class.getName(), null, String.class).await(); | ||
| ctx.getLogger().info("Hotel booking completed: {}", hotelResult); | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| compensations.add("CancelHotel"); | ||
|
|
||
| String carResult = ctx.callActivity( | ||
| BookCarActivity.class.getName(), null, String.class).await(); | ||
| ctx.getLogger().info("Car booking completed: {}", carResult); | ||
| compensations.add("CancelCar"); | ||
|
|
||
| String result = String.format("%s, %s, %s", flightResult, hotelResult, carResult); | ||
| ctx.getLogger().info("Trip booked successfully: {}", result); | ||
| ctx.complete(result); | ||
|
|
||
| } catch (TaskFailedException e) { | ||
| ctx.getLogger().info("******** executing compensation logic ********"); | ||
| ctx.getLogger().error("Activity failed", e); | ||
|
|
||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Collections.reverse(compensations); | ||
| for (String compensation : compensations) { | ||
| try { | ||
| switch (compensation) { | ||
| case "CancelCar": | ||
| String carCancelResult = ctx.callActivity( | ||
| CancelCarActivity.class.getName(), null, compensationOptions, String.class).await(); | ||
| ctx.getLogger().info("Car cancellation completed: {}", carCancelResult); | ||
| break; | ||
| case "CancelHotel": | ||
| String hotelCancelResult = ctx.callActivity( | ||
| CancelHotelActivity.class.getName(), null, compensationOptions, String.class).await(); | ||
| ctx.getLogger().info("Hotel cancellation completed: {}", hotelCancelResult); | ||
| break; | ||
| case "CancelFlight": | ||
| String flightCancelResult = ctx.callActivity( | ||
| CancelFlightActivity.class.getName(), null, compensationOptions, String.class).await(); | ||
| ctx.getLogger().info("Flight cancellation completed: {}", flightCancelResult); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } catch (TaskFailedException ex) { | ||
| ctx.getLogger().error("Activity failed during compensation", ex); | ||
| } | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| ctx.complete("Workflow failed, compensation applied"); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...atterns/src/main/java/io/dapr/springboot/examples/wfp/compensation/CancelCarActivity.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Component | ||
| public class CancelCarActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(CancelCarActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| TimeUnit.SECONDS.sleep(2); | ||
| } catch (InterruptedException e) { | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| String result = "Car canceled successfully"; | ||
| logger.info("Activity completed with result: " + result); | ||
| return result; | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...erns/src/main/java/io/dapr/springboot/examples/wfp/compensation/CancelFlightActivity.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Component | ||
| public class CancelFlightActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(CancelFlightActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| TimeUnit.SECONDS.sleep(2); | ||
| } catch (InterruptedException e) { | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| String result = "Flight canceled successfully"; | ||
| logger.info("Activity completed with result: " + result); | ||
| return result; | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...terns/src/main/java/io/dapr/springboot/examples/wfp/compensation/CancelHotelActivity.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.springboot.examples.wfp.compensation; | ||
|
|
||
| import io.dapr.workflows.WorkflowActivity; | ||
| import io.dapr.workflows.WorkflowActivityContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Component | ||
| public class CancelHotelActivity implements WorkflowActivity { | ||
| private static final Logger logger = LoggerFactory.getLogger(CancelHotelActivity.class); | ||
|
|
||
| @Override | ||
| public Object run(WorkflowActivityContext ctx) { | ||
| logger.info("Starting Activity: " + ctx.getName()); | ||
|
|
||
| try { | ||
| TimeUnit.SECONDS.sleep(2); | ||
| } catch (InterruptedException e) { | ||
siri-varma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| String result = "Hotel canceled successfully"; | ||
| logger.info("Activity completed with result: " + result); | ||
| return result; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.