use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BatchMessageTest method testSimpleBatchSyncProducerWithFixedBatchSize.
@Test
public void testSimpleBatchSyncProducerWithFixedBatchSize() throws Exception {
int numMsgs = 10;
int numMsgsInBatch = numMsgs / 2;
final String topicName = "persistent://prop/use/ns-abc/testSimpleBatchSyncProducerWithFixedBatchSize";
final String subscriptionName = "syncsub-1";
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
consumer.close();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxPublishDelay(1000, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(numMsgsInBatch);
producerConf.setBatchingEnabled(true);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
for (int i = 0; i < numMsgs; i++) {
byte[] message = ("my-message-" + i).getBytes();
Message msg = MessageBuilder.create().setContent(message).build();
producer.send(msg);
}
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
rolloverPerIntervalStats();
assertTrue(topic.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
// we expect 10 messages in the backlog since we sent 10 messages with the batch size set to 5.
// However, we are using synchronous send and so each message will go as an individual message
assertEquals(topic.getPersistentSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 10);
consumer = pulsarClient.subscribe(topicName, subscriptionName);
for (int i = 0; i < numMsgs; i++) {
Message msg = consumer.receive(5, TimeUnit.SECONDS);
assertNotNull(msg);
String receivedMessage = new String(msg.getData());
String expectedMessage = "my-message-" + i;
Assert.assertEquals(receivedMessage, expectedMessage, "Received message " + receivedMessage + " did not match the expected message " + expectedMessage);
}
consumer.close();
producer.close();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BatchMessageTest method testBatchProducerWithLargeMessage.
@Test(dataProvider = "codec")
public void testBatchProducerWithLargeMessage(CompressionType compressionType) throws Exception {
int numMsgs = 50;
int numMsgsInBatch = numMsgs / 2;
final String topicName = "persistent://prop/use/finance/testBatchProducerWithLargeMessage";
final String subscriptionName = "large-message-sub-1" + compressionType.toString();
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
consumer.close();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setCompressionType(compressionType);
producerConf.setBatchingMaxPublishDelay(5000, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(numMsgsInBatch);
producerConf.setBatchingEnabled(true);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
for (int i = 0; i < numMsgs; i++) {
if (i == 25) {
// send a large message
byte[] largeMessage = new byte[128 * 1024 + 4];
Message msg = MessageBuilder.create().setContent(largeMessage).build();
sendFutureList.add(producer.sendAsync(msg));
} else {
byte[] message = ("msg-" + i).getBytes();
Message msg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(msg));
}
}
byte[] message = ("msg-" + "last").getBytes();
Message lastMsg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(lastMsg));
FutureUtil.waitForAll(sendFutureList).get();
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
rolloverPerIntervalStats();
assertTrue(topic.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
// we expect 3 messages in the backlog since the large message in the middle should
// close out the batch and be sent in a batch of its own
assertEquals(topic.getPersistentSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 3);
consumer = pulsarClient.subscribe(topicName, subscriptionName);
for (int i = 0; i <= numMsgs; i++) {
Message msg = consumer.receive(5, TimeUnit.SECONDS);
assertNotNull(msg);
LOG.info("received msg - {}", msg.getData().toString());
consumer.acknowledge(msg);
}
Thread.sleep(100);
assertEquals(topic.getPersistentSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 0);
consumer.close();
producer.close();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class ResendRequestTest method testExclusiveSingleAckedPartitionedTopic.
@Test(timeOut = testTimeout)
public void testExclusiveSingleAckedPartitionedTopic() throws Exception {
String key = "testExclusiveSingleAckedPartitionedTopic";
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;
final int numberOfPartitions = 4;
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(7);
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
log.info("Message produced: " + message);
producer.send(message.getBytes());
}
// 4. Receive messages
Message message = consumer.receive();
int messageCount = 0;
log.info("Message received " + new String(message.getData()));
do {
messageCount += 1;
log.info("Message received " + new String(message.getData()));
message = consumer.receive(500, TimeUnit.MILLISECONDS);
} while (message != null);
assertEquals(messageCount, totalMessages);
// 5. Ask for redeliver
consumer.redeliverUnacknowledgedMessages();
// 6. Check if Messages redelivered again
message = consumer.receive();
messageCount = 0;
log.info("Message received " + new String(message.getData()));
do {
messageCount += 1;
log.info("Message received " + new String(message.getData()));
message = consumer.receive(500, TimeUnit.MILLISECONDS);
} while (message != null);
assertEquals(messageCount, totalMessages);
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class ResendRequestTest 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 = 10;
final int numberOfPartitions = 3;
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
Random rn = new Random();
// 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(7);
consumerConfig.setSubscriptionType(SubscriptionType.Shared);
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
log.info("Message produced: " + message);
producer.send(message.getBytes());
}
// 4. Receive messages
Message message1 = consumer1.receive();
Message message2 = consumer2.receive();
int messageCount1 = 0;
int messageCount2 = 0;
int ackCount1 = 0;
int ackCount2 = 0;
do {
if (message1 != null) {
log.info("Consumer1 received " + new String(message1.getData()));
messageCount1 += 1;
if (rn.nextInt() % 3 == 0) {
consumer1.acknowledge(message1);
log.info("Consumer1 acked " + new String(message1.getData()));
ackCount1 += 1;
}
}
if (message2 != null) {
log.info("Consumer2 received " + new String(message2.getData()));
messageCount2 += 1;
if (rn.nextInt() % 3 == 0) {
consumer2.acknowledge(message2);
log.info("Consumer2 acked " + new String(message2.getData()));
ackCount2 += 1;
}
}
message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
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. Ask for redeliver
log.info(key + ": Sent a Redeliver Message Request");
consumer1.redeliverUnacknowledgedMessages();
if ((ackCount1 + ackCount2) == totalMessages) {
return;
}
// 6. Check if Messages redelivered again
message1 = consumer1.receive(5000, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(5000, TimeUnit.MILLISECONDS);
messageCount1 = 0;
do {
if (message1 != null) {
log.info("Consumer1 received " + new String(message1.getData()));
messageCount1 += 1;
}
if (message2 != null) {
log.info("Consumer2 received " + new String(message2.getData()));
messageCount2 += 1;
}
message1 = consumer1.receive(1000, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(1000, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2 + ackCount1, totalMessages);
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class ResendRequestTest method testFailoverSingleAckedPartitionedTopic.
@Test(timeOut = testTimeout)
public void testFailoverSingleAckedPartitionedTopic() throws Exception {
String key = "testFailoverSingleAckedPartitionedTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-failover-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 10;
final int numberOfPartitions = 3;
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
Random rn = new Random();
// 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(7);
consumerConfig.setSubscriptionType(SubscriptionType.Failover);
consumerConfig.setConsumerName("Consumer-1");
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
consumerConfig.setConsumerName("Consumer-2");
Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, consumerConfig);
Thread.sleep(1000);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
log.info("Message produced: " + message);
producer.send(message.getBytes());
}
// 4. Receive messages
Message message1 = consumer1.receive();
Message message2 = consumer2.receive();
int messageCount1 = 0;
int messageCount2 = 0;
int ackCount1 = 0;
int ackCount2 = 0;
do {
if (message1 != null) {
log.info("Consumer1 received " + new String(message1.getData()));
messageCount1 += 1;
if (rn.nextInt() % 3 == 0) {
consumer1.acknowledge(message1);
ackCount1 += 1;
}
}
if (message2 != null) {
log.info("Consumer2 received " + new String(message2.getData()));
messageCount2 += 1;
if (rn.nextInt() % 3 == 0) {
consumer2.acknowledge(message2);
ackCount2 += 1;
}
}
message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount1 + messageCount2, totalMessages);
if ((ackCount1 + ackCount2) == totalMessages) {
return;
}
// 5. Ask for redeliver
log.info(key + ": Sent a Redeliver Message Request");
consumer1.redeliverUnacknowledgedMessages();
consumer1.close();
// 6. Check if Messages redelivered again
message2 = consumer2.receive();
messageCount1 = 0;
do {
if (message2 != null) {
log.info("Consumer2 received " + new String(message2.getData()));
messageCount2 += 1;
}
message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info(key + " messageCount1 = " + messageCount1);
log.info(key + " messageCount2 = " + messageCount2);
log.info(key + " ackCount1 = " + ackCount1);
log.info(key + " ackCount2 = " + ackCount2);
assertEquals(messageCount2 + ackCount1, totalMessages);
}
Aggregations