@@ -49,7 +49,7 @@ public class KinesisShardConsumer implements IngestionShardConsumer<SequenceNumb
49
49
*/
50
50
;
51
51
private KinesisClient kinesisClient ;
52
- private String lastFetchedSequenceNumber = "" ;
52
+ private String lastShardIterator ;
53
53
final String clientId ;
54
54
final String kinesisShardId ;
55
55
final int shardId ;
@@ -124,6 +124,7 @@ public List<ReadResult<SequenceNumber, KinesisMessage>> readNext(
124
124
int timeoutMillis
125
125
) throws TimeoutException {
126
126
List <ReadResult <SequenceNumber , KinesisMessage >> records = fetch (
127
+ null ,
127
128
sequenceNumber .getSequenceNumber (),
128
129
includeStart ,
129
130
maxMessages ,
@@ -132,6 +133,14 @@ public List<ReadResult<SequenceNumber, KinesisMessage>> readNext(
132
133
return records ;
133
134
}
134
135
136
+ @ Override
137
+ public List <ReadResult <SequenceNumber , KinesisMessage >> readNext (long maxMessages , int timeoutMillis ) throws TimeoutException {
138
+ if (lastShardIterator == null ) {
139
+ throw new IllegalStateException ("No shard iterator available" );
140
+ }
141
+ return fetch (lastShardIterator , null , false , maxMessages , timeoutMillis );
142
+ }
143
+
135
144
@ Override
136
145
public IngestionShardPointer earliestPointer () {
137
146
return getSequenceNumber (ShardIteratorType .TRIM_HORIZON , null , 0 );
@@ -142,40 +151,50 @@ public IngestionShardPointer latestPointer() {
142
151
return getSequenceNumber (ShardIteratorType .LATEST , null , 0 );
143
152
}
144
153
145
- private List <Record > fetchRecords (ShardIteratorType shardIteratorType , String startingSequenceNumber , long timestampMillis , int limit ) {
146
- // Get a shard iterator AFTER the given sequence number
147
- GetShardIteratorRequest .Builder builder = GetShardIteratorRequest .builder ()
148
- .streamName (config .getStream ())
149
- .shardId (kinesisShardId )
150
- .shardIteratorType (shardIteratorType );
154
+ private List <Record > fetchRecords (
155
+ String shardIterator ,
156
+ ShardIteratorType shardIteratorType ,
157
+ String startingSequenceNumber ,
158
+ long timestampMillis ,
159
+ int limit
160
+ ) {
161
+ String shardIteratorToUse = shardIterator ;
151
162
152
- if (startingSequenceNumber != null ) {
153
- builder = builder .startingSequenceNumber (startingSequenceNumber );
154
- }
163
+ if (shardIterator == null ) {
164
+ // fetch the shard iterator
165
+ GetShardIteratorRequest .Builder builder = GetShardIteratorRequest .builder ()
166
+ .streamName (config .getStream ())
167
+ .shardId (kinesisShardId )
168
+ .shardIteratorType (shardIteratorType );
169
+
170
+ if (startingSequenceNumber != null ) {
171
+ builder = builder .startingSequenceNumber (startingSequenceNumber );
172
+ }
155
173
156
- if (timestampMillis != 0 ) {
157
- builder = builder .timestamp (Instant .ofEpochMilli (timestampMillis ));
158
- }
174
+ if (timestampMillis != 0 ) {
175
+ builder = builder .timestamp (Instant .ofEpochMilli (timestampMillis ));
176
+ }
159
177
160
- GetShardIteratorRequest shardIteratorRequest = builder .build ();
178
+ GetShardIteratorRequest shardIteratorRequest = builder .build ();
161
179
162
- GetShardIteratorResponse shardIteratorResponse = kinesisClient .getShardIterator (shardIteratorRequest );
163
- String shardIterator = shardIteratorResponse .shardIterator ();
180
+ GetShardIteratorResponse shardIteratorResponse = kinesisClient .getShardIterator (shardIteratorRequest );
181
+ shardIteratorToUse = shardIteratorResponse .shardIterator ();
182
+ }
164
183
165
- if (shardIterator == null ) {
184
+ if (shardIteratorToUse == null ) {
166
185
return new ArrayList <>();
167
186
}
168
187
169
188
// Fetch the next records
170
- GetRecordsRequest recordsRequest = GetRecordsRequest .builder ().shardIterator (shardIterator ).limit (limit ).build ();
171
-
189
+ GetRecordsRequest recordsRequest = GetRecordsRequest .builder ().shardIterator (shardIteratorToUse ).limit (limit ).build ();
172
190
GetRecordsResponse recordsResponse = kinesisClient .getRecords (recordsRequest );
191
+ lastShardIterator = recordsResponse .nextShardIterator ();
173
192
List <Record > records = recordsResponse .records ();
174
193
return records ;
175
194
}
176
195
177
196
private SequenceNumber getSequenceNumber (ShardIteratorType shardIteratorType , String startingSequenceNumber , long timestampMillis ) {
178
- List <Record > records = fetchRecords (shardIteratorType , startingSequenceNumber , timestampMillis , 1 );
197
+ List <Record > records = fetchRecords (null , shardIteratorType , startingSequenceNumber , timestampMillis , 1 );
179
198
180
199
if (!records .isEmpty ()) {
181
200
Record nextRecord = records .get (0 );
@@ -197,6 +216,7 @@ public IngestionShardPointer pointerFromOffset(String offset) {
197
216
}
198
217
199
218
private synchronized List <ReadResult <SequenceNumber , KinesisMessage >> fetch (
219
+ String shardIterator ,
200
220
String sequenceNumber ,
201
221
boolean includeStart ,
202
222
long maxMessages ,
@@ -208,7 +228,7 @@ private synchronized List<ReadResult<SequenceNumber, KinesisMessage>> fetch(
208
228
209
229
ShardIteratorType iteratorType = includeStart ? ShardIteratorType .AT_SEQUENCE_NUMBER : ShardIteratorType .AFTER_SEQUENCE_NUMBER ;
210
230
211
- List <Record > records = fetchRecords (iteratorType , sequenceNumber , 0 , (int ) limit );
231
+ List <Record > records = fetchRecords (shardIterator , iteratorType , sequenceNumber , 0 , (int ) limit );
212
232
213
233
List <ReadResult <SequenceNumber , KinesisMessage >> results = new ArrayList <>();
214
234
0 commit comments