@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; }
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; }
public DescribeStreamResult unmarshall(JsonUnmarshallerContext context) throws Exception { DescribeStreamResult describeStreamResult = new DescribeStreamResult(); int originalDepth = context.getCurrentDepth(); String currentParentElement = context.getCurrentParentElement(); int targetDepth = originalDepth + 1; JsonToken token = context.getCurrentToken(); if (token == null) token = context.nextToken(); if (token == VALUE_NULL) return null; while (true) { if (token == null) break; if (token == FIELD_NAME || token == START_OBJECT) { if (context.testExpression("StreamDescription", targetDepth)) { context.nextToken(); describeStreamResult.setStreamDescription( StreamDescriptionJsonUnmarshaller.getInstance().unmarshall(context)); } } else if (token == END_ARRAY || token == END_OBJECT) { if (context.getLastParsedParentElement() == null || context.getLastParsedParentElement().equals(currentParentElement)) { if (context.getCurrentDepth() <= originalDepth) break; } } token = context.nextToken(); } return describeStreamResult; }
/** * 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(); }