Java Code Examples for org.apache.pulsar.client.api.Reader#readNext()

The following examples show how to use org.apache.pulsar.client.api.Reader#readNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReaderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void testReadMessages(String topic, boolean enableBatch) throws Exception {
    int numKeys = 10;

    Set<String> keys = publishMessages(topic, numKeys, enableBatch);
    Reader<byte[]> reader = pulsarClient.newReader()
            .topic(topic)
            .startMessageId(MessageId.earliest)
            .readerName(subscription)
            .create();

    while (reader.hasMessageAvailable()) {
        Message<byte[]> message = reader.readNext();
        Assert.assertTrue(keys.remove(message.getKey()));
    }
    Assert.assertTrue(keys.isEmpty());

    Reader<byte[]> readLatest = pulsarClient.newReader().topic(topic).startMessageId(MessageId.latest)
                                            .readerName(subscription + "latest").create();
    Assert.assertFalse(readLatest.hasMessageAvailable());
}
 
Example 2
Source File: ReaderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadFromPartition() throws Exception {
    String topic = "persistent://my-property/my-ns/testReadFromPartition";
    String partition0 = topic + "-partition-0";
    admin.topics().createPartitionedTopic(topic, 4);
    int numKeys = 10;

    Set<String> keys = publishMessages(partition0, numKeys, false);
    Reader<byte[]> reader = pulsarClient.newReader()
            .topic(partition0)
            .startMessageId(MessageId.earliest)
            .create();

    while (reader.hasMessageAvailable()) {
        Message<byte[]> message = reader.readNext();
        Assert.assertTrue(keys.remove(message.getKey()));
    }
    Assert.assertTrue(keys.isEmpty());
}
 
Example 3
Source File: TopicTerminationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 20000)
public void testSimpleTerminationReader() throws Exception {
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

    MessageId msgId1 = producer.send("test-msg-1".getBytes());
    MessageId msgId2 = producer.send("test-msg-2".getBytes());
    MessageId msgId3 = producer.send("test-msg-3".getBytes());

    MessageId lastMessageId = admin.topics().terminateTopicAsync(topicName).get();
    assertEquals(lastMessageId, msgId3);

    Reader<byte[]> reader = pulsarClient.newReader().topic(topicName).startMessageId(MessageId.earliest).create();

    Message<byte[]> msg1 = reader.readNext();
    assertEquals(msg1.getMessageId(), msgId1);

    Message<byte[]> msg2 = reader.readNext();
    assertEquals(msg2.getMessageId(), msgId2);

    Message<byte[]> msg3 = reader.readNext();
    assertEquals(msg3.getMessageId(), msgId3);

    Message<byte[]> msg4 = reader.readNext(100, TimeUnit.MILLISECONDS);
    assertNull(msg4);

    Thread.sleep(100);
    assertTrue(reader.hasReachedEndOfTopic());
}
 
Example 4
Source File: FunctionRuntimeManager.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the FunctionRuntimeManager.  Does the following:
 * 1. Consume all existing assignments to establish existing/latest set of assignments
 * 2. After current assignments are read, assignments belonging to this worker will be processed
 *
 * @return the message id of the message processed during init phase
 */
public MessageId initialize() {
    try {
        Reader<byte[]> reader = WorkerUtils.createReader(
                workerService.getClient().newReader(),
                workerConfig.getWorkerId() + "-function-assignment-initialize",
                workerConfig.getFunctionAssignmentTopic(),
                MessageId.earliest);

        // start init phase
        this.isInitializePhase = true;
        // keep track of the last message read
        MessageId lastMessageRead = MessageId.earliest;
        // read all existing messages
        while (reader.hasMessageAvailable()) {
            Message<byte[]> message = reader.readNext();
            lastMessageRead = message.getMessageId();
            processAssignmentMessage(message);
        }
        // init phase is done
        this.isInitializePhase = false;
        // close reader
        reader.close();
        // realize existing assignments
        Map<String, Assignment> assignmentMap = workerIdToAssignments.get(this.workerConfig.getWorkerId());
        if (assignmentMap != null) {
            for (Assignment assignment : assignmentMap.values()) {
                if (needsStart(assignment)) {
                    startFunctionInstance(assignment);
                }
            }
        }
        // complete future to indicate initialization is complete
        isInitialized.complete(null);
        return lastMessageRead;
    } catch (Exception e) {
        log.error("Failed to initialize function runtime manager: {}", e.getMessage(), e);
        throw new RuntimeException(e);
    }
}