use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class V1_ProducerConsumerTest method testPriorityConsumer.
@Test
public void testPriorityConsumer() throws Exception {
log.info("-- Starting {} test --", methodName);
ConsumerConfiguration conf1 = new ConsumerConfiguration();
conf1.setSubscriptionType(SubscriptionType.Shared);
conf1.setPriorityLevel(1);
conf1.setReceiverQueueSize(5);
ConsumerConfiguration conf4 = new ConsumerConfiguration();
conf4.setSubscriptionType(SubscriptionType.Shared);
conf4.setPriorityLevel(2);
conf4.setReceiverQueueSize(5);
Consumer consumer1 = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic2", "my-subscriber-name", conf1);
Consumer consumer2 = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic2", "my-subscriber-name", conf1);
Consumer consumer3 = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic2", "my-subscriber-name", conf1);
Consumer consumer4 = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic2", "my-subscriber-name", conf4);
ProducerConfiguration producerConf = new ProducerConfiguration();
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic2", producerConf);
List<Future<MessageId>> futures = Lists.newArrayList();
// Asynchronously produce messages
for (int i = 0; i < 15; i++) {
final String message = "my-message-" + i;
Future<MessageId> future = producer.sendAsync(message.getBytes());
futures.add(future);
}
log.info("Waiting for async publish to complete");
for (Future<MessageId> future : futures) {
future.get();
}
for (int i = 0; i < 20; i++) {
consumer1.receive(100, TimeUnit.MILLISECONDS);
consumer2.receive(100, TimeUnit.MILLISECONDS);
}
/**
* a. consumer1 and consumer2 now has more permits (as received and sent more permits) b. try to produce more
* messages: which will again distribute among consumer1 and consumer2 and should not dispatch to consumer4
*/
for (int i = 0; i < 5; i++) {
final String message = "my-message-" + i;
Future<MessageId> future = producer.sendAsync(message.getBytes());
futures.add(future);
}
Assert.assertNull(consumer4.receive(100, TimeUnit.MILLISECONDS));
// Asynchronously acknowledge upto and including the last message
producer.close();
consumer1.close();
consumer2.close();
consumer3.close();
consumer4.close();
log.info("-- Exiting {} test --", methodName);
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class MessageIdSerialization method testProtobufSerialization2.
@Test
void testProtobufSerialization2() throws Exception {
MessageId id = new MessageIdImpl(1, 2, -1);
byte[] serializedId = id.toByteArray();
assertEquals(MessageId.fromByteArray(serializedId), id);
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class MessageIdSerialization method testProtobufSerialization1.
@Test
void testProtobufSerialization1() throws Exception {
MessageId id = new MessageIdImpl(1, 2, 3);
byte[] serializedId = id.toByteArray();
assertEquals(MessageId.fromByteArray(serializedId), id);
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class TopicTerminationTest method testSimpleTerminationMessageListener.
@Test(timeOut = 20000)
public void testSimpleTerminationMessageListener() throws Exception {
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
CountDownLatch latch = new CountDownLatch(1);
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").messageListener(new MessageListener<byte[]>() {
@Override
public void received(Consumer<byte[]> consumer, Message<byte[]> msg) {
// do nothing
}
@Override
public void reachedEndOfTopic(Consumer<byte[]> consumer) {
latch.countDown();
assertTrue(consumer.hasReachedEndOfTopic());
}
}).subscribe();
/* MessageId msgId1 = */
producer.send("test-msg-1".getBytes());
/* MessageId msgId2 = */
producer.send("test-msg-2".getBytes());
MessageId msgId3 = producer.send("test-msg-3".getBytes());
consumer.acknowledgeCumulative(msgId3);
Thread.sleep(100);
assertFalse(consumer.hasReachedEndOfTopic());
MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
assertEquals(lastMessageId, msgId3);
assertTrue(latch.await(3, TimeUnit.SECONDS));
assertTrue(consumer.hasReachedEndOfTopic());
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class TopicTerminationTest method testSubscribeOnTerminatedTopic.
@Test(timeOut = 20000)
public void testSubscribeOnTerminatedTopic() throws Exception {
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
/* MessageId msgId1 = */
producer.send("test-msg-1".getBytes());
MessageId msgId2 = producer.send("test-msg-2".getBytes());
MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
assertEquals(lastMessageId, msgId2);
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe();
Thread.sleep(200);
assertTrue(consumer.hasReachedEndOfTopic());
}
Aggregations