| 3 | import pulsar,time,sys |
| 4 | |
| 5 | class Consumer: |
| 6 | |
| 7 | def __init__(self, service_url, topic, token, consumer): |
| 8 | self.client = pulsar.Client(service_url, authentication=pulsar.AuthenticationToken(token)) |
| 9 | self.consumer = self.client.subscribe(topic, consumer) |
| 10 | |
| 11 | def consume(self): |
| 12 | waitingForMsg = True |
| 13 | while waitingForMsg: |
| 14 | try: |
| 15 | msg = self.consumer.receive(2000) |
| 16 | print("Received message '{}' id='{}'".format(msg.data(), msg.message_id())) |
| 17 | self.consumer.acknowledge(msg) |
| 18 | |
| 19 | # waitingForMsg = False |
| 20 | except: |
| 21 | time.sleep(1) |
| 22 | continue |
| 23 | |
| 24 | |
| 25 | def __del__(self): |
| 26 | self.client.close() |
| 27 | |
| 28 | |
| 29 | |