use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class CmdConsume method run.
/**
* Run the consume command.
*
* @return 0 for success, < 0 otherwise
*/
public int run() throws PulsarClientException, IOException {
if (mainOptions.size() != 1)
throw (new ParameterException("Please provide one and only one topic name."));
if (this.serviceURL == null || this.serviceURL.isEmpty())
throw (new ParameterException("Broker URL is not provided."));
if (this.subscriptionName == null || this.subscriptionName.isEmpty())
throw (new ParameterException("Subscription name is not provided."));
if (this.numMessagesToConsume < 0)
throw (new ParameterException("Number of messages should be zero or positive."));
String topic = this.mainOptions.get(0);
int numMessagesConsumed = 0;
int returnCode = 0;
try {
ConsumerConfiguration consumerConf = new ConsumerConfiguration();
consumerConf.setSubscriptionType(this.subscriptionType);
PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig);
Consumer consumer = client.subscribe(topic, this.subscriptionName, consumerConf);
RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null;
while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) {
if (limiter != null) {
limiter.acquire();
}
Message msg = consumer.receive(5, TimeUnit.SECONDS);
if (msg == null) {
LOG.warn("No message to consume after waiting for 20 seconds.");
} else {
numMessagesConsumed += 1;
System.out.println(MESSAGE_BOUNDARY);
String output = this.interpretMessage(msg, displayHex);
System.out.println(output);
consumer.acknowledge(msg);
}
}
client.close();
} catch (Exception e) {
LOG.error("Error while consuming messages");
LOG.error(e.getMessage(), e);
returnCode = -1;
} finally {
LOG.info("{} messages successfully consumed", numMessagesConsumed);
}
return returnCode;
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class ZeroQueueSizeTest method validQueueSizeConfig.
@Test
public void validQueueSizeConfig() {
try {
ConsumerConfiguration configuration = new ConsumerConfiguration();
configuration.setReceiverQueueSize(0);
} catch (Exception ex) {
Assert.fail();
}
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class ZeroQueueSizeTest method zeroQueueSizeNormalConsumer.
@Test()
public void zeroQueueSizeNormalConsumer() throws PulsarClientException {
String key = "nonZeroQueueSizeNormalConsumer";
// 1. Config
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
// 2. Create Producer
Producer producer = pulsarClient.createProducer(topicName);
// 3. Create Consumer
ConsumerConfiguration configuration = new ConsumerConfiguration();
configuration.setReceiverQueueSize(0);
ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, configuration);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
log.info("Producer produced: " + message);
producer.send(message.getBytes());
}
// 4. Receiver receives the message
Message message;
for (int i = 0; i < totalMessages; i++) {
assertEquals(consumer.numMessagesInQueue(), 0);
message = consumer.receive();
assertEquals(new String(message.getData()), messagePredicate + i);
assertEquals(consumer.numMessagesInQueue(), 0);
log.info("Consumer received : " + new String(message.getData()));
}
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class ZeroQueueSizeTest method zeroQueueSizeSharedSubscription.
@Test()
public void zeroQueueSizeSharedSubscription() throws PulsarClientException {
String key = "zeroQueueSizeSharedSubscription";
// 1. Config
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
// 2. Create Producer
Producer producer = pulsarClient.createProducer(topicName);
// 3. Create Consumer
int numOfSubscribers = 4;
ConsumerConfiguration configuration = new ConsumerConfiguration();
configuration.setReceiverQueueSize(0);
configuration.setSubscriptionType(SubscriptionType.Shared);
ConsumerImpl[] consumers = new ConsumerImpl[numOfSubscribers];
for (int i = 0; i < numOfSubscribers; i++) {
consumers[i] = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, configuration);
}
// 4. Produce Messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
producer.send(message.getBytes());
}
// 5. Consume messages
Message message;
for (int i = 0; i < totalMessages; i++) {
assertEquals(consumers[i % numOfSubscribers].numMessagesInQueue(), 0);
message = consumers[i % numOfSubscribers].receive();
assertEquals(new String(message.getData()), messagePredicate + i);
assertEquals(consumers[i % numOfSubscribers].numMessagesInQueue(), 0);
log.info("Consumer received : " + new String(message.getData()));
}
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class ZeroQueueSizeTest method InvalidQueueSizeConfig.
@Test(expectedExceptions = IllegalArgumentException.class)
public void InvalidQueueSizeConfig() {
ConsumerConfiguration configuration = new ConsumerConfiguration();
configuration.setReceiverQueueSize(-1);
}
Aggregations