github.com/vmware/transport-go@v1.3.4/plank/cmd/broker_sample/rabbitmq/over_stomp.go (about)

     1  // Copyright 2019-2021 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package rabbitmq
     5  
     6  import (
     7  	"github.com/vmware/transport-go/bridge"
     8  	"github.com/vmware/transport-go/bus"
     9  	"github.com/vmware/transport-go/plank/utils"
    10  	"os"
    11  	"time"
    12  )
    13  
    14  // ListenViaStomp directly connects to a RabbitMQ instance via TCP port 61613 that RabbitMQ STOMP plugin
    15  // exposes, and listens on topic "something.somewhere". also it sends out a test message to the topic
    16  // after a one second delay. note that RabbitMQ STOMP plugin routes messages arriving from the client
    17  // to the amq.topic exchange. for details see producer.go
    18  func ListenViaStomp(c2 chan os.Signal) {
    19  	b := bus.GetBus()
    20  	bus.EnableLogging(true)
    21  
    22  	// connect to the RabbitMQ STOMP endpoint
    23  	broker, err := b.ConnectBroker(&bridge.BrokerConnectorConfig{
    24  		Username:     "guest",
    25  		Password:     "guest",
    26  		ServerAddr:   "localhost:61613",
    27  		HeartBeatOut: 30 * time.Second,
    28  		STOMPHeader: map[string]string{
    29  			"access-token": "something",
    30  		},
    31  	})
    32  	if err != nil {
    33  		utils.Log.Fatalln("conn error", err)
    34  	}
    35  
    36  	// send a message to topic named "something.somewhere" after one second
    37  	go func() {
    38  		time.Sleep(1 * time.Second)
    39  		broker.SendMessage("/topic/something.somewhere", "text/plain", []byte("i can send too!"))
    40  	}()
    41  
    42  	// subscribe to topic named "something.somewhere"
    43  	subs, err := broker.Subscribe("/topic/something.somewhere")
    44  	if err != nil {
    45  		utils.Log.Fatalln(err)
    46  	}
    47  
    48  	// get the message channel from the subscription and print out messages as soon as they arrive on the channel
    49  	c := subs.GetMsgChannel()
    50  	go func() {
    51  		for msg := range c {
    52  			utils.Log.Infoln(msg)
    53  		}
    54  	}()
    55  
    56  	utils.Log.Infoln("waiting for messages")
    57  	<-c2
    58  }