-
Notifications
You must be signed in to change notification settings - Fork 72
/
consumer.py
51 lines (38 loc) · 1.25 KB
/
consumer.py
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
import qbus
import signal
import sys
class Callback(qbus.QbusConsumerCallback):
def __init__(self):
qbus.QbusConsumerCallback.__init__(self)
def setConsumer(self, consumer):
self.consumer = consumer
def deliveryMsg(self, topic, msg, msg_len):
print "Topic: %s | msg: %s" % (topic, msg)
def deliveryMsgForCommitOffset(self, info):
print "User commit offset | Topic: %s | msg: %s" % (info.topic, info.msg)
self.consumer.CommitOffset(info)
if len(sys.argv) < 5:
print "Usage: ./consumer config_path topic_name group_name cluster_name"
sys.exit(1)
config_path = sys.argv[1]
topic_name = sys.argv[2]
group = sys.argv[3]
cluster_name = sys.argv[4]
print "topic: %s | group: %s | cluster: %s" % (topic_name, group, cluster_name)
callback = Callback().__disown__()
consumer = qbus.QbusConsumer()
callback.setConsumer(consumer)
if False == consumer.init(cluster_name, "consumer.log", config_path, callback):
print "Init failed"
sys.exit(2)
if False == consumer.subscribeOne(group, topic_name):
print "SubscribeOne failed"
sys.exit(3)
if False == consumer.start():
print "Start failed"
sys.exit(4)
try:
signal.pause()
except KeyboardInterrupt:
consumer.stop()
print "done"