I trying to pull messages from azure service bus queue using Go, but I got an error when running the code. Here is my code.
func Example_queue_receive() {
ctx, cancel :=context.WithTimeout(context.Background(),10*time.Second)
defer cancel()
connectionString :="Endpoint=sb://{my_service_name}.servicebus.windows.net/;SharedAccessKeyName = RootManageSharedAccessKey;SharedAccessKey={my_shared_access_key_value}"
// Create a client to communicate with a Service Bus Namespace.
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connectionString))
if err != nil {
fmt.Println(err)
}
// Create a client to communicate with the queue.
q, err := ns.NewQueue("MyQueueName")
if err != nil {
fmt.Println("FATAL: ", err)
}
err = q.ReceiveOne(ctx, servicebus.HandlerFunc(func(ctx context.Context, message *servicebus.Message) servicebus.DispositionAction {
fmt.Println(string(message.Data))
return message.Complete()
}))
if err != nil {
fmt.Println("FATAL: ", err)
}
}
This is the error:
link detached, reason: *Error{Condition: amqp:not-found}
I searched for the error information in the Github repo, and I found the code ErrorNotFound MessageErrorCondition = "amqp:not-found"
, but there is not any explaination for the error.
I compared it with Exception types
in C# from the offical document Service Bus messaging exceptions
and my testing, I think it's the same as below.
In my environment go version go1.11.3 windows/amd64
, I run the similar code without an existing queue MyQueueName
, I got the similar error below.
FATAL: unhandled error link xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx: status code 404 and description: The messaging entity 'sb://.servicebus.windows.net/MyQueueName' could not be found. TrackingId:f9fc309d-xxxx-xxxx-xxxx-8fccd694f266_G42, SystemTracker:.servicebus.windows.MyQueueName, Timestamp:2019-01-25T09:45:28
So I think the error means your Queue MyQueueName
in your code is not existing in your Azure Service Bus, you should create it first before to use.
Meanwhile, as @JerryLiu said, your code below has some mistakes.
err = q.ReceiveOne(ctx, servicebus.HandlerFunc(func(ctx context.Context, message *servicebus.Message) servicebus.DispositionAction { fmt.Println(string(message.Data)) return message.Complete() }))
According to the godoc for azure-service-bus-go
, the parameter type of servicebus.HanderFunc
method must be HandlerFunc
which is a function return error
, not servicebus.DispositionAction
in your code.
And the method message.Complete
should be passed a parameter ctx
(a context
object) and return error
that not match servicebus.DispositionAction
. The message.CompleteAction
method return servicebus.DispositionAction
, but not suitable in the reciving message code.
Please refer to the example of godoc Example (QueueSendAndReceive)
to modify your code.