Search in sources :

Example 21 with MessageId

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);
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 22 with MessageId

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);
}
Also used : MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 23 with MessageId

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);
}
Also used : MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 24 with MessageId

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());
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Message(org.apache.pulsar.client.api.Message) MessageListener(org.apache.pulsar.client.api.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 25 with MessageId

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());
}
Also used : MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Aggregations

MessageId (org.apache.pulsar.client.api.MessageId)65 Test (org.testng.annotations.Test)42 CompletableFuture (java.util.concurrent.CompletableFuture)25 Message (org.apache.pulsar.client.api.Message)22 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)16 List (java.util.List)14 TimeUnit (java.util.concurrent.TimeUnit)14 Producer (org.apache.pulsar.client.api.Producer)14 Future (java.util.concurrent.Future)13 Consumer (org.apache.pulsar.client.api.Consumer)13 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)13 ExecutorService (java.util.concurrent.ExecutorService)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 ByteBuf (io.netty.buffer.ByteBuf)10 HashSet (java.util.HashSet)10 Map (java.util.Map)10 Lists (com.google.common.collect.Lists)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8