使用Go-Stomp缓存ActiveMQ的连接

huangapple go评论87阅读模式
英文:

Caching connection for ActiveMQ using 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
}
英文:

Using Go-Stomp, one can obtain the connection using below code.

<!-- language: go -->

if conn, err = stomp.Dial(&quot;tcp&quot;,
		Broker.URI,
		stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
		panic(fmt.Sprintf(&quot;Could not connect to ActiveMQ using brokerUri %v. Can not continue.&quot;, 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? <br>Later sounds in-efficient.<br>
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.

<!-- language: go -->

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

答案1

得分: 0

你可以在连接失败之前重复使用该连接,可以参考go-stomp示例中的示例

无法测试连接是否打开。

在库本身中,他们会忽略读取时的错误,但不会忽略发送时的错误:

if err != nil {
    if err == io.EOF {
        log.Println("连接已关闭:", c.rw.RemoteAddr())
英文:

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(&quot;connection closed:&quot;, c.rw.RemoteAddr())

答案2

得分: 0

我添加了处理失败和检查具体错误的代码。

if err := conn.Send(queue, "text/plain", []byte(message)); err != nil {
    if err == stomp.ErrAlreadyClosed {
        log.Println("ActiveMQ连接已关闭。重新连接...")
        conn = ConnectToBroker()
        err = conn.Send(queue, "text/plain", []byte(message))
    }
    if err != nil {
        log.Printf("发送消息到队列 %v 失败。错误信息:%v,消息内容:%v", queue, err.Error(), message)
    }
    return err
}

请注意,这是一个示例代码片段,其中使用了一些自定义的变量和函数。你需要根据你的实际情况进行适当的修改和调整。

英文:

I added code to handle failures and check the specific error.

<!-- language: go -->

if err := conn.Send(queue, &quot;text/plain&quot;, []byte(message)); err != nil {
			if err == stomp.ErrAlreadyClosed {
				log.Println(&quot;ActiveMQ Connection is in closed state. Reconnecting ...&quot;)
				conn = ConnectToBroker()
				err = conn.Send(queue, &quot;text/plain&quot;, []byte(message))
			}
			if err != nil {
				log.Printf(&quot;Failed to send message to Queue %v.  Error is %v, Payload is %v&quot;, queue, err.Error(), message)
			}
			return err
		}
	}

huangapple
  • 本文由 发表于 2016年3月11日 04:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/35926096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定