2024-09-11 20:42:05 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
2024-09-16 15:34:27 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/Azure/go-amqp"
|
|
|
|
|
)
|
2024-09-11 20:42:05 +08:00
|
|
|
|
|
|
|
|
type AMQPBindingInfo struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AMQPBinding struct {
|
2024-09-18 00:59:33 +08:00
|
|
|
sourceName string
|
|
|
|
|
destinationName string
|
|
|
|
|
toQueue bool
|
|
|
|
|
bindingKey string
|
|
|
|
|
management *AmqpManagement
|
2024-09-11 20:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newAMQPBinding(management *AmqpManagement) *AMQPBinding {
|
|
|
|
|
return &AMQPBinding{management: management}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *AMQPBinding) Key(bindingKey string) IBindingSpecification {
|
|
|
|
|
b.bindingKey = bindingKey
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
func (b *AMQPBinding) SourceExchange(exchangeSpec IExchangeSpecification) IBindingSpecification {
|
|
|
|
|
b.sourceName = exchangeSpec.GetName()
|
|
|
|
|
b.toQueue = false
|
2024-09-11 20:42:05 +08:00
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
func (b *AMQPBinding) SourceExchangeName(exchangeName string) IBindingSpecification {
|
|
|
|
|
b.sourceName = exchangeName
|
|
|
|
|
b.toQueue = false
|
2024-09-11 20:42:05 +08:00
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
func (b *AMQPBinding) DestinationExchange(exchangeSpec IExchangeInfo) IBindingSpecification {
|
|
|
|
|
b.destinationName = exchangeSpec.GetName()
|
|
|
|
|
b.toQueue = false
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *AMQPBinding) DestinationExchangeName(exchangeName string) IBindingSpecification {
|
|
|
|
|
b.destinationName = exchangeName
|
|
|
|
|
b.toQueue = false
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *AMQPBinding) DestinationQueue(queueSpec IQueueSpecification) IBindingSpecification {
|
|
|
|
|
b.destinationName = queueSpec.GetName()
|
|
|
|
|
b.toQueue = true
|
|
|
|
|
return b
|
|
|
|
|
}
|
2024-09-11 20:42:05 +08:00
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
func (b *AMQPBinding) DestinationQueueName(queueName string) IBindingSpecification {
|
|
|
|
|
b.destinationName = queueName
|
|
|
|
|
b.toQueue = true
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *AMQPBinding) Bind(ctx context.Context) error {
|
2024-09-11 20:42:05 +08:00
|
|
|
path := bindingPath()
|
|
|
|
|
kv := make(map[string]any)
|
|
|
|
|
kv["binding_key"] = b.bindingKey
|
2024-09-18 00:59:33 +08:00
|
|
|
kv["source"] = b.sourceName
|
|
|
|
|
kv["destination_queue"] = b.destinationName
|
2024-09-11 20:42:05 +08:00
|
|
|
kv["arguments"] = make(map[string]any)
|
|
|
|
|
_, err := b.management.Request(ctx, kv, path, commandPost, []int{responseCode204})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *AMQPBinding) Unbind(ctx context.Context) error {
|
2024-09-18 00:59:33 +08:00
|
|
|
bindingPathWithExchangeQueueKey := bindingPathWithExchangeQueueKey(b.toQueue, b.sourceName, b.destinationName, b.bindingKey)
|
2024-09-16 15:34:27 +08:00
|
|
|
_, err := b.management.Request(ctx, amqp.Null{}, bindingPathWithExchangeQueueKey, commandDelete, []int{responseCode204})
|
2024-09-11 20:42:05 +08:00
|
|
|
return err
|
|
|
|
|
}
|