def __init__(self, url=None, token=None): if not isinstance(url, str) or not isinstance(token, str): assert 0 from pulsar import AuthenticationToken self._client = pulsar.Client(url, authentication=AuthenticationToken(token)) self._client_id = generate_client_id()
def test_token_auth_supplier(self): def read_token(): with open('/tmp/pulsar-test-data/tokens/token.txt') as tf: return tf.read().strip() client = Client(self.serviceUrl, authentication=AuthenticationToken(read_token)) consumer = client.subscribe('persistent://private/auth/my-python-topic-token-auth', 'my-sub', consumer_type=ConsumerType.Shared) producer = client.create_producer('persistent://private/auth/my-python-topic-token-auth') producer.send(b'hello') msg = consumer.receive(1000) self.assertTrue(msg) self.assertEqual(msg.data(), b'hello') client.close()
def test_token_auth(self): with open('/tmp/pulsar-test-data/tokens/token.txt') as tf: token = tf.read().strip() # Use adminUrl to test both HTTP request and binary protocol client = Client(self.adminUrl, authentication=AuthenticationToken(token)) consumer = client.subscribe('persistent://private/auth/my-python-topic-token-auth', 'my-sub', consumer_type=ConsumerType.Shared) producer = client.create_producer('persistent://private/auth/my-python-topic-token-auth') producer.send(b'hello') msg = consumer.receive(1000) self.assertTrue(msg) self.assertEqual(msg.data(), b'hello') client.close()
def produce_message(): # Setup Pulsar Client try: token = request.headers['Authorization'] client = Client(broker_url, authentication=AuthenticationToken(token)) except Exception as e: return 'UNAUTHORIZED' # Create Producer and send Message try: producer = client.create_producer(topic) payload = request.get_json() message = payload['message'] producer.send(message.encode('utf-8')) return 'OK' except Exception as e: return 'BAD REQUEST'
def redeliver_message(self): client = pulsar.Client( service_url=self._url, authentication=AuthenticationToken(token=self._token)) consumer = client.subscribe(topic=self._topics, subscription_name=self._subscription_name, schema=AvroSchema(MilvusRecord), consumer_type=ConsumerType.KeyShared) delete_producers = [] insert_producers = [] for topic in self._topics: d_and_u_topic = topic + "-delete" delete_producers.append( client.create_producer(topic=d_and_u_topic, schema=AvroSchema(MilvusRecord))) i_and_q_topic = topic + "-insert" insert_producers.append( client.create_producer(topic=i_and_q_topic, schema=AvroSchema(MilvusRecord))) print("Service start successful !!!") while True: msg = consumer.receive() message_topic_name = msg.topic_name() for i, topic in enumerate(self._topics): if message_topic_name.find(topic) != -1: print(i) if msg.value().op in [Op.insert, Op.query]: insert_producers[i].send( content=msg.value(), event_timestamp=int(time.time() * 1000), partition_key=str(msg.value().id)) else: delete_producers[i].send( content=msg.value(), event_timestamp=int(time.time() * 1000), partition_key=str(msg.value().id)) break consumer.acknowledge(msg)
def consume(topic): url = 'pulsar://localhost:6650' token = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.8oAwbbd8dd3ZYhCWKAiShP4Kd0nHSwvQbTMX7Iat_o0' client = pulsar.Client(service_url=url, authentication=AuthenticationToken(token=token)) consumer = client.subscribe(topic=topic, subscription_name=topic, schema=AvroSchema(MilvusRecord), consumer_type=ConsumerType.Shared) while True: msg = consumer.receive() ex = msg.value() try: print("Received message id={} op={} topic_name={}".format( ex.id, ex.op, msg.topic_name())) # Acknowledge successful processing of the message consumer.acknowledge(msg) except: # Message failed to be processed consumer.negative_acknowledge(msg)
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. import os from pulsar import Client, AuthenticationToken client = Client(os.environ.get('SERVICE_URL'), authentication=AuthenticationToken( os.environ.get('AUTH_PARAMS'))) client.close()
# distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from pulsar import Client, AuthenticationToken client = Client("SERVICE_URL", authentication=AuthenticationToken("AUTH_PARAMS")) consumer = client.subscribe('my-topic', 'my-subscription') while True: msg = consumer.receive() try: print("Received message '{}' id='{}'".format(msg.data(), msg.message_id())) # Acknowledge successful processing of the message consumer.acknowledge(msg) except: # Message failed to be processed consumer.negative_acknowledge(msg)
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from pulsar import Client, AuthenticationToken client = Client("SERVICE_URL", authentication=AuthenticationToken("AUTH_PARAMS")) client.close()