How to Receive a Message from an Apache ActiveMQ Topic with Go

W/o Durability

package main

import "github.com/jjeffery/stomp"

func main() {
  conn, _ := stomp.Dial("tcp", "localhost:61613", &stomp.Options {})

  sub, _ := conn.Subscribe("/topic/SampleTopic", stomp.AckAuto)

  msg := <- sub.C

  println(string(msg.Body))

  conn.Disconnect()
}

W/ Durability

package main

import (
  "fmt"

  "github.com/jjeffery/stomp"
)

func main() {
  conn, _ := stomp.Dial("tcp", "localhost:61613", &stomp.Options { NonStandard: stomp.NewHeader("client-id", "SampleClient") })

  sub, _ := conn.SubscribeWithHeaders("/topic/SampleTopic", stomp.AckAuto, stomp.NewHeader("activemq.subscriptionName", "SampleSubscription"))

  msg := <- sub.C

  fmt.Printf("%v\n", msg.Body)

  conn.Disconnect()
}

Published by:

Fernando Ribeiro

Experienced tech executive with a 24-year track record in enterprise computing. Leading AWS's professional services application modernization division in Brazil. He's held diverse roles including management, solutions architecture, sales consultancy, and full-stack development at major players like Oracle, Red Hat, and IBM. Fernando also contributes to open source and writes about emerging technologies. The views expressed here are his own and do not necessarily reflect the views of AWS.

Categories SoftwareTags , , 4 Comments

4 thoughts on “How to Receive a Message from an Apache ActiveMQ Topic with Go”

  1. I can’t use your code because everytime gives me this error

    cannot use stomp.Options literal (type *stomp.Options) as type stomp.Options in argument to stomp.Dial

  2. In case anyone else runs into this issue, the signature for the Dial is:

    func Dial(network, addr string, opts Options)

    So it’s not a pointer. Remove the “&” from “stomp.Options” i.e.

    conn, _ := stomp.Dial(“tcp”, “localhost:61613”, stomp.Options{})

  3. Thanks for the correction. I am guessing it is a difference from the time I wrote the post. I haven’t updated it in a long time.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.