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

使用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
}

图片转代码服务由CSDN问答提供 功能建议

使用Go-Stomp,可以使用以下代码获得连接。

 如果conn,则err = stomp.Dial(“ tcp”,
 Broker.URI,
 stomp.ConnOpt.Login(Broker.User,Broker.Password));  err!= nil {
 panic(fmt.Sprintf(“无法使用brokerUri%v连接到ActiveMQ。无法继续。”,Broker.URI))
} 
   
  
 

是否可以缓存连接以便重复使用以发送不同请求的消息? 还是我们每次需要发送消息时都需要获得连接?
后来听起来效率低下。连接实例上的
发送方法会在发生故障时关闭连接。 因此,如果我们缓存它,则必须检查该连接是否仍然有效,以供后续发送消息调用使用。 但是我没有找到检查连接是否关闭的方法? Conn 结构已关闭成员,但是不会通过任何方法公开。

  // Conn是与 STOMP服务器。 使用
 // Dial或Connect函数创建一个Conn。
键入Conn struct {
 conn io.ReadWriteCloser 
 readCh chan * frame.Frame 
 writeCh chan writeRequest 
版本Version 
会话字符串
服务器 字符串
 readTimeout时间.Duration 
 writeTimeout时间.Duration 
关闭bool 
选项* 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
            }
        }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douxiong0668 2016-03-10 21:06

    You can reuse the connection until failure, see the example from the go-stomp examples.

    There is no way of testing if open or not.

    in the library itself they eat the error on the read, but not on send:

    if err != nil {
            if err == io.EOF {
                log.Println("connection closed:", c.rw.RemoteAddr())
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题