34
34
35
35
import static org .apache .camel .component .azure .eventhubs .EventHubsConstants .COMPLETED_BY_SIZE ;
36
36
import static org .apache .camel .component .azure .eventhubs .EventHubsConstants .COMPLETED_BY_TIMEOUT ;
37
- import static org .apache .camel .component .azure .eventhubs .EventHubsConstants .UNCOMPLETED ;
38
37
39
38
public class EventHubsConsumer extends DefaultConsumer {
40
39
@@ -170,21 +169,28 @@ private void processCommit(final Exchange exchange, final EventContext eventCont
170
169
}
171
170
172
171
try {
173
- var completionCondition = processCheckpoint (exchange );
174
- if (completionCondition .equals (COMPLETED_BY_SIZE )) {
172
+ var cnt = processedEvents .incrementAndGet ();
173
+ if (cnt == getConfiguration ().getCheckpointBatchSize ()) {
174
+ processedEvents .set (0 );
175
+ exchange .getIn ().setHeader (EventHubsConstants .CHECKPOINT_UPDATED_BY , COMPLETED_BY_SIZE );
176
+ LOG .debug ("eventhub consumer batch size of reached" );
177
+ if (lastTask != null ) {
178
+ lastTask .cancel ();
179
+ }
175
180
eventContext .updateCheckpointAsync ()
176
181
.subscribe (unused -> LOG .debug ("Processed one event..." ),
177
182
error -> LOG .debug ("Error when updating Checkpoint: {}" , error .getMessage ()),
178
183
() -> {
179
- processedEvents .set (0 );
180
184
LOG .debug ("Checkpoint updated." );
181
185
});
182
-
183
- } else if (!completionCondition .equals (COMPLETED_BY_TIMEOUT )) {
184
- processedEvents .incrementAndGet ();
186
+ } else if (System .currentTimeMillis () >= lastTask .scheduledExecutionTime ()) {
187
+ exchange .getIn ().setHeader (EventHubsConstants .CHECKPOINT_UPDATED_BY , COMPLETED_BY_TIMEOUT );
188
+ LOG .debug ("eventhub consumer batch timeout reached" );
189
+ } else {
190
+ LOG .debug ("neither eventhub consumer batch size of {}/{} nor batch timeout reached yet" , cnt ,
191
+ getConfiguration ().getCheckpointBatchSize ());
185
192
}
186
193
// we assume that the timer task has done the update by its side
187
-
188
194
} catch (Exception ex ) {
189
195
getExceptionHandler ().handleException ("Error occurred during updating the checkpoint. This exception is ignored." ,
190
196
exchange , ex );
@@ -202,39 +208,4 @@ private void processRollback(Exchange exchange) {
202
208
getExceptionHandler ().handleException ("Error during processing exchange." , exchange , cause );
203
209
}
204
210
}
205
-
206
- /**
207
- * Checks either the batch size or the batch timeout is reached
208
- *
209
- * @param exchange the exchange
210
- * @return the completion condition (batch size or batch timeout) if one of them is reached, else the
211
- * 'uncompleted' state adds a header {@value EventHubsConstants#CHECKPOINT_UPDATED_BY} with the
212
- * completion condition (
213
- */
214
- private String processCheckpoint (Exchange exchange ) {
215
- // Check if the batch size is reached
216
- if (processedEvents .get () % getConfiguration ().getCheckpointBatchSize () == 0 ) {
217
- exchange .getIn ().setHeader (EventHubsConstants .CHECKPOINT_UPDATED_BY , COMPLETED_BY_SIZE );
218
- LOG .debug ("eventhub consumer batch size of reached" );
219
- // no need to run task if the batch size already did the checkpointing
220
- if (lastTask != null ) {
221
- lastTask .cancel ();
222
- }
223
-
224
- return COMPLETED_BY_SIZE ;
225
- } else {
226
- LOG .debug ("eventhub consumer batch size of {}/{} not reached yet" , processedEvents .get (),
227
- getConfiguration ().getCheckpointBatchSize ());
228
- }
229
-
230
- // Check if the batch timeout is reached
231
- if (System .currentTimeMillis () >= lastTask .scheduledExecutionTime ()) {
232
- exchange .getIn ().setHeader (EventHubsConstants .CHECKPOINT_UPDATED_BY , COMPLETED_BY_TIMEOUT );
233
- LOG .debug ("eventhub consumer batch timeout reached" );
234
-
235
- return COMPLETED_BY_TIMEOUT ;
236
- }
237
-
238
- return UNCOMPLETED ;
239
- }
240
211
}
0 commit comments