MCPcopy
hub / github.com/IBM/sarama / ExampleAsyncProducer_select

Function ExampleAsyncProducer_select

async_producer_test.go:2840–2871  ·  view source on GitHub ↗

This example shows how to use the producer while simultaneously reading the Errors channel to know about any failures.

()

Source from the content-addressed store, hash-verified

2838// This example shows how to use the producer while simultaneously
2839// reading the Errors channel to know about any failures.
2840func ExampleAsyncProducer_select() {
2841 producer, err := NewAsyncProducer([]string{"localhost:9092"}, nil)
2842 if err != nil {
2843 panic(err)
2844 }
2845
2846 defer func() {
2847 if err := producer.Close(); err != nil {
2848 log.Fatalln(err)
2849 }
2850 }()
2851
2852 // Trap SIGINT to trigger a shutdown.
2853 signals := make(chan os.Signal, 1)
2854 signal.Notify(signals, os.Interrupt)
2855
2856 var enqueued, producerErrors int
2857ProducerLoop:
2858 for {
2859 select {
2860 case producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder("testing 123")}:
2861 enqueued++
2862 case err := <-producer.Errors():
2863 log.Println("Failed to produce message", err)
2864 producerErrors++
2865 case <-signals:
2866 break ProducerLoop
2867 }
2868 }
2869
2870 log.Printf("Enqueued: %d; errors: %d\n", enqueued, producerErrors)
2871}
2872
2873// This example shows how to use the producer with separate goroutines
2874// reading from the Successes and Errors channels. Note that in order

Callers

nothing calls this directly

Calls 7

CloseMethod · 0.95
InputMethod · 0.95
ErrorsMethod · 0.95
StringEncoderTypeAlias · 0.85
NewAsyncProducerFunction · 0.70
PrintlnMethod · 0.65
PrintfMethod · 0.65

Tested by

no test coverage detected