go - 使用 Go-Stomp 为 ActiveMQ 缓存连接

标签 go stomp

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

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

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

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

最佳答案

您可以重用连接直到失败,请参阅 example来自 go-stomp 示例。

打开与否无法测试。

在库本身中,它们会在读取时吃掉错误,但不会在发送时吃掉:

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

关于go - 使用 Go-Stomp 为 ActiveMQ 缓存连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926096/

相关文章:

Golang panic 崩溃预防

java - Spring 4 STOMP Websockets 心跳

c# - PurgeTempDestinations 没有实现

python - 无法使用 STOMP 将消息发送到 activemq

angular - ng2 stomp 客户端 unsubscribe() 不起作用

带有 CloudSQL 的 Golang Gorm db.raw 更新 SQL 查询不起作用?

postgresql - sqlx.Connect() 卡在 docker alpine :latest 中

Go Mod 私有(private) repo

arrays - 如何在不定义数组大小的情况下将数组传递给 GO 函数?

go - go stomp 客户端中 ActiveMQ 的故障转移 URI