suman j
suman j

Reputation: 6980

failover URI for ActiveMQ in go stomp client

How do we connect to ActiveMQ using failover stomp connection URI in Go? Using Go-Stomp client, I tried below code and it fails to connect.

if conn, err = stomp.Dial("tcp", "failover:(tcp://10.01.02.03:61613,tcp://10.04.05.06:61613)?startupMaxReconnectAttempts=2"); err != nil {
        panic(fmt.Sprintf("Could not connect to ActiveMQ using brokerUri %v. Can not continue.", Config.Broker.URI))
    }

Upvotes: 1

Views: 1779

Answers (2)

suman j
suman j

Reputation: 6980

Due to lack of support for failover, had to write some code to achieve the desired result.

//connect to ActiveMQ using failover approach
    var err error
    for _, uri := range ["10.01.02.03:61613","10.04.05.06:61613", {
        if err = connect(uri); err == nil {
            break
        }
    }
    if conn == nil {
        panic(fmt.Sprintf("Could not connect to ActiveMQ using brokerUri. Can not continue."))
    }

func connect(brokerIp string) (err error) {
    log.Printf("Attempting to connect to ActiveMQ node %v", brokerIp)
    if conn, err = stomp.Dial("tcp",
        brokerIp,
        stomp.ConnOpt.Login(Broker.User, Broker.Password)); err != nil {
        log.Printf("Faild to connect to ActiveMQ using %v", brokerIp)
    }
    if err == nil {
        log.Printf("Successfully connected to ActiveMQ node %v", brokerIp)
    }
    return
}

Upvotes: 1

animal
animal

Reputation: 238

What is the err you are receiving? I do not believe the format of your Dial is correct: The Go-Stomp Dial uses the underlying net.Dial

func Dial(network, addr string, opts ...func(*Conn) error) (*Conn, error) {

c, err := net.Dial(network, addr)

The underlying net.Dial documentation states

For TCP and UDP networks, addresses have the form host:port. If host is a literal IPv6 address it must be enclosed in square brackets as in "[::1]:80" or "[ipv6-host%zone]:80". The functions JoinHostPort and SplitHostPort manipulate addresses in this form. If the host is empty, as in ":80", the local system is assumed.

There is no failover: syntax

Upvotes: 0

Related Questions