private String getShardItertor() { // either return a cached one or get a new one via a GetShardIterator request. if (currentShardIterator == null) { String shardId; // If ShardId supplied use it, else choose first one if (!getEndpoint().getShardId().isEmpty()) { shardId = getEndpoint().getShardId(); } else { DescribeStreamRequest req1 = new DescribeStreamRequest().withStreamName(getEndpoint().getStreamName()); DescribeStreamResult res1 = getClient().describeStream(req1); shardId = res1.getStreamDescription().getShards().get(0).getShardId(); } LOG.debug("ShardId is: {}", shardId); GetShardIteratorRequest req = new GetShardIteratorRequest() .withStreamName(getEndpoint().getStreamName()) .withShardId(shardId) .withShardIteratorType(getEndpoint().getIteratorType()); if (hasSequenceNumber()) { req.withStartingSequenceNumber(getEndpoint().getSequenceNumber()); } GetShardIteratorResult result = getClient().getShardIterator(req); currentShardIterator = result.getShardIterator(); } LOG.debug("Shard Iterator is: {}", currentShardIterator); return currentShardIterator; }
@JsonIgnore public boolean setupStream(AmazonKinesis kinesis, String streamName) { boolean setup = false; Preconditions.checkState(!Strings.isNullOrEmpty(streamName), "streamName was not specified"); try { DescribeStreamResult result; if (getRetryPeriod() != null) { Integer retryAttempts = getMaxAttempts(); while (retryAttempts == null || retryAttempts > 0) { try { result = kinesis.describeStream(streamName); if ("active".equalsIgnoreCase(result.getStreamDescription().getStreamStatus())) { LOG.info("stream {} is active", streamName); setup = true; break; } } catch (NullPointerException | ResourceNotFoundException e) { createStream(kinesis, streamName); } Thread.sleep(retryPeriod.toMilliseconds()); if (retryAttempts != null) { retryAttempts--; } } } } catch (InterruptedException e) { LOG.error( "Needed to create stream {} but was interrupted, nothing is guaranteed now", streamName); } return setup; }
/** * Get the available shards from the kinesis * * @param streamName Name of the stream from where the shards to be accessed * @return the list of shards from the given stream */ public List<Shard> getShardList(String streamName) { assert client != null : "Illegal client"; DescribeStreamRequest describeRequest = new DescribeStreamRequest(); describeRequest.setStreamName(streamName); DescribeStreamResult describeResponse = client.describeStream(describeRequest); return describeResponse.getStreamDescription().getShards(); }