Last active
April 29, 2019 06:31
-
-
Save ramya-rao-a/dc8dc7f6b9c2573995f7531cccedd507 to your computer and use it in GitHub Desktop.
Sample Go code to illustrate issue with deferred msgs received in ReceiveAndDelete mode
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
/* | |
Set up instructions: | |
- Ensure you have Go installed, else install from https://golang.org/dl/ | |
- Create a new folder and copy this file | |
- In that folder run `go mod init sbtests` | |
- Add your connection string and queue (no sessions) details | |
- Run `go run deferred_receiveAndDelete_mode.go` (if you have named the file differently, use that name) | |
*/ | |
package main | |
import ( | |
"context" | |
"fmt" | |
"time" | |
servicebus "github.com/Azure/azure-service-bus-go" | |
) | |
func main() { | |
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) | |
defer cancel() | |
// Use an empty queue | |
connStr := "" | |
queueName := "" | |
// Create a client to communicate with a Service Bus Namespace. | |
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr)) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Create a QueueClient in PeekLock mode | |
q, err := ns.NewQueue(queueName) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Send a message | |
fmt.Println("Sending: hello") | |
if err := q.Send(ctx, servicebus.NewMessageFromString("hello")); err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Receive and defer the message | |
var sequenceNumber int64 | |
err = q.ReceiveOne(ctx, servicebus.HandlerFunc(func(ctx context.Context, msg *servicebus.Message) error { | |
fmt.Println("Deferring: ", string(msg.Data)) | |
sequenceNumber = *msg.SystemProperties.SequenceNumber | |
return msg.Defer(ctx) | |
})) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Create new QueueClient, this time in ReceiveAndDelete mode | |
q2, err := ns.NewQueue(queueName, servicebus.QueueWithReceiveAndDelete()) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Receive the deferred message, but don't complete it | |
err = q2.ReceiveDeferred(ctx, servicebus.HandlerFunc(func(ctx context.Context, msg *servicebus.Message) error { | |
fmt.Println("Receiving deferred: ", string(msg.Data)) | |
fmt.Println("Locktoken: ", msg.LockToken) | |
return nil | |
// return msg.Complete(ctx) | |
}), sequenceNumber) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// PeekOne should return no results, but it does | |
peekedMsg, err := q.PeekOne(ctx) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println("Peeking: ", string(peekedMsg.Data)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment