예제 #1
0
파일: main.go 프로젝트: paulCodes/dozer
// Send messages to a ZeroMQ socket for 10 seconds.
func main() {

	// Create a dozer ZeroMQ socket instance
	dz := dozer.Socket("push").WithProtocol("zmq4")
	err := dz.Bind("*", 5555) // Bind to all interfaces
	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)
}
예제 #2
0
파일: main.go 프로젝트: paulCodes/dozer
// Consume messages from a ZeroMQ socket.
func main() {

	// Create a dozer ZeroMQ socket instance
	dz := dozer.Socket("pull").WithProtocol("zmq4")
	err := dz.Connect("localhost", 5555)
	if err != nil {
		log.Fatal(err)
	}

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

	// Dedicate a majority of CPUs to message processing.
	workers := runtime.NumCPU()/2 + 1
	wg.Add(workers)
	for i := 1; i <= workers; i++ {
		go messageHandler(i, messages, quit)
	}

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

	// Start receiving messages
	log.Println("Receiving messages; hit [ctrl-c] to quit.")
	if err := dz.RecvLoop(messages, interrupted); err != nil {
		log.Println(err)
	}

	// Shut down workers
	for i := 1; i <= workers; i++ {
		log.Printf("Sending quit signal to worker %d\n", i)
		quit <- true
	}

	// Cleanup
	close(messages)
	close(interrupt)
	close(quit)

	// Wait until all workers have completed
	wg.Wait()
}
예제 #3
0
파일: main.go 프로젝트: paulCodes/dozer
// 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)
}