-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstreaming.go
60 lines (49 loc) · 1.22 KB
/
streaming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package streaming
import (
"os"
"os/signal"
"github.com/Shopify/sarama"
cluster "github.com/bsm/sarama-cluster"
)
//Streaming config
type Streaming struct {
SeedBrokers []string
Config *cluster.Config
InTopic []string
OutTopic []string
GroupID string
CallBack CallBack
Logger sarama.StdLogger
signals chan os.Signal
consumer *cluster.Consumer
producer sarama.AsyncProducer
}
//NewStreaming New Streaming
func NewStreaming(seedbrokers, inTopic, outTopic []string, groupID string, config *cluster.Config, cb CallBack) (err error) {
s := &Streaming{
Config: config,
SeedBrokers: seedbrokers,
InTopic: inTopic,
OutTopic: outTopic,
GroupID: groupID,
CallBack: cb,
signals: make(chan os.Signal, 1),
Logger: sarama.Logger,
}
return NewWithConfig(s)
}
//NewWithConfig New With Config
func NewWithConfig(s *Streaming) (err error) {
s.signals = make(chan os.Signal, 1)
signal.Notify(s.signals, os.Interrupt)
s.consumer, err = cluster.NewConsumer(s.SeedBrokers, s.GroupID, s.InTopic, s.Config)
if err != nil {
return err
}
s.producer, err = sarama.NewAsyncProducer(s.SeedBrokers, &s.Config.Config)
if err != nil {
return err
}
s.processor()
return nil
}