2016-03-10 36 views
0

使用Go-Stomp,可以使用下面的代码获取连接。使用Go-Stomp为ActiveMQ缓存连接

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)) 
    } 

可以缓存连接以重复使用以发送不同请求的消息吗? 或者我们是否每次需要发送消息都需要获取连接?
后来听起来效率很高。
发送连接实例上的方法在发生故障时关闭连接。因此,如果我们缓存它,则必须检查连接是否仍然为后续发送消息调用而生效。但我没有找到检查连接是否关闭的方法? Conn struct有已关闭成员,但未通过任何方法公开。

// 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 
} 

回答

0

我添加代码来处理故障,并检查特定的错误。

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 
     } 
    } 
0

可以重新使用,直到发生故障的连接,看到从中间人跺脚例子example

如果打开或没有测试,则无法进行测试。

库本身他们吃错误的读,但不能发送:

if err != nil { 
     if err == io.EOF { 
      log.Println("connection closed:", c.rw.RemoteAddr())