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);
}
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();
}
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();
}
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;
}
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;
});
}
}
Aggregations