doucong8553 2016-10-07 09:41
浏览 258
已采纳

如何解决Go-Stomp读取超时

Try subscribing ActiveMQ(Apollo) using Go-Stomp, but I am having read timeout error. My app should be alive 24 hours per day to process incoming message.

Question :

  1. Is there a way to keep the subcription although there is no more message exist in the queue? Trying to put ConnOpt.HeartBeat also does not seems to work
  2. Why after the readTimeout, it seems that I still accept one more message ?

Below is my steps :

  • I put 1000 messages for testing in the inputQueue
  • Run a subscriber, code provided below
  • Subscriber finished reading 1000 messages After 2-3 seconds, saw error " 2016/10/07 17:12:44 Subscription 1: /queue/hflc-in: ERROR message:read timeout".
  • Put another 1000 messages, but it seems the subscription is already down, therefore no message is not being processed

My Code :

  var(
   serverAddr   = flag.String("server", "10.92.10.10:61613", "STOMP server    endpoint")
   messageCount = flag.Int("count", 10, "Number of messages to send/receive")
   inputQ       = flag.String("inputq", "/queue/hflc-in", "Input queue")
)

var options []func(*stomp.Conn) error = []func(*stomp.Conn) error{
   stomp.ConnOpt.Login("userid", "userpassword"),
   stomp.ConnOpt.Host("mybroker"),
   stomp.ConnOpt.HeartBeat(360*time.Second, 360*time.Second), // I put this but seems no impact
}

func main() {
  flag.Parse()
  jobschan := make(chan bean.Request, 10)
  //my init setup
  go getInput(1, jobschan)
}

func getInput(id int, jobschan chan bean.Request) {
   conn, err := stomp.Dial("tcp", *serverAddr, options...)

   if err != nil {
      println("cannot connect to server", err.Error())
      return
   }
   fmt.Printf("Connected %v 
", id)

   sub, err := conn.Subscribe(*inputQ, stomp.AckClient)
   if err != nil {
     println("cannot subscribe to", *inputQ, err.Error())
     return
   }

   fmt.Printf("Subscribed %v 
", id)
   var messageCount int
   for {
    msg := <-sub.C
    //expectedText := fmt.Sprintf("Message #%d", i)
    if msg != nil {

        actualText := string(msg.Body)

        var req bean.Request
        if actualText != "SHUTDOWN" {
            messageCount = messageCount + 1
            var err2 = easyjson.Unmarshal([]byte(actualText), &req)
            if err2 != nil {
                log.Error("Unable unmarshall", zap.Error(err))
                println("message body %v", msg.Body) // what is [0/0]0x0 ?
            } else {
                fmt.Printf("Subscriber %v received message, count %v 
  ", id, messageCount)
                jobschan <- req
            }
        } else {
            logchan <- "got some issue"
        }
    }
   }
  }

Error :

2016/10/07 17:12:44 Subscription 1: /queue/hflc-in: ERROR message:read timeout
[E] 2016-10-07T09:12:44Z Unable unmarshall
message body %v [0/0]0x0

  • 写回答

1条回答 默认 最新

  • douyakao5308 2016-10-10 03:47
    关注

    Solved by adding these lines :

    in Apollo, noticed that the queue deleted after being empty after several secs, so put auto_delete_after to several hours in apollo.xml , eg :

    <queue id="hflc-in" dlq="dlq-in" nak_limit="3" auto_delete_after="7200"/>
    <queue id="hflc-log" dlq="dlq-log" nak_limit="3" auto_delete_after="7200"/>
    <queue id="hflc-out" dlq="dlq-out" nak_limit="3" auto_delete_after="7200"/>
    

    in Go, noticed that go-stomp will just give up right after it can't found any message in queue, so in the conn options, add HeartBeat Error

    var options []func(*stomp.Conn) error = []func(*stomp.Conn) error{
       //.... original configuration
       stomp.ConnOpt.HeartBeatError(360 * time.Second),
    }
    

    However still confused about the question no 2.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能