Java Code Examples for software.amazon.awssdk.services.kinesis.model.GetRecordsResponse#nextShardIterator()

The following examples show how to use software.amazon.awssdk.services.kinesis.model.GetRecordsResponse#nextShardIterator() . 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: KinesisShardIterator.java    From synapse with Apache License 2.0 6 votes vote down vote up
private GetRecordsResponse tryNext() {
    GetRecordsResponse response = kinesisClient.getRecords(GetRecordsRequest.builder()
            .shardIterator(id)
            .limit(fetchRecordLimit)
            .build())
            .join();
    this.id = response.nextShardIterator();
    LOG.debug("next() with id " + this.id + " returned " + response.records().size() + " records");
    if (!response.records().isEmpty()) {
        this.shardPosition = fromPosition(
                shardPosition.shardName(),
                response.records().get(response.records().size()-1).sequenceNumber()
        );
    }
    return response;
}
 
Example 2
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void testGets(final String streamName, final Shard shard) {
    // Get an iterator for the first shard.
    GetShardIteratorResponse iteratorResult = client.getShardIterator(
            GetShardIteratorRequest.builder()
                                   .streamName(streamName)
                                   .shardId(shard.shardId())
                                   .shardIteratorType(ShardIteratorType.AT_SEQUENCE_NUMBER)
                                   .startingSequenceNumber(shard.sequenceNumberRange().startingSequenceNumber())
                                   .build());
    Assert.assertNotNull(iteratorResult);

    String iterator = iteratorResult.shardIterator();
    Assert.assertNotNull(iterator);

    int tries = 0;
    GetRecordsResponse result;
    List<Record> records;

    // Read the first record from the first shard (looping until it's
    // available).
    while (true) {
        tries += 1;
        if (tries > 100) {
            Assert.fail("Failed to read any records after 100 seconds");
        }

        result = client.getRecords(GetRecordsRequest.builder()
                                                    .shardIterator(iterator)
                                                    .limit(1)
                                                    .build());
        Assert.assertNotNull(result);
        Assert.assertNotNull(result.records());
        Assert.assertNotNull(result.nextShardIterator());

        records = result.records();
        if (records.size() > 0) {
            long arrivalTime = records.get(0).approximateArrivalTimestamp().toEpochMilli();
            Long delta = Math.abs(Instant.now().minusMillis(arrivalTime).toEpochMilli());
            // Assert that the arrival date is within 5 minutes of the current date to make sure it unmarshalled correctly.
            assertThat(delta, Matchers.lessThan(60 * 5000L));
            break;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException exception) {
            throw new RuntimeException(exception);
        }

        iterator = result.nextShardIterator();
    }

    System.out.println("  [Succeeded after " + tries + " tries]");
    Assert.assertEquals(1, records.size());
    validateRecord(records.get(0), "See No Evil");

    // Read the second record from the first shard.
    result = client.getRecords(GetRecordsRequest.builder()
                                                .shardIterator(result.nextShardIterator())
                                                .build());
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.records());
    Assert.assertNotNull(result.nextShardIterator());

    records = result.records();
    Assert.assertEquals(1, records.size());
    validateRecord(records.get(0), "See No Evil");

    // Try to read some more, get EOF.
    result = client.getRecords(GetRecordsRequest.builder()
                                                .shardIterator(result.nextShardIterator())
                                                .build());
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.records());
    Assert.assertTrue(result.records().isEmpty());
    Assert.assertNull(result.nextShardIterator());
}