Ejemplo n.º 1
0
// Send messages to a test queue for 10 seconds.
func main() {

	// Create a stomp dozer instance for a queue named "test"
	dz := dozer.Queue("test").WithProtocol("stomp").WithMessageType("text/plain")
	err := dz.Connect("localhost", 61613)
	if err != nil {
		log.Fatal(err)
	}

	// Helper channels
	messages, timeout, quit := make(chan []byte), make(chan bool), make(chan bool)

	// Start timer
	go func() {
		<-time.After(10 * time.Second)
		log.Println("Timeout reached")
		timeout <- true
	}()

	// Start sending messages
	go sendWorker(messages, timeout, quit)
	if err := dz.SendLoop(messages, quit); err != nil {
		log.Println(err)
	}

	// Cleanup
	close(messages)
	close(timeout)
	close(quit)
}
Ejemplo n.º 2
0
// Consume messages from a test queue and forward them to a ZeroMQ socket.
func main() {

	// Create a dozer queue instance
	var err error
	queue := dozer.Queue("test").WithProtocol("stomp")
	err = queue.Connect("localhost", 61613)
	if err != nil {
		log.Fatal(err)
	}

	// Create a dozer ZeroMQ socket instance
	socket := dozer.Socket("push").WithProtocol("zmq4")
	err = socket.Bind("*", 5555) // Bind to all interfaces
	if err != nil {
		log.Fatal(err)
	}

	// Helper channels
	pipe, interrupted := make(chan []byte), make(chan bool)

	// Listen for [ctrl-c] interrupt signal
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)
	go func() {
		<-interrupt
		signal.Stop(interrupt)
		interrupted <- true // Send signal to both loops
		interrupted <- true
	}()

	// Start forwarding messages to socket
	go func() {
		if err := socket.SendLoop(pipe, interrupted); err != nil {
			log.Fatal(err)
		}
	}()

	// Start receiving messages from queue
	log.Println("Pump started; enter [ctrl-c] to quit.")
	if err := queue.RecvLoop(pipe, interrupted); err != nil {
		log.Fatal(err)
	}

	// Cleanup
	close(pipe)
	close(interrupted)
}