async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a aiostomp connection, default stomp port in activemq is 61613 stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() # Create a pub sub scope using the configured transport and protocol transport_factory = FStompSubscriberTransportFactory(stomp_client) provider = FScopeProvider(None, transport_factory, prot_factory) subscriber = AlbumWinnersSubscriber(provider) def event_handler(ctx, req): raise ValueError('BLAH') root.info("You won! {}".format(req.ASIN)) def start_contest_handler(ctx, albums): root.info("Contest started, available albums: {}".format(albums)) await subscriber.subscribe_Winner(event_handler) await subscriber.subscribe_ContestStart(start_contest_handler) root.info("Subscriber starting...")
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a aiostomp connection, default stomp port in activemq is 61613 stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() # Create a pub sub scope using the configured transport and protocol transport_factory = FStompPublisherTransportFactory(stomp_client) provider = FScopeProvider(transport_factory, None, prot_factory) # Create a publisher publisher = AlbumWinnersPublisher(provider) await publisher.open() # Publish an album win event album = Album() album.ASIN = str(uuid.uuid4()) album.duration = 12000 album.tracks = [ Track(title="Comme des enfants", artist="Coeur de pirate", publisher="Grosse Boîte", composer="Béatrice Martin", duration=169, pro=PerfRightsOrg.ASCAP) ] await publisher.publish_Winner(FContext(), album) await publisher.publish_ContestStart(FContext(), [album, album]) # Close publisher and nats client await publisher.close() stomp_client.close()
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a NATS connection to receive requests nats_client = NatsClient() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} await nats_client.connect(**options) # Create a pub sub scope using the configured transport and protocol transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(None, transport_factory, prot_factory) subscriber = AlbumWinnersSubscriber(provider) def event_handler(ctx, req): root.info("You won! {}".format(req.ASIN)) def start_contest_handler(ctx, albums): root.info("Contest started, available albums: {}".format(albums)) await subscriber.subscribe_Winner(event_handler) await subscriber.subscribe_ContestStart(start_contest_handler) root.info("Subscriber starting...")
def test_new_provider(self, mock_pub_transport_factory, mock_sub_transport_factory, mock_thrift_protocol): pub_transport = FTransport() sub_transport = FTransport() protocol_factory = FProtocolFactory(None) mock_pub_transport_factory.get_transport.return_value = pub_transport mock_sub_transport_factory.get_transport.return_value = sub_transport provider = FScopeProvider(mock_pub_transport_factory, mock_sub_transport_factory, protocol_factory) trans, prot_factory = provider.new_publisher() self.assertEqual(pub_transport, trans) self.assertEqual(protocol_factory, prot_factory) trans, prot_factory = provider.new_subscriber() self.assertEqual(sub_transport, trans) self.assertEqual(protocol_factory, prot_factory)
async def test_pub_sub(nats_client, protocol_factory, port): global response_received pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) publisher = EventsPublisher(provider) await publisher.open() def subscribe_handler(context, event): print("Response received {}".format(event)) global response_received if context: response_received = True # Subscribe to response preamble = "foo" ramble = "bar" subscriber = EventsSubscriber(provider) await subscriber.subscribe_EventCreated(preamble, ramble, "response", "{}".format(port), subscribe_handler) event = Event(Message="Sending Call") context = FContext("Call") context.set_request_header(PREAMBLE_HEADER, preamble) context.set_request_header(RAMBLE_HEADER, ramble) print("Publishing...") await publisher.publish_EventCreated(context, preamble, ramble, "call", "{}".format(port), event) # Loop with sleep interval. Fail if not received within 3 seconds total_time = 0 interval = 0.1 while total_time < 3: if response_received: break else: await asyncio.sleep(interval) total_time += interval if not response_received: print("Pub/Sub response timed out!") exit(1) await publisher.close() exit(0)
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a NATS connection to receive requests nats_client = NatsClient() options = { "verbose": True, "servers": ["nats://127.0.0.1:4222"] } await nats_client.connect(**options) # Create a pub sub scope using the configured transport and protocol transport_factory = FNatsPublisherTransportFactory(nats_client) provider = FScopeProvider(transport_factory, None, prot_factory) # Create a publisher publisher = AlbumWinnersPublisher(provider) await publisher.open() # Publish an album win event album = Album() album.ASIN = str(uuid.uuid4()) album.duration = 12000 album.tracks = [Track(title="Comme des enfants", artist="Coeur de pirate", publisher="Grosse Boîte", composer="Béatrice Martin", duration=169, pro=PerfRightsOrg.ASCAP)] await publisher.publish_Winner(FContext(), album) await publisher.publish_ContestStart(FContext(), [album, album]) # Close publisher and nats client await publisher.close() await nats_client.close()
def pub_sub(subject, protocol_factory): nats_client = NATS() yield nats_client.connect(**get_nats_options()) # Setup subscriber, send response upon receipt pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) publisher = EventsPublisher(provider) yield publisher.open() @gen.coroutine def response_handler(context, event): print("received {} : {}".format(context, event)) preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == "": logging.error("Client did not provide preamble header") return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == "": logging.error("Client did not provide ramble header") return response_event = Event(Message="Sending Response") response_context = FContext("Call") yield publisher.publish_EventCreated(response_context, preamble, ramble, "response", "{}".format(subject), response_event) print("Published event={}".format(response_event)) publisher.close() subscriber = EventsSubscriber(provider) yield subscriber.subscribe_EventCreated("*", "*", "call", "{}".format(subject), response_handler)
def main(): parser = argparse.ArgumentParser(description="Run a tornado python server") parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default="binary", choices="binary, compact, json") parser.add_argument('--transport', dest="transport_type", default="stateless", choices="stateless, http") args = parser.parse_args() if args.protocol_type == "binary": protocol_factory = FProtocolFactory(TBinaryProtocolFactory()) elif args.protocol_type == "compact": protocol_factory = FProtocolFactory(TCompactProtocolFactory()) elif args.protocol_type == "json": protocol_factory = FProtocolFactory(TJSONProtocolFactory()) else: logging.error("Unknown protocol type: %s", args.protocol_type) sys.exit(1) nats_client = NATS() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} yield nats_client.connect(**options) global port port = args.port handler = FrugalTestHandler() subject = "frugal.*.*.rpc.{}".format(args.port) processor = Processor(handler) if args.transport_type == "stateless": server = FNatsServer(nats_client, [subject], processor, protocol_factory) # start healthcheck so the test runner knows the server is running thread.start_new_thread(healthcheck, (port, )) print("Starting {} server...".format(args.transport_type)) yield server.serve() elif args.transport_type == "http": factories = { 'processor': processor, 'protocol_factory': protocol_factory } server = Application([(r'/', FHttpHandler, factories)]) print("Starting {} server...".format(args.transport_type)) server.listen(port) else: logging.error("Unknown transport type: %s", args.transport_type) sys.exit(1) # Setup subscriber, send response upon receipt pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) global publisher publisher = EventsPublisher(provider) yield publisher.open() @gen.coroutine def response_handler(context, event): print("received {} : {}".format(context, event)) preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == "": logging.error("Client did not provide preamble header") return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == "": logging.error("Client did not provide ramble header") return response_event = Event(Message="Sending Response") response_context = FContext("Call") global publisher global port yield publisher.publish_EventCreated(response_context, preamble, ramble, "response", "{}".format(port), response_event) print("Published event={}".format(response_event)) subscriber = EventsSubscriber(provider) yield subscriber.subscribe_EventCreated("*", "*", "call", "{}".format(args.port), response_handler)
async def main(): parser = argparse.ArgumentParser( description="Run an asyncio python server") parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default="binary", choices="binary, compact, json") parser.add_argument('--transport', dest="transport_type", default=NATS_NAME, choices="nats, http") args = parser.parse_args() protocol_factory = get_protocol_factory(args.protocol_type) nats_client = NatsClient() await nats_client.connect(**get_nats_options()) port = args.port handler = FrugalTestHandler() subject = "frugal.*.*.rpc.{}".format(args.port) processor = Processor(handler) # Setup subscriber, send response upon receipt pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) publisher = EventsPublisher(provider) await publisher.open() async def response_handler(context, event): preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == "": logging.error("Client did not provide preamble header") return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == "": logging.error("Client did not provide ramble header") return response_event = Event(Message="Sending Response") response_context = FContext("Call") await publisher.publish_EventCreated(response_context, preamble, ramble, "response", "{}".format(port), response_event) subscriber = EventsSubscriber(provider) await subscriber.subscribe_EventCreated("*", "*", "call", "{}".format(args.port), response_handler) if args.transport_type == NATS_NAME: server = FNatsServer(nats_client, [subject], processor, protocol_factory) # start healthcheck so the test runner knows the server is running threading.Thread(target=healthcheck, args=(port, )).start() print("Starting {} server...".format(args.transport_type)) await server.serve() elif args.transport_type == HTTP_NAME: print('starting http server') handler = new_http_handler(processor, protocol_factory) app = web.Application(loop=asyncio.get_event_loop()) app.router.add_route("*", "/", handler) srv = await asyncio.get_event_loop().create_server( app.make_handler(), '0.0.0.0', port) else: logging.error("Unknown transport type: %s", args.transport_type) sys.exit(1)
async def main(): global response_received parser = argparse.ArgumentParser( description='Run a python asyncio stomp publisher') parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default='binary', choices='binary, compact, json') parser.add_argument('--transport', dest='transport_type', default=ACTIVEMQ_NAME, choices='activemq') args = parser.parse_args() protocol_factory = get_protocol_factory(args.protocol_type) if args.transport_type == ACTIVEMQ_NAME: stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() pub_transport_factory = FStompPublisherTransportFactory(stomp_client) sub_transport_factory = FStompSubscriberTransportFactory(stomp_client) provider = FScopeProvider( pub_transport_factory, sub_transport_factory, protocol_factory) publisher = EventsPublisher(provider) else: print( 'Unknown transport type: {type}'.format(type=args.transport_type)) sys.exit(1) await publisher.open() def subscribe_handler(context, event): print('Response received {}'.format(event)) global response_received if context: response_received = True # Subscribe to response preamble = 'foo' ramble = 'bar' subscriber = EventsSubscriber(provider) await subscriber.subscribe_EventCreated(preamble, ramble, 'response', '{}'.format(args.port), subscribe_handler) event = Event(Message='Sending Call') context = FContext('Call') context.set_request_header(PREAMBLE_HEADER, preamble) context.set_request_header(RAMBLE_HEADER, ramble) print('Publishing...') await publisher.publish_EventCreated(context, preamble, ramble, 'call', '{}'.format(args.port), event) # Loop with sleep interval. Fail if not received within 3 seconds total_time = 0 interval = 0.1 while total_time < 3: if response_received: break else: await asyncio.sleep(interval) total_time += interval if not response_received: print('publisher did not get a response from subscriber!') exit(1) await publisher.close() exit(0)
async def main(): global message_received parser = argparse.ArgumentParser( description='Run a python asyncio stomp publisher') parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default='binary', choices="binary, compact, json") parser.add_argument('--transport', dest='transport_type', default=ACTIVEMQ_NAME, choices='activemq') args = parser.parse_args() protocol_factory = get_protocol_factory(args.protocol_type) if args.transport_type == ACTIVEMQ_NAME: stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() pub_transport_factory = FStompPublisherTransportFactory(stomp_client) sub_transport_factory = FStompSubscriberTransportFactory(stomp_client) else: print( 'Unknown transport type: {type}'.format(type=args.transport_type)) sys.exit(1) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) # start healthcheck so the test runner knows the server is running threading.Thread(target=healthcheck, args=(args.port, )).start() async def subscribe_handler(context, event): publisher = EventsPublisher(provider) try: await publisher.open() preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == '': print('Client did not provide preamble header') return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == '': print('Client did not provide ramble header') return response_event = Event(Message='Sending Response') response_context = FContext('Call') await publisher.publish_EventCreated(response_context, preamble, ramble, 'response', '{}'.format(args.port), response_event) global message_received message_received = True except Exception as e: print('Error opening publisher to respond:' + repr(e)) subscriber = EventsSubscriber(provider) await subscriber.subscribe_EventCreated('*', '*', 'call', '{}'.format(args.port), subscribe_handler) # Loop with sleep interval. Fail if not received within 3 seconds total_time = 0 interval = 0.1 while total_time < 3: if message_received: break else: await asyncio.sleep(interval) total_time += interval if not message_received: print('subscriber did not received message!') exit(1) exit(0)