doujiaci7976 2016-03-10 20:00
浏览 304
已采纳

使用Go-Stomp缓存ActiveMQ的连接

Using Go-Stomp, one can obtain the connection using below code.

if conn, err = stomp.Dial("tcp",
        Broker.URI,
        stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
        panic(fmt.Sprintf("Could not connect to ActiveMQ using brokerUri %v. Can not continue.", Broker.URI))
    }

Can the connection be cached to reuse to send messages for different requests? Or do we need to obtain the connection each time one wants to send a message?
Later sounds in-efficient.
Send method on the connection instance closes the connection in case of failures. So if we cache it, one has to check if the connection is still live for subsequent send message invocations. But I did not find a method to check if the connection is closed? Conn struct has closed member but this is not exposed via any method.

// A Conn is a connection to a STOMP server. Create a Conn using either
// the Dial or Connect function.
type Conn struct {
    conn         io.ReadWriteCloser
    readCh       chan *frame.Frame
    writeCh      chan writeRequest
    version      Version
    session      string
    server       string
    readTimeout  time.Duration
    writeTimeout time.Duration
    closed       bool
    options      *connOptions
}
  • 写回答

2条回答 默认 最新

  • douhan4812 2016-03-10 21:12
    关注

    I added code to handle failures and check the specific error.

    if err := conn.Send(queue, "text/plain", []byte(message)); err != nil {
                if err == stomp.ErrAlreadyClosed {
                    log.Println("ActiveMQ Connection is in closed state. Reconnecting ...")
                    conn = ConnectToBroker()
                    err = conn.Send(queue, "text/plain", []byte(message))
                }
                if err != nil {
                    log.Printf("Failed to send message to Queue %v.  Error is %v, Payload is %v", queue, err.Error(), message)
                }
                return err
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器