-
Notifications
You must be signed in to change notification settings - Fork 21
/
managedsettling_example_test.go
60 lines (54 loc) · 1.92 KB
/
managedsettling_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package shuttle_test
import (
"context"
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"github.com/Azure/go-shuttle/v2"
)
func ExampleNewManagedSettlingHandler() {
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
client, err := azservicebus.NewClient("myservicebus.servicebus.windows.net", tokenCredential, nil)
if err != nil {
panic(err)
}
receiver, err := client.NewReceiverForSubscription("topic-a", "sub-a", nil)
if err != nil {
panic(err)
}
lockRenewalInterval := 10 * time.Second
p := shuttle.NewProcessor(receiver,
shuttle.NewPanicHandler(nil,
shuttle.NewRenewLockHandler(&shuttle.LockRenewalOptions{Interval: &lockRenewalInterval},
shuttle.NewManagedSettlingHandler(&shuttle.ManagedSettlingOptions{
RetryDecision: &shuttle.MaxAttemptsRetryDecision{MaxAttempts: 2},
RetryDelayStrategy: &shuttle.ConstantDelayStrategy{Delay: 2 * time.Second},
OnAbandoned: func(ctx context.Context, message *azservicebus.ReceivedMessage, err error) {
fmt.Printf("message abandoned due to error: %s\n", err)
},
OnDeadLettered: func(ctx context.Context, message *azservicebus.ReceivedMessage, err error) {
fmt.Printf("message deadlettered due to error: %s\n", err)
},
}, myManagedSettlementHandler()))), &shuttle.ProcessorOptions{MaxConcurrency: 10})
ctx, cancel := context.WithCancel(context.Background())
err = p.Start(ctx)
if err != nil {
panic(err)
}
cancel()
}
func myManagedSettlementHandler() shuttle.ManagedSettlingHandler {
count := 0
return shuttle.ManagedSettlingFunc(func(ctx context.Context, message *azservicebus.ReceivedMessage) error {
count++
if count == 0 {
// this will abandon the message
return fmt.Errorf("this will abandon the message, and eventually move it to DLQ")
}
return nil // this will complete the message
})
}