rabbitmq
This commit is contained in:
parent
63f13fa409
commit
92c6d693e3
@ -93,6 +93,9 @@ func (c *Connection) Connect() error {
|
|||||||
|
|
||||||
// 设置确认通道
|
// 设置确认通道
|
||||||
c.notifyClose = make(chan *amqp.Error)
|
c.notifyClose = make(chan *amqp.Error)
|
||||||
|
if err = c.channel.Confirm(false); err != nil {
|
||||||
|
return errors.Join(err, errors.New("failed to set confirm"))
|
||||||
|
}
|
||||||
c.notifyConfirm = make(chan amqp.Confirmation)
|
c.notifyConfirm = make(chan amqp.Confirmation)
|
||||||
c.channel.NotifyClose(c.notifyClose)
|
c.channel.NotifyClose(c.notifyClose)
|
||||||
c.channel.NotifyPublish(c.notifyConfirm)
|
c.channel.NotifyPublish(c.notifyConfirm)
|
||||||
|
@ -2,6 +2,7 @@ package rmq
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/fox/fox/log"
|
"github.com/fox/fox/log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -61,12 +62,15 @@ func TestRabbitmq(t *testing.T) {
|
|||||||
// 示例:发送测试消息
|
// 示例:发送测试消息
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(2 * time.Second) // 等待消费者启动
|
time.Sleep(2 * time.Second) // 等待消费者启动
|
||||||
testMessage := "hello world"
|
for i := 0; i < 10; i++ {
|
||||||
if err = producer.Publish(ctx, testMessage); err != nil {
|
testMessage := fmt.Sprintf("hello world %d", i)
|
||||||
log.ErrorF("Failed to publish test message: %v", err)
|
if err = producer.Publish(ctx, testMessage); err != nil {
|
||||||
} else {
|
log.ErrorF("Failed to publish test message: %v", err)
|
||||||
log.Info("Test message published successfully")
|
} else {
|
||||||
|
log.Info("Test message published successfully")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 等待中断信号
|
// 等待中断信号
|
||||||
|
Loading…
x
Reference in New Issue
Block a user