def on_event_data(self, message): """ Process one event data. """ self.last_offset = EventData.offset(message) self.last_sn = EventData.sequence_number(message) # message.properties (dict) - application defined properties # message.body (object) - application set object self.total += 1 if self.total % 50 == 0: # do checkpoint for every 50 events received logging.info("Received %s, sn=%d offset=%s", self.total, self.last_sn, self.last_offset)
def on_message(self, event): """ Handle message received event """ event_data = EventData.create(event.message) self.offset = event_data.offset waiter = None with self.lock: self.messages.put(event_data) self.credit -= 1 self._check_flow() if self.credit == 0: # workaround before having an EventInjector event.reactor.schedule(0.1, self) if self.waiter is None: return waiter = self.waiter self.waiter = None self.loop.call_soon_threadsafe(waiter.set_result, None)
def on_subscription_data(self, data): ADDRESS = ("amqps://" "send" ":" "RJQLhw9Uu8qHL0KZQkb0dd+4didTvGqIzJZljtP5m/s=" "@" "structstreaming.servicebus.windows.net" "/" "twitter") sender = Sender() azClient = EventHubClient(ADDRESS if len(sys.argv) == 1 else sys.argv[1]) \ .publish(sender) \ .run_daemon() for message in data['messages']: sender.send(EventData(str(message))) print("Got message:", message)
async def send(snd, count): for i in range(count): await snd.send(EventData(str(i))) logger.info("Send message %d", i)
import sys import logging from eventhubs import EventHubClient, Sender, EventData import examples logger = examples.get_logger(logging.INFO) try: ADDRESS = ("amqps://" "<SAS-policy>" ":" "<SAS-key>" "@" "<mynamespace>.servicebus.windows.net" "/" "myeventhub") sender = Sender() client = EventHubClient(ADDRESS if len(sys.argv) == 1 else sys.argv[1]) \ .publish(sender) \ .run_daemon() for i in range(100): sender.send(EventData(str(i))) logger.info("Send message %d", i) client.stop() except KeyboardInterrupt: pass
def start_send(self): self._sender.transfer(EventData("B" * args.payload), self.end_send)
self._success += 1 if self._total % 500 == 0: logger.info("Send total %d, success %d, failure %d", self._total, self._success, self._failure) if time.time() < self._deadline: self.start_send() else: self._event.set() try: sender = Sender() client = EventHubClient(args.address).publish(sender).run_daemon() if args.batch > 1: TransferClient(sender).run() else: total = 0 deadline = time.time() + args.duration while time.time() < deadline: try: sender.send(EventData("D" * args.payload)) total += 1 if total % 500 == 0: logger.info("Send total %d", total) except Exception as err: logger.error("Send failed %s", err) client.stop() except KeyboardInterrupt: pass