17
17
package org .apache .kafka .clients .consumer .internals ;
18
18
19
19
import org .apache .kafka .clients .consumer .AcknowledgeType ;
20
+ import org .apache .kafka .common .KafkaException ;
20
21
import org .apache .kafka .common .protocol .Errors ;
21
22
22
23
import java .util .ArrayList ;
@@ -35,15 +36,20 @@ public class Acknowledgements {
35
36
// The acknowledgements keyed by offset. If the record is a gap, the AcknowledgeType will be null.
36
37
private final Map <Long , AcknowledgeType > acknowledgements ;
37
38
38
- // When the broker responds to the acknowledgements, this is the error code returned.
39
- private Errors acknowledgeErrorCode ;
39
+ // When the broker responds to the acknowledgements, this is the exception thrown.
40
+ private KafkaException acknowledgeException ;
41
+
42
+ // Set when the broker has responded to the acknowledgements.
43
+ private boolean completed ;
40
44
41
45
public static Acknowledgements empty () {
42
46
return new Acknowledgements (new TreeMap <>());
43
47
}
44
48
45
49
private Acknowledgements (Map <Long , AcknowledgeType > acknowledgements ) {
46
50
this .acknowledgements = acknowledgements ;
51
+ this .acknowledgeException = null ;
52
+ this .completed = false ;
47
53
}
48
54
49
55
/**
@@ -115,25 +121,26 @@ public int size() {
115
121
* @return Whether the acknowledgements were sent to the broker and a response received
116
122
*/
117
123
public boolean isCompleted () {
118
- return acknowledgeErrorCode != null ;
124
+ return completed ;
119
125
}
120
126
121
127
/**
122
- * Set the acknowledgement error code when the response has been received from the broker.
128
+ * Completes the acknowledgements when the response has been received from the broker.
123
129
*
124
- * @param acknowledgeErrorCode the error code
130
+ * @param acknowledgeException the exception (will be null if successful)
125
131
*/
126
- public void setAcknowledgeErrorCode (Errors acknowledgeErrorCode ) {
127
- this .acknowledgeErrorCode = acknowledgeErrorCode ;
132
+ public void complete (KafkaException acknowledgeException ) {
133
+ this .acknowledgeException = acknowledgeException ;
134
+ completed = true ;
128
135
}
129
136
130
137
/**
131
- * Get the acknowledgement error code when the response has been received from the broker.
138
+ * Get the acknowledgement exception when the response has been received from the broker.
132
139
*
133
140
* @return the error code
134
141
*/
135
- public Errors getAcknowledgeErrorCode () {
136
- return acknowledgeErrorCode ;
142
+ public KafkaException getAcknowledgeException () {
143
+ return acknowledgeException ;
137
144
}
138
145
139
146
/**
@@ -301,10 +308,10 @@ private boolean canOptimiseForSingleAcknowledgeType(AcknowledgementBatch acknowl
301
308
public String toString () {
302
309
StringBuilder sb = new StringBuilder ("Acknowledgements(" );
303
310
sb .append (acknowledgements );
304
- if ( acknowledgeErrorCode != null ) {
305
- sb . append ( ", errorCode= " );
306
- sb .append (acknowledgeErrorCode . code () );
307
- }
311
+ sb . append ( ", acknowledgeException=" );
312
+ sb . append ( acknowledgeException != null ? Errors . forException ( acknowledgeException ) : "null " );
313
+ sb .append (", completed=" );
314
+ sb . append ( completed );
308
315
sb .append (")" );
309
316
return sb .toString ();
310
317
}
0 commit comments