use of com.yahoo.pulsar.client.api.Producer in project pulsar by yahoo.
the class PersistentTopicE2ETest method testSimpleProducerEvents.
@Test
public void testSimpleProducerEvents() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic0";
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. producer publish messages
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
rolloverPerIntervalStats();
assertTrue(topicRef.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
// 3. producer disconnect
producer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
assertEquals(topicRef.getProducers().size(), 0);
}
use of com.yahoo.pulsar.client.api.Producer in project pulsar by yahoo.
the class PersistentTopicE2ETest method testMessageExpiry.
@Test
public void testMessageExpiry() throws Exception {
int messageTTLSecs = 1;
String namespaceName = "prop/use/expiry-check";
admin.namespaces().createNamespace(namespaceName);
admin.namespaces().setNamespaceMessageTTL(namespaceName, messageTTLSecs);
final String topicName = "persistent://prop/use/expiry-check/topic1";
final String subName = "sub1";
final int numMsgs = 10;
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
consumer.close();
assertFalse(subRef.getDispatcher().isConsumerConnected());
Producer producer = pulsarClient.createProducer(topicName);
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
rolloverPerIntervalStats();
assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs);
Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs));
runMessageExpiryCheck();
// 1. check all messages expired for this unconnected subscription
assertEquals(subRef.getNumberOfEntriesInBacklog(), 0);
// clean-up
producer.close();
consumer.close();
admin.persistentTopics().deleteSubscription(topicName, subName);
admin.persistentTopics().delete(topicName);
admin.namespaces().deleteNamespace(namespaceName);
}
use of com.yahoo.pulsar.client.api.Producer in project pulsar by yahoo.
the class PersistentTopicE2ETest method testReceiveWithTimeout.
@Test
public void testReceiveWithTimeout() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic-receive-timeout";
final String subName = "sub";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
conf.setReceiverQueueSize(1000);
ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subName, conf);
Producer producer = pulsarClient.createProducer(topicName);
assertEquals(consumer.getAvailablePermits(), 0);
Message msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNull(msg);
assertEquals(consumer.getAvailablePermits(), 0);
producer.send("test".getBytes());
Thread.sleep(100);
assertEquals(consumer.getAvailablePermits(), 0);
msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(consumer.getAvailablePermits(), 1);
msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNull(msg);
assertEquals(consumer.getAvailablePermits(), 1);
}
use of com.yahoo.pulsar.client.api.Producer in project pulsar by yahoo.
the class PersistentTopicE2ETest method testConsumerFlowControl.
@Test
public void testConsumerFlowControl() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic2";
final String subName = "sub2";
Message msg;
int recvQueueSize = 4;
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
conf.setReceiverQueueSize(recvQueueSize);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
assertNotNull(subRef);
// 1. initial receive queue size recorded
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
assertEquals(getAvailablePermits(subRef), recvQueueSize);
for (int i = 0; i < recvQueueSize / 2; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
msg = consumer.receive();
consumer.acknowledge(msg);
}
// 2. queue size re-adjusted after successful receive of half of window size
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
assertEquals(getAvailablePermits(subRef), recvQueueSize);
consumer.close();
assertFalse(subRef.getDispatcher().isConsumerConnected());
}
use of com.yahoo.pulsar.client.api.Producer in project pulsar by yahoo.
the class PersistentTopicE2ETest method testSimpleCloseTopic.
@Test
public void testSimpleCloseTopic() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic5";
final String subName = "sub5";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
assertNotNull(subRef);
Message msg;
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
msg = consumer.receive();
consumer.acknowledge(msg);
}
producer.close();
consumer.close();
topicRef.close().get();
assertNull(pulsar.getBrokerService().getTopicReference(topicName));
}
Aggregations