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