tessl install tessl/golang-github-com--azure--azure-sdk-for-go--sdk--messaging--azservicebus@1.10.1Client module for Azure Service Bus, a highly reliable cloud messaging service providing real-time and fault-tolerant communication between distributed senders and receivers.
The Client is the main entry point for interacting with Azure Service Bus. It manages connections and creates senders and receivers for queues, topics, and subscriptions.
Create a Service Bus client using Azure identity credentials or a connection string.
func NewClient(fullyQualifiedNamespace string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error)Creates a client using token-based authentication with Azure Active Directory. The fullyQualifiedNamespace is the Service Bus namespace (e.g., "myservicebus.servicebus.windows.net"). The credential is typically from the azidentity package (e.g., azidentity.NewDefaultAzureCredential()).
Example:
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
client, err := azservicebus.NewClient(
"myservicebus.servicebus.windows.net",
credential,
nil,
)func NewClientFromConnectionString(connectionString string, options *ClientOptions) (*Client, error)Creates a client using a connection string with SharedAccessKeyName and SharedAccessKey:
Endpoint=sb://<sb>.servicebus.windows.net/;SharedAccessKeyName=<key name>;SharedAccessKey=<key value>Or with SharedAccessSignature:
Endpoint=sb://<sb>.servicebus.windows.net;SharedAccessSignature=SharedAccessSignature sr=<sb>.servicebus.windows.net&sig=<base64-sig>&se=<expiry>&skn=<keyname>Example:
client, err := azservicebus.NewClientFromConnectionString(
"Endpoint=sb://myservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=...",
nil,
)type ClientOptions struct {
TLSConfig *tls.Config
ApplicationID string
CustomEndpoint string
NewWebSocketConn func(ctx context.Context, args NewWebSocketConnArgs) (net.Conn, error)
RetryOptions RetryOptions
}Example with custom retry options:
client, err := azservicebus.NewClient(
"myservicebus.servicebus.windows.net",
credential,
&azservicebus.ClientOptions{
RetryOptions: azservicebus.RetryOptions{
MaxRetries: 5,
RetryDelay: time.Second * 2,
MaxRetryDelay: time.Second * 30,
},
},
)func (client *Client) NewSender(queueOrTopic string, options *NewSenderOptions) (*Sender, error)Creates a Sender for a queue or topic to send messages.
Example:
sender, err := client.NewSender("myqueue", nil)
if err != nil {
panic(err)
}
defer sender.Close(context.Background())func (client *Client) NewReceiverForQueue(queueName string, options *ReceiverOptions) (*Receiver, error)Creates a Receiver for a queue to receive messages.
Example:
receiver, err := client.NewReceiverForQueue("myqueue", &azservicebus.ReceiverOptions{
ReceiveMode: azservicebus.ReceiveModePeekLock,
})
if err != nil {
panic(err)
}
defer receiver.Close(context.Background())func (client *Client) NewReceiverForSubscription(topicName, subscriptionName string, options *ReceiverOptions) (*Receiver, error)Creates a Receiver for a topic subscription to receive messages.
Example:
receiver, err := client.NewReceiverForSubscription("mytopic", "mysubscription", nil)
if err != nil {
panic(err)
}
defer receiver.Close(context.Background())Session receivers are used for session-aware queues and subscriptions, which ensure FIFO (first-in-first-out) message ordering within a session.
func (client *Client) AcceptSessionForQueue(ctx context.Context, queueName, sessionID string, options *SessionReceiverOptions) (*SessionReceiver, error)Accepts a specific session from a queue by session ID. The receiver is initialized immediately, not lazily.
Example:
sessionReceiver, err := client.AcceptSessionForQueue(
context.Background(),
"mysessionqueue",
"session123",
nil,
)
if err != nil {
panic(err)
}
defer sessionReceiver.Close(context.Background())func (client *Client) AcceptSessionForSubscription(ctx context.Context, topicName, subscriptionName, sessionID string, options *SessionReceiverOptions) (*SessionReceiver, error)Accepts a specific session from a subscription by session ID. The receiver is initialized immediately.
Example:
sessionReceiver, err := client.AcceptSessionForSubscription(
context.Background(),
"mytopic",
"mysessionsubscription",
"session123",
nil,
)func (client *Client) AcceptNextSessionForQueue(ctx context.Context, queueName string, options *SessionReceiverOptions) (*SessionReceiver, error)Accepts the next available session from a queue. Returns an error with Code == CodeTimeout if no sessions are available within the timeout period. The receiver is initialized immediately.
Example:
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
sessionReceiver, err := client.AcceptNextSessionForQueue(ctx, "mysessionqueue", nil)
if err != nil {
var sbErr *azservicebus.Error
if errors.As(err, &sbErr) && sbErr.Code == azservicebus.CodeTimeout {
fmt.Println("No available sessions")
return
}
panic(err)
}
defer sessionReceiver.Close(context.Background())func (client *Client) AcceptNextSessionForSubscription(ctx context.Context, topicName, subscriptionName string, options *SessionReceiverOptions) (*SessionReceiver, error)Accepts the next available session from a subscription. Returns an error with Code == CodeTimeout if no sessions are available within the timeout period. The receiver is initialized immediately.
Example:
sessionReceiver, err := client.AcceptNextSessionForSubscription(
context.Background(),
"mytopic",
"mysessionsubscription",
nil,
)func (client *Client) Close(ctx context.Context) errorCloses the client and all associated senders and receivers. Always call Close when done to release resources.
Example:
defer client.Close(context.Background())type ClientOptions struct {
TLSConfig *tls.Config
ApplicationID string
CustomEndpoint string
NewWebSocketConn func(ctx context.Context, args NewWebSocketConnArgs) (net.Conn, error)
RetryOptions RetryOptions
}Configuration options for creating a Client.
type NewSenderOptions struct {
// Currently empty, reserved for future expansion
}Options for creating a Sender (currently no configurable options).
type ReceiverOptions struct {
ReceiveMode ReceiveMode
SubQueue SubQueue
}Options for creating a Receiver:
ReceiveModePeekLock (default) or ReceiveModeReceiveAndDeleteSubQueueDeadLetter or SubQueueTransfertype SessionReceiverOptions struct {
ReceiveMode ReceiveMode
}Options for creating a SessionReceiver:
ReceiveModePeekLock (default) or ReceiveModeReceiveAndDeletetype RetryOptions struct {
MaxRetries int
RetryDelay time.Duration
MaxRetryDelay time.Duration
}Configuration for automatic retries:
type NewWebSocketConnArgs struct {
// Host is the fully qualified namespace
Host string
}Arguments passed to the WebSocket connection factory function.