use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class UnAcknowledgedMessagesTimeoutTest method testAckTimeoutMinValue.
@Test
public void testAckTimeoutMinValue() throws PulsarClientException {
ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
consumerConfig.setReceiverQueueSize(7);
consumerConfig.setSubscriptionType(SubscriptionType.Failover);
try {
consumerConfig.setAckTimeout(999, TimeUnit.MILLISECONDS);
Assert.fail("Exception should have been thrown since the set timeout is less than min timeout.");
} catch (Exception ex) {
// Ok
}
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class UnAcknowledgedMessagesTimeoutTest method testSharedSingleAckedPartitionedTopic.
@Test(timeOut = testTimeout)
public void testSharedSingleAckedPartitionedTopic() throws Exception {
String key = "testSharedSingleAckedPartitionedTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-shared-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 20;
final int numberOfPartitions = 3;
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
// Special step to create partitioned topic
// 1. producer connect
ProducerConfiguration prodConfig = new ProducerConfiguration();
prodConfig.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = pulsarClient.createProducer(topicName, prodConfig);
// 2. Create consumer
ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
consumerConfig.setReceiverQueueSize(100);
consumerConfig.setSubscriptionType(SubscriptionType.Shared);
consumerConfig.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
consumerConfig.setConsumerName("Consumer-1");
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
consumerConfig.setConsumerName("Consumer-2");
Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
MessageId msgId = producer.send(message.getBytes());
log.info("Message produced: {} -- msgId: {}", message, msgId);
}
// 4. Receive messages
int messageCount1 = receiveAllMessage(consumer1, false);
int messageCount2 = receiveAllMessage(consumer2, true);
int ackCount1 = 0;
int ackCount2 = messageCount2;
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2, totalMessages);
// 5. Check if Messages redelivered again
// Since receive is a blocking call hoping that timeout will kick in
Thread.sleep((int) (ackTimeOutMillis * 1.1));
log.info(key + " Timeout should be triggered now");
messageCount1 = receiveAllMessage(consumer1, true);
messageCount2 += receiveAllMessage(consumer2, false);
ackCount1 = messageCount1;
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2, totalMessages);
assertEquals(ackCount1 + messageCount2, totalMessages);
Thread.sleep((int) (ackTimeOutMillis * 1.1));
// Since receive is a blocking call hoping that timeout will kick in
log.info(key + " Timeout should be triggered again");
ackCount1 += receiveAllMessage(consumer1, true);
ackCount2 += receiveAllMessage(consumer2, true);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(ackCount1 + ackCount2, totalMessages);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class UnAcknowledgedMessagesTimeoutTest method testCheckUnAcknowledgedMessageTimer.
@Test(timeOut = testTimeout)
public void testCheckUnAcknowledgedMessageTimer() throws PulsarClientException, InterruptedException {
String key = "testCheckUnAcknowledgedMessageTimer";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 3;
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
// 2. Create consumer
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(7);
conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, conf);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
log.info("Producer produced: " + message);
producer.send(message.getBytes());
}
Thread.sleep((long) (ackTimeOutMillis * 1.1));
for (int i = 0; i < totalMessages - 1; i++) {
Message msg = consumer.receive();
consumer.acknowledge(msg);
}
assertEquals(consumer.getUnAckedMessageTracker().size(), 1);
Message msg = consumer.receive();
consumer.acknowledge(msg);
assertEquals(consumer.getUnAckedMessageTracker().size(), 0);
Thread.sleep((long) (ackTimeOutMillis * 1.1));
assertEquals(consumer.getUnAckedMessageTracker().size(), 0);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class UnAcknowledgedMessagesTimeoutTest method testConfiguration.
@Test()
public void testConfiguration() {
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setAckTimeout(10, TimeUnit.MINUTES);
assertEquals(conf.getAckTimeoutMillis(), 10 * 60 * 1000);
conf.setAckTimeout(11, TimeUnit.SECONDS);
assertEquals(conf.getAckTimeoutMillis(), 11 * 1000);
conf.setAckTimeout(15000000, TimeUnit.MICROSECONDS);
assertEquals(conf.getAckTimeoutMillis(), 15000);
conf.setAckTimeout(17000, TimeUnit.MILLISECONDS);
assertEquals(conf.getAckTimeoutMillis(), 17000);
conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
assertEquals(conf.getAckTimeoutMillis(), ackTimeOutMillis);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class UnAcknowledgedMessagesTimeoutTest method testExclusiveCumulativeAckedNormalTopic.
@Test(timeOut = testTimeout)
public void testExclusiveCumulativeAckedNormalTopic() throws Exception {
String key = "testExclusiveCumulativeAckedNormalTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 10;
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
// 2. Create consumer
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(7);
conf.setAckTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS);
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
producer.send(message.getBytes());
}
// 4. Receiver receives the message
HashSet<String> hSet = new HashSet<>();
Message message = consumer.receive();
Message lastMessage = message;
while (message != null) {
lastMessage = message;
hSet.add(new String(message.getData()));
log.info("Consumer received " + new String(message.getData()));
log.info("Message ID details " + ((MessageIdImpl) message.getMessageId()).toString());
message = consumer.receive(500, TimeUnit.MILLISECONDS);
}
long size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
assertEquals(size, totalMessages);
log.info("Comulative Ack sent for " + new String(lastMessage.getData()));
log.info("Message ID details " + ((MessageIdImpl) lastMessage.getMessageId()).toString());
consumer.acknowledgeCumulative(lastMessage);
size = ((ConsumerImpl) consumer).getUnAckedMessageTracker().size();
assertEquals(size, 0);
message = consumer.receive((int) (2 * ackTimeOutMillis), TimeUnit.MILLISECONDS);
assertEquals(message, null);
}
Aggregations