douhuan9289 2016-04-20 09:52
浏览 431
已采纳

Golang AMQP重新连接

I want to test the restart connection to the rabbitmq server. On wrote small script to test. http://play.golang.org/p/l3ZWzG0Qqb But it's not working.

In step 10, I close the channel and connection. And open them again. And re-create chan amqp.Confirmation ( :75) . And continue the cycle. But after that, from the chan confirms nothing return.

UPD: code here.

package main

import (
    "fmt"
    "github.com/streadway/amqp"
    "log"
    "os"
    "time"
)

const SERVER = "amqp://user:pass@localhost:5672/"
const EXCHANGE_NAME = "publisher.test.1"
const EXCHANGE_TYPE = "direct"
const ROUTING_KEY = "publisher.test"

var Connection *amqp.Connection
var Channel *amqp.Channel

func setup(url string) (*amqp.Connection, *amqp.Channel, error) {
    conn, err := amqp.Dial(url)
    if err != nil {
        return nil, nil, err
    }

    ch, err := conn.Channel()
    if err != nil {
        return nil, nil, err
    }

    return conn, ch, nil
}

func main() {
    url := SERVER

    Connection, Channel, err := setup(url)
    if err != nil {
        fmt.Println("err publisher setup:", err)
        return
    }

    confirms := Channel.NotifyPublish(make(chan amqp.Confirmation, 1))
    if err := Channel.Confirm(false); err != nil {
        log.Fatalf("confirm.select destination: %s", err)
    }

    for i := 1; i <= 3000000; i++ {
        log.Println(i)

        if err != nil {
            fmt.Println("err consume:", err)
            return
        }

        if err := Channel.Publish(EXCHANGE_NAME, ROUTING_KEY, false, false, amqp.Publishing{
            Body: []byte(fmt.Sprintf("%d", i)),
        }); err != nil {
            fmt.Println("err publish:", err)
            log.Printf("%+v", err)
            os.Exit(1)
            return
        }

        // only ack the source delivery when the destination acks the publishing
        confirmed := <-confirms
        if confirmed.Ack {
            log.Printf("confirmed delivery with delivery tag: %d", confirmed.DeliveryTag)
        } else {
            log.Printf("failed delivery of delivery tag: %d", confirmed.DeliveryTag)
            // TODO. Reconnect will be here
        }

        if i == 10 {
            Channel.Close()
            Connection.Close()
            while := true
            for while {
                log.Println("while")
                time.Sleep(time.Second * 1)
                Connection, Channel, err = setup(url)
                if err == nil {
                    while = false
                    confirms = Channel.NotifyPublish(make(chan amqp.Confirmation, 1))
                    log.Printf("%+v", confirms)
                }
            }
        }
        time.Sleep(time.Millisecond * 300)
    }

    os.Exit(1)
}
  • 写回答

1条回答 默认 最新

  • dss087358 2016-04-20 11:47
    关注

    You should put channel in confirm mode. by calling the channel.Confirm() method. After closing the connection and even after getting new channel on the same connection, you should call Confirm() method again, since the channel is different from the old channel, and the default for all new channel is not to send confirm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)