func ExampleConn_Subscribe_1() error { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { return err } sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient) if err != nil { return err } // receive 5 messages and then quit for i := 0; i < 5; i++ { msg := <-sub.C if msg.Err != nil { return msg.Err } doSomethingWith(msg) // acknowledge the message err = conn.Ack(msg) if err != nil { return err } } err = sub.Unsubscribe() if err != nil { return err } return conn.Disconnect() }
func recvMessages(subscribed chan bool) { defer func() { stop <- true }() conn, err := stomp.Dial("tcp", *serverAddr, options...) if err != nil { println("cannot connect to server", err.Error()) return } sub, err := conn.Subscribe(*queueName, stomp.AckAuto) if err != nil { println("cannot subscribe to", *queueName, err.Error()) return } close(subscribed) for i := 1; i <= *messageCount; i++ { msg := <-sub.C expectedText := fmt.Sprintf("Message #%d", i) actualText := string(msg.Body) if expectedText != actualText { println("Expected:", expectedText) println("Actual:", actualText) } } println("receiver finished") }
func consume(config Config) { rabbitHost := fmt.Sprintf("%s:%s", config.RabbitHost, config.RabbitPort) conn, err := stomp.Dial("tcp", rabbitHost, stomp.ConnOpt.Login(config.RabbitUser, config.RabbitPassword), stomp.ConnOpt.AcceptVersion(stomp.V11), stomp.ConnOpt.AcceptVersion(stomp.V12), stomp.ConnOpt.Host(config.RabbitVHost), stomp.ConnOpt.Header("nonce", "B256B26D320A")) if err != nil { log.Fatal(err) } defer conn.Disconnect() subscription, err := conn.Subscribe(config.RabbitQueue, stomp.AckClient) if err != nil { log.Fatal(err) } for { payload := <-subscription.C log.Println("Received subscribed message -> ", string(payload.Body)) err = conn.Ack(payload) if err != nil { log.Fatal(err) } } err = subscription.Unsubscribe() if err != nil { log.Fatal(err) } }
// Connect to a stomp server func (p *DozerProtocolStomp) Dial(host string, port int64) error { bind := fmt.Sprintf("%s:%d", host, port) conn, err := stomp.Dial("tcp", bind, stomp.Options{}) if err != nil { return err } p.conn = conn return nil }
func TestConnectToActiveMQ(t *testing.T) { c, err := ConnectToActiveMQ(15, time.Millisecond*500, func(url string) bool { conn, err := stomp.Dial("tcp", url, stomp.ConnOpt.Login("admin", "admin")) if err != nil { return false } defer conn.Disconnect() return true }) assert.Nil(t, err) defer c.KillRemove() }
func run(config Config) { purl := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", config.DbUser, config.DbPassword, config.DbHost, config.DbPort, config.DbName, config.SslMode) listener := pq.NewListener(purl, 10*time.Second, time.Minute, errorReporter) err := listener.Listen("usertrigger") if err != nil { log.Fatal(err) } rabbitchannel := make(chan string, 100) //Code for STOMP go func() { rabbitHost := fmt.Sprintf("%s:%s", config.RabbitHost, config.RabbitPort) conn, err := stomp.Dial("tcp", rabbitHost, stomp.ConnOpt.Login(config.RabbitUser, config.RabbitPassword), stomp.ConnOpt.AcceptVersion(stomp.V11), stomp.ConnOpt.AcceptVersion(stomp.V12), stomp.ConnOpt.Host(config.RabbitVHost), stomp.ConnOpt.Header("nonce", "B256B26D320A")) if err != nil { log.Fatal(err) } defer conn.Disconnect() for { payload := <-rabbitchannel log.Println(payload) err = conn.Send(config.RabbitQueue, "text/plain", []byte(payload)) if err != nil { log.Fatal(err) } } }() for { select { case notification := <-listener.Notify: rabbitchannel <- notification.Extra case <-time.After(90 * time.Second): go func() { err := listener.Ping() if err != nil { log.Fatal(err) } }() } } }
//Connect to ActiveMQ and produce messages func main() { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { fmt.Println(err) } c := make(chan string) quit := make(chan string) go Producer(c, quit, conn) for { fmt.Println(<-c) } quit <- "read" }
// Connect to a STOMP server using default options. func ExampleDial_1() error { conn, err := stomp.Dial("tcp", "192.168.1.1:61613") if err != nil { return err } err = conn.Send( "/queue/test-1", // destination "text/plain", // content-type []byte("Test message #1")) // body if err != nil { return err } return conn.Disconnect() }
func ExampleTransaction() error { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { return err } defer conn.Disconnect() sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient) if err != nil { return err } // receive 5 messages and then quit for i := 0; i < 5; i++ { msg := <-sub.C if msg.Err != nil { return msg.Err } tx := conn.Begin() doAnotherThingWith(msg, tx) tx.Send("/queue/another-one", "text/plain", []byte(fmt.Sprintf("Message #%d", i)), nil) // acknowledge the message err = tx.Ack(msg) if err != nil { return err } err = tx.Commit() if err != nil { return err } } err = sub.Unsubscribe() if err != nil { return err } return nil }
func (feed *NREFeed) Subscribe(cfg Cfg) *NREUpdates { addr := server + ":" + strconv.Itoa(nrePort) log.Println("Stomping trains from:", addr, " with user:"******"tcp", addr, stomp.ConnOpt.Login(cfg.Nre.User, cfg.Nre.Pass)) failIf(err) log.Println("Dial success") feed.sub, err = feed.con.Subscribe("/queue/"+cfg.Nre.Queue, stomp.AckClient) failIf(err) log.Println("Subscribe success") results := make(NREUpdates) go func() { log.Println("Created NRE update thread") for { msg := <-feed.sub.C log.Print("Beginning receive") failIf(msg.Err) reader := bytes.NewReader(msg.Body) gz, err := gzip.NewReader(reader) failIf(err) buff := make([]byte, 1024) str := []byte{} for { n, _ := gz.Read(buff) if n == 0 { break } else { str = append(str, buff[:n]...) } } log.Println("rxed:", len(str), "bytes") results <- str err = feed.con.Ack(msg) failIf(err) } }() return &results }
//Connect to ActiveMQ and listen for messages func main() { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { fmt.Println(err) } sub, err := conn.Subscribe("/queue/test-1", stomp.AckAuto) if err != nil { fmt.Println(err) } for { msg := <-sub.C fmt.Println(msg) } err = sub.Unsubscribe() if err != nil { fmt.Println(err) } defer conn.Disconnect() }
// Connect to a STOMP server that requires authentication. In addition, // we are only prepared to use STOMP protocol version 1.1 or 1.2, and // the virtual host is named "dragon". In this example the STOMP // server also accepts a non-standard header called 'nonce'. func ExampleDial_2() error { conn, err := stomp.Dial("tcp", "192.168.1.1:61613", stomp.ConnOpt.Login("scott", "leopard"), stomp.ConnOpt.AcceptVersion(stomp.V11), stomp.ConnOpt.AcceptVersion(stomp.V12), stomp.ConnOpt.Host("dragon"), stomp.ConnOpt.Header("nonce", "B256B26D320A")) if err != nil { return err } err = conn.Send( "/queue/test-1", // destination "text/plain", // content-type []byte("Test message #1")) // body if err != nil { return err } return conn.Disconnect() }
func sendMessages() { defer func() { stop <- true }() conn, err := stomp.Dial("tcp", *serverAddr, options...) if err != nil { println("cannot connect to server", err.Error()) return } for i := 1; i <= *messageCount; i++ { text := fmt.Sprintf("Message #%d", i) err = conn.Send(*queueName, "text/plain", []byte(text), nil) if err != nil { println("failed to send to server", err) return } } println("sender finished") }