예제 #1
0
    def test_seek(self):
        client = Client(self.serviceUrl)
        consumer = client.subscribe('my-python-topic-seek',
                                    'my-sub',
                                    consumer_type=ConsumerType.Shared)
        producer = client.create_producer('my-python-topic-seek')

        for i in range(100):
            if i > 0:
                time.sleep(0.02)
            producer.send(b'hello-%d' % i)

        ids = []
        timestamps = []
        for i in range(100):
            msg = consumer.receive(TM)
            self.assertEqual(msg.data(), b'hello-%d' % i)
            ids.append(msg.message_id())
            timestamps.append(msg.publish_timestamp())
            consumer.acknowledge(msg)

        # seek, and after reconnect, expected receive first message.
        consumer.seek(MessageId.earliest)
        time.sleep(0.5)
        msg = consumer.receive(TM)
        self.assertEqual(msg.data(), b'hello-0')

        # seek on messageId
        consumer.seek(ids[50])
        time.sleep(0.5)
        msg = consumer.receive(TM)
        self.assertEqual(msg.data(), b'hello-50')

        # ditto, but seek on timestamp
        consumer.seek(timestamps[42])
        time.sleep(0.5)
        msg = consumer.receive(TM)
        self.assertEqual(msg.data(), b'hello-42')

        # repeat with reader
        reader = client.create_reader('my-python-topic-seek', MessageId.latest)
        with self.assertRaises(pulsar.Timeout):
            reader.read_next(100)

        # earliest
        reader.seek(MessageId.earliest)
        time.sleep(0.5)
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-0')
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-1')

        # seek on messageId
        reader.seek(ids[33])
        time.sleep(0.5)
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-33')
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-34')

        # seek on timestamp
        reader.seek(timestamps[79])
        time.sleep(0.5)
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-79')
        msg = reader.read_next(TM)
        self.assertEqual(msg.data(), b'hello-80')

        reader.close()
        client.close()
예제 #2
0
 def test_simple_producer(self):
     client = Client(self.serviceUrl)
     producer = client.create_producer('my-python-topic')
     producer.send(b'hello')
     producer.close()
     client.close()
예제 #3
0
 def test_producer_routing_mode(self):
     client = Client(self.serviceUrl)
     producer = client.create_producer('my-python-test-producer',
                                       message_routing_mode=PartitionsRoutingMode.UseSinglePartition)
     producer.send(b'test')
     client.close()
예제 #4
0
    def test_publish_compact_and_consume(self):
        client = Client(self.serviceUrl)
        topic = 'compaction_%s' % (uuid.uuid4())
        producer = client.create_producer(topic, producer_name='my-producer-name', batching_enabled=False)
        self.assertEqual(producer.last_sequence_id(), -1)
        consumer = client.subscribe(topic, 'my-sub1', is_read_compacted=True)
        consumer.close()
        consumer2 = client.subscribe(topic, 'my-sub2', is_read_compacted=False)

        # producer create 2 messages with same key.
        producer.send(b'hello-0', partition_key='key0')
        producer.send(b'hello-1', partition_key='key0')
        producer.close()

        # issue compact command, and wait success
        url='%s/admin/v2/persistent/public/default/%s/compaction' % (self.adminUrl, topic)
        doHttpPut(url, '')
        while True:
            s=doHttpGet(url).decode('utf-8')
            if 'RUNNING' in s:
                print(s)
                print("Compact still running")
                time.sleep(0.2)
            else:
                print(s)
                print("Compact Complete now")
                self.assertTrue('SUCCESS' in s)
                break

        # after compaction completes the compacted ledger is recorded
        # as a property of a cursor. As persisting the cursor is async
        # and we don't wait for the acknowledgement of the acknowledgement,
        # there may be a race if we try to read the compacted ledger immediately.
        # therefore wait a second to allow the compacted ledger to be updated on
        # the broker.
        time.sleep(1.0)

        # after compact, consumer with `is_read_compacted=True`, expected read only the second message for same key.
        consumer1 = client.subscribe(topic, 'my-sub1', is_read_compacted=True)
        msg0 = consumer1.receive(TM)
        self.assertEqual(msg0.data(), b'hello-1')
        consumer1.acknowledge(msg0)
        consumer1.close()

        # ditto for reader
        reader1 = client.create_reader(topic, MessageId.earliest, is_read_compacted=True)
        msg0 = reader1.read_next(TM)
        self.assertEqual(msg0.data(), b'hello-1')
        reader1.close()

        # after compact, consumer with `is_read_compacted=False`, expected read 2 messages for same key.
        msg0 = consumer2.receive(TM)
        self.assertEqual(msg0.data(), b'hello-0')
        consumer2.acknowledge(msg0)
        msg1 = consumer2.receive(TM)
        self.assertEqual(msg1.data(), b'hello-1')
        consumer2.acknowledge(msg1)
        consumer2.close()

        # ditto for reader
        reader2 = client.create_reader(topic, MessageId.earliest, is_read_compacted=False)
        msg0 = reader2.read_next(TM)
        self.assertEqual(msg0.data(), b'hello-0')
        msg1 = reader2.read_next(TM)
        self.assertEqual(msg1.data(), b'hello-1')
        reader2.close()
        client.close()
예제 #5
0
 def test_client_logger(self):
     logger = logging.getLogger("pulsar")
     Client(self.serviceUrl, logger=logger)
예제 #6
0
 def test_connect_error(self):
     with self.assertRaises(pulsar.ConnectError):
         client = Client('fakeServiceUrl')
         client.create_producer('connect-error-topic')
         client.close()
예제 #7
0
 def __init__(self, args):
     self.args = args
     self.client = Client(args.service_url,
                          authentication=AuthenticationOauth2(
                              args.auth_params))
예제 #8
0
 def get_producer():
     cl = Client(self.serviceUrl)
     return cl.create_producer(topic="foobar")
예제 #9
0
 def test_simple_producer(self):
     client = Client(self.serviceUrl)
     producer = client.create_producer('persistent://sample/standalone/ns/my-python-topic')
     producer.send('hello')
     producer.close()
     client.close()
예제 #10
0
#  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')))

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)
예제 #11
0
#  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)