Search in sources :

Example 96 with Consumer

use of com.yahoo.pulsar.client.api.Consumer in project pulsar by yahoo.

the class ZeroQueueSizeTest method zeroQueueSizeReceieveAsyncInCompatibility.

@Test(expectedExceptions = PulsarClientException.InvalidConfigurationException.class)
public void zeroQueueSizeReceieveAsyncInCompatibility() throws PulsarClientException {
    String key = "zeroQueueSizeReceieveAsyncInCompatibility";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    ConsumerConfiguration configuration = new ConsumerConfiguration();
    configuration.setReceiverQueueSize(0);
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, configuration);
    consumer.receive(10, TimeUnit.SECONDS);
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 97 with Consumer

use of com.yahoo.pulsar.client.api.Consumer in project pulsar by yahoo.

the class SampleConsumer method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException {
    PulsarClient pulsarClient = PulsarClient.create("http://localhost:8080");
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic", "my-subscriber-name");
    Message msg = null;
    for (int i = 0; i < 100; i++) {
        msg = consumer.receive();
        // do something
        System.out.println("Received: " + new String(msg.getData()));
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledge(msg);
    pulsarClient.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient)

Example 98 with Consumer

use of com.yahoo.pulsar.client.api.Consumer in project pulsar by yahoo.

the class SampleConsumerListener method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://localhost:8080");
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setMessageListener(new MessageListener() {

        public void received(Consumer consumer, Message msg) {
            log.info("Received message: {}", msg);
            consumer.acknowledgeAsync(msg);
        }
    });
    pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic", "my-subscriber-name", conf);
    // Block main thread
    System.in.read();
    pulsarClient.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) MessageListener(com.yahoo.pulsar.client.api.MessageListener) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient)

Example 99 with Consumer

use of com.yahoo.pulsar.client.api.Consumer in project pulsar by yahoo.

the class PartitionedConsumerImpl method closeAsync.

@Override
public CompletableFuture<Void> closeAsync() {
    if (getState() == State.Closing || getState() == State.Closed) {
        return CompletableFuture.completedFuture(null);
    }
    setState(State.Closing);
    AtomicReference<Throwable> closeFail = new AtomicReference<Throwable>();
    AtomicInteger completed = new AtomicInteger(numPartitions);
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    for (Consumer consumer : consumers) {
        if (consumer != null) {
            consumer.closeAsync().handle((closed, ex) -> {
                if (ex != null) {
                    closeFail.compareAndSet(null, ex);
                }
                if (completed.decrementAndGet() == 0) {
                    if (closeFail.get() == null) {
                        setState(State.Closed);
                        closeFuture.complete(null);
                        log.info("[{}] [{}] Closed Partitioned Consumer", topic, subscription);
                        client.cleanupConsumer(this);
                    } else {
                        setState(State.Failed);
                        closeFuture.completeExceptionally(closeFail.get());
                        log.error("[{}] [{}] Could not close Partitioned Consumer", topic, subscription, closeFail.get().getCause());
                    }
                }
                return null;
            });
        }
    }
    return closeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Consumer(com.yahoo.pulsar.client.api.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 100 with Consumer

use of com.yahoo.pulsar.client.api.Consumer in project pulsar by yahoo.

the class PartitionedConsumerImpl method start.

private void start() {
    AtomicReference<Throwable> subscribeFail = new AtomicReference<Throwable>();
    AtomicInteger completed = new AtomicInteger();
    ConsumerConfiguration internalConfig = getInternalConsumerConfig();
    for (int partitionIndex = 0; partitionIndex < numPartitions; partitionIndex++) {
        String partitionName = DestinationName.get(topic).getPartition(partitionIndex).toString();
        ConsumerImpl consumer = new ConsumerImpl(client, partitionName, subscription, internalConfig, client.externalExecutorProvider().getExecutor(), partitionIndex, new CompletableFuture<Consumer>());
        consumers.add(consumer);
        consumer.subscribeFuture().handle((cons, subscribeException) -> {
            if (subscribeException != null) {
                setState(State.Failed);
                subscribeFail.compareAndSet(null, subscribeException);
                client.cleanupConsumer(this);
            }
            if (completed.incrementAndGet() == numPartitions) {
                if (subscribeFail.get() == null) {
                    try {
                        // We have successfully created N consumers, so we can start receiving messages now
                        starReceivingMessages();
                        setState(State.Ready);
                        subscribeFuture().complete(PartitionedConsumerImpl.this);
                        log.info("[{}] [{}] Created partitioned consumer", topic, subscription);
                        return null;
                    } catch (PulsarClientException e) {
                        subscribeFail.set(e);
                    }
                }
                closeAsync().handle((ok, closeException) -> {
                    subscribeFuture().completeExceptionally(subscribeFail.get());
                    client.cleanupConsumer(this);
                    return null;
                });
                log.error("[{}] [{}] Could not create partitioned consumer.", topic, subscription, subscribeFail.get().getCause());
            }
            return null;
        });
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Consumer (com.yahoo.pulsar.client.api.Consumer)109 Test (org.testng.annotations.Test)99 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)75 Producer (com.yahoo.pulsar.client.api.Producer)71 Message (com.yahoo.pulsar.client.api.Message)62 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)34 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)34 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)33 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)26 CompletableFuture (java.util.concurrent.CompletableFuture)21 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)20 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)14 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)13 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)13 HashSet (java.util.HashSet)12 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)11 MessageId (com.yahoo.pulsar.client.api.MessageId)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)7