2025-09-19 16:17:46 +08:00
|
|
|
package rabbit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func NewPublisher(ctx context.Context, tag string, xqk *XQK) (*rmq.Publisher, error) {
|
2025-09-19 16:17:46 +08:00
|
|
|
cli := client
|
|
|
|
|
if tag != "default" {
|
2025-11-06 21:09:50 +08:00
|
|
|
var err error
|
|
|
|
|
cli, err = NewClient(ctx, tag)
|
2025-09-19 16:17:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cli.conn.NewPublisher(context.Background(), &rmq.ExchangeAddress{
|
2025-11-06 21:09:50 +08:00
|
|
|
Exchange: xqk.Exchange,
|
|
|
|
|
Key: xqk.Key,
|
2025-09-19 16:17:46 +08:00
|
|
|
}, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:56:11 +08:00
|
|
|
func Publishing(ctx context.Context, publisher *rmq.Publisher, msgChan <-chan []byte) {
|
2025-09-19 16:17:46 +08:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case msg := <-msgChan:
|
|
|
|
|
result, err := publisher.Publish(ctx, rmq.NewMessage(msg))
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = err // TODO
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch result.Outcome.(type) {
|
|
|
|
|
case *rmq.StateAccepted:
|
|
|
|
|
// TODO: "Message accepted"
|
2025-10-11 14:56:11 +08:00
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
case *rmq.StateReleased:
|
2025-10-11 14:56:11 +08:00
|
|
|
// TODO: "Message not routed"
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
case *rmq.StateRejected:
|
2025-10-11 14:56:11 +08:00
|
|
|
// TODO: "Message rejected"
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
default:
|
2025-10-11 14:56:11 +08:00
|
|
|
// *rmp.StateModified
|
|
|
|
|
// *rmq.StateReceived
|
2025-09-19 16:17:46 +08:00
|
|
|
// TODO: ("Message state: %v", publishResult.Outcome)
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
|
// TODO
|
|
|
|
|
fmt.Println("second passed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|