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

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

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