Skip to content

Commit c07db16

Browse files
committed
extend the time trigger sample with Retry policies
1 parent abf1159 commit c07db16

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/FunctionInvoker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@
6565
* @param <O> result type
6666
* @author Oleg Zhurakousky
6767
* @author Chris Bono
68+
* @author Christian Tzolov
69+
*
6870
* @since 3.2
6971
*
70-
* @deprecated since 4.0.0 in favor of dependency injection model implemented by {@link AzureFunctionInstanceInjector}.
72+
* @deprecated since 4.0.0 in favor of the dependency injection implementation {@link AzureFunctionInstanceInjector}.
7173
* Follow the official documentation for further information.
7274
*/
7375
@Deprecated

spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/HttpFunctionInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @since 3.2
3232
*
33-
* @deprecated since 4.0.0 in favor of dependency injection model implemented by {@link AzureFunctionInstanceInjector}.
33+
* @deprecated since 4.0.0 in favor of the dependency injection implementation {@link AzureFunctionInstanceInjector}.
3434
* Follow the official documentation for further information.
3535
*/
3636
@Deprecated

spring-cloud-function-samples/function-sample-azure-time-trigger/README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ In result the the `uppercase` Spring Cloud Function is called and uppercase the
3131
[2022-10-11T08:53:00.011Z] Execution Context Log - TimeInfo: {"Schedule":{"AdjustForDST":true},"ScheduleStatus":{"Last":"2022-10-11T10:52:00.003967+02:00","Next":"2022-10-11T10:53:00+02:00","LastUpdated":"2022-10-11T10:52:00.003967+02:00"},"IsPastDue":false}
3232
```
3333

34+
The `executeExpRetry` handler demonstrates how to handle errors using the https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=exponential-backoff%2Cin-process&pivots=programming-language-java#retry-policies[Retry policies].
35+
Sample emulates 3 errors on the first 3 executions and then continues as expected.
36+
3437
=== Running on Azure
3538

3639
Make sure you are logged in your Azure account.

spring-cloud-function-samples/function-sample-azure-time-trigger/src/main/java/com/example/azure/di/timetriggerdemo/UppercaseHandler.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package com.example.azure.di.timetriggerdemo;
1818

19+
import java.util.concurrent.atomic.AtomicInteger;
1920
import java.util.function.Consumer;
2021

2122
import com.microsoft.azure.functions.ExecutionContext;
23+
import com.microsoft.azure.functions.annotation.ExponentialBackoffRetry;
24+
import com.microsoft.azure.functions.annotation.FixedDelayRetry;
2225
import com.microsoft.azure.functions.annotation.FunctionName;
2326
import com.microsoft.azure.functions.annotation.TimerTrigger;
2427

@@ -32,10 +35,13 @@ public class UppercaseHandler {
3235

3336
public static String EXECUTION_CONTEXT = "executionContext";
3437

38+
private static AtomicInteger count = new AtomicInteger();
39+
3540
@Autowired
3641
private Consumer<Message<String>> uppercase;
3742

3843
@FunctionName("uppercase")
44+
@FixedDelayRetry(maxRetryCount = 4, delayInterval = "00:00:10")
3945
public void execute(@TimerTrigger(name = "keepAliveTrigger", schedule = "0 */1 * * * *") String timerInfo,
4046
ExecutionContext context) {
4147

@@ -46,4 +52,24 @@ public void execute(@TimerTrigger(name = "keepAliveTrigger", schedule = "0 */1 *
4652

4753
this.uppercase.accept(message);
4854
}
55+
56+
@FunctionName("uppercaseExpRetry")
57+
@ExponentialBackoffRetry(maxRetryCount = 4, maximumInterval = "00:15:00", minimumInterval = "00:00:03")
58+
public void executeExpRetry(@TimerTrigger(name = "keepAliveTrigger", schedule = "*/10 * * * * *") String timerInfo,
59+
ExecutionContext context) {
60+
61+
if (count.incrementAndGet() < 3) {
62+
context.getLogger().info("EMULATE ERROR# " + count.get());
63+
throw new IllegalStateException("Emulated ERROR# " + count.get());
64+
}
65+
66+
context.getLogger().info("ERRORLESS EXECUTION");
67+
68+
Message<String> message = MessageBuilder
69+
.withPayload(timerInfo)
70+
.setHeader(EXECUTION_CONTEXT, context)
71+
.build();
72+
73+
this.uppercase.accept(message);
74+
}
4975
}

0 commit comments

Comments
 (0)