47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
|
package rabbit
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
||
|
|
)
|
||
|
|
|
||
|
|
type rabbitClient struct {
|
||
|
|
env *rmq.Environment
|
||
|
|
conn *rmq.AmqpConnection
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewClient(ctx context.Context, endpoints []rmq.Endpoint) (*rabbitClient, error) {
|
||
|
|
|
||
|
|
env := rmq.NewClusterEnvironment(endpoints)
|
||
|
|
conn, err := client.env.NewConnection(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &rabbitClient{
|
||
|
|
env: env,
|
||
|
|
conn: conn,
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *rabbitClient) Management() *rmq.AmqpManagement {
|
||
|
|
return c.conn.Management()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *rabbitClient) NewPublisher(ctx context.Context, destination rmq.ITargetAddress,
|
||
|
|
options rmq.IConsumerOptions) (*rmq.Publisher, error) {
|
||
|
|
|
||
|
|
return c.conn.NewPublisher(ctx, destination, options)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *rabbitClient) NewConsumer(ctx context.Context, queueName string,
|
||
|
|
options rmq.IConsumerOptions) (*rmq.Consumer, error) {
|
||
|
|
|
||
|
|
return c.conn.NewConsumer(ctx, queueName, options)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *rabbitClient) Close(ctx context.Context) error {
|
||
|
|
return c.env.CloseConnections(ctx)
|
||
|
|
}
|