Exemple #1
0
// sends file & exits
func SendFile(msgFile string) {

	broker := kafka.NewBrokerPublisher(hostname, topic, partition)

	fmt.Println("Publishing File:", msgFile)
	file, err := os.Open(msgFile)
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	stat, err := file.Stat()
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	payload := make([]byte, stat.Size())
	file.Read(payload)
	timing := kafka.StartTiming("Sending")

	if compress {
		broker.Publish(kafka.NewCompressedMessage(payload))
	} else {
		broker.Publish(kafka.NewMessage(payload))
	}

	timing.Print()
	file.Close()
}
Exemple #2
0
// sends single message and exits
func SendMessage() {

	broker := kafka.NewBrokerPublisher(hostname, topic, partition)

	fmt.Println("Publishing :", message)
	broker.Publish(kafka.NewMessage([]byte(message)))
}
Exemple #3
0
// sends x copies of a message
func SendManyMessages() {

	broker := kafka.NewBrokerPublisher(hostname, topic, partition)
	timing := kafka.StartTiming("Sending")

	fmt.Println("Publishing :", message, ": Will send ", sendCt, " times")
	done := make(chan bool)
	msgChan := make(chan *kafka.MessageTopic, 1000)

	go broker.PublishOnChannel(msgChan, 100, 100, done)
	for i := 0; i < sendCt; i++ {
		msgChan <- MakeMsg([]byte(message))
	}
	done <- true // force flush

	timing.Print()
}
Exemple #4
0
// sends messages from stdin
func StdinPruducer() {

	broker := kafka.NewBrokerPublisher(hostname, topic, partition)
	b := bufio.NewReader(os.Stdin)
	done := make(chan bool)
	msgChan := make(chan *kafka.MessageTopic, 1000)

	go broker.PublishOnChannel(msgChan, 2000, 200, done)
	fmt.Println("reading from stdin")
	for {
		if s, e := b.ReadString('\n'); e == nil {

			fmt.Println("sending ---", s, []byte(s))

			msgChan <- MakeMsg([]byte(s)[:len(s)-1])

		}
	}
}