60
60
* @author Gary Russell
61
61
* @author Dariusz Szablinski
62
62
* @author Biju Kunjummen
63
+ * @author Borahm Lee
63
64
* @since 1.1
64
65
*/
65
66
public class BatchMessagingMessageConverter implements BatchMessageConverter {
@@ -89,7 +90,7 @@ public BatchMessagingMessageConverter() {
89
90
* @param recordConverter the converter.
90
91
* @since 1.3.2
91
92
*/
92
- public BatchMessagingMessageConverter (RecordMessageConverter recordConverter ) {
93
+ public BatchMessagingMessageConverter (@ Nullable RecordMessageConverter recordConverter ) {
93
94
this .recordConverter = recordConverter ;
94
95
if (JacksonPresent .isJackson2Present ()) {
95
96
this .headerMapper = new DefaultKafkaHeaderMapper ();
@@ -157,53 +158,42 @@ public Message<?> toMessage(List<ConsumerRecord<?, ?>> records, @Nullable Acknow
157
158
List <Headers > natives = new ArrayList <>();
158
159
List <ConsumerRecord <?, ?>> raws = new ArrayList <>();
159
160
List <ConversionException > conversionFailures = new ArrayList <>();
161
+
160
162
addToRawHeaders (rawHeaders , convertedHeaders , natives , raws , conversionFailures );
161
163
commonHeaders (acknowledgment , consumer , rawHeaders , keys , topics , partitions , offsets , timestampTypes ,
162
164
timestamps );
163
- boolean logged = false ;
164
- String info = null ;
165
+
166
+ String listenerInfo = null ;
165
167
for (ConsumerRecord <?, ?> record : records ) {
166
- payloads .add (obtainPayload (type , record , conversionFailures ));
167
- keys .add (record .key ());
168
- topics .add (record .topic ());
169
- partitions .add (record .partition ());
170
- offsets .add (record .offset ());
171
- if (record .timestampType () != null ) {
172
- timestampTypes .add (record .timestampType ().name ());
173
- }
174
- timestamps .add (record .timestamp ());
168
+ addRecordInfo (record , type , payloads , keys , topics , partitions , offsets , timestampTypes , timestamps , conversionFailures );
175
169
if (this .headerMapper != null && record .headers () != null ) {
176
- Map <String , Object > converted = new HashMap <>();
177
- this .headerMapper .toHeaders (record .headers (), converted );
178
- convertedHeaders .add (converted );
179
- Object object = converted .get (KafkaHeaders .LISTENER_INFO );
180
- if (object instanceof String ) {
181
- info = (String ) object ;
170
+ Map <String , Object > converted = convertHeaders (record .headers (), convertedHeaders );
171
+ Object obj = converted .get (KafkaHeaders .LISTENER_INFO );
172
+ if (obj instanceof String ) {
173
+ listenerInfo = (String ) obj ;
182
174
}
183
175
}
184
176
else {
185
- if (!logged ) {
186
- this .logger .debug (() ->
187
- "No header mapper is available; Jackson is required for the default mapper; "
188
- + "headers (if present) are not mapped but provided raw in "
189
- + KafkaHeaders .NATIVE_HEADERS );
190
- logged = true ;
191
- }
192
177
natives .add (record .headers ());
193
178
}
194
179
if (this .rawRecordHeader ) {
195
180
raws .add (record );
196
181
}
197
182
}
198
- if (info != null ) {
199
- rawHeaders .put (KafkaHeaders .LISTENER_INFO , info );
183
+ if (this .headerMapper == null && !natives .isEmpty ()) {
184
+ this .logger .debug (() ->
185
+ "No header mapper is available; Jackson is required for the default mapper; "
186
+ + "headers (if present) are not mapped but provided raw in "
187
+ + KafkaHeaders .NATIVE_HEADERS );
188
+ }
189
+ if (listenerInfo != null ) {
190
+ rawHeaders .put (KafkaHeaders .LISTENER_INFO , listenerInfo );
200
191
}
201
192
return MessageBuilder .createMessage (payloads , kafkaMessageHeaders );
202
193
}
203
194
204
195
private void addToRawHeaders (Map <String , Object > rawHeaders , List <Map <String , Object >> convertedHeaders ,
205
196
List <Headers > natives , List <ConsumerRecord <?, ?>> raws , List <ConversionException > conversionFailures ) {
206
-
207
197
if (this .headerMapper != null ) {
208
198
rawHeaders .put (KafkaHeaders .BATCH_CONVERTED_HEADERS , convertedHeaders );
209
199
}
@@ -216,12 +206,33 @@ private void addToRawHeaders(Map<String, Object> rawHeaders, List<Map<String, Ob
216
206
rawHeaders .put (KafkaHeaders .CONVERSION_FAILURES , conversionFailures );
217
207
}
218
208
209
+ private void addRecordInfo (ConsumerRecord <?, ?> record , Type type , List <Object > payloads , List <Object > keys ,
210
+ List <String > topics , List <Integer > partitions , List <Long > offsets , List <String > timestampTypes ,
211
+ List <Long > timestamps , List <ConversionException > conversionFailures ) {
212
+ payloads .add (obtainPayload (type , record , conversionFailures ));
213
+ keys .add (record .key ());
214
+ topics .add (record .topic ());
215
+ partitions .add (record .partition ());
216
+ offsets .add (record .offset ());
217
+ timestamps .add (record .timestamp ());
218
+ if (record .timestampType () != null ) {
219
+ timestampTypes .add (record .timestampType ().name ());
220
+ }
221
+ }
222
+
219
223
private Object obtainPayload (Type type , ConsumerRecord <?, ?> record , List <ConversionException > conversionFailures ) {
220
224
return this .recordConverter == null || !containerType (type )
221
225
? extractAndConvertValue (record , type )
222
226
: convert (record , type , conversionFailures );
223
227
}
224
228
229
+ private Map <String , Object > convertHeaders (Headers headers , List <Map <String , Object >> convertedHeaders ) {
230
+ Map <String , Object > converted = new HashMap <>();
231
+ this .headerMapper .toHeaders (headers , converted );
232
+ convertedHeaders .add (converted );
233
+ return converted ;
234
+ }
235
+
225
236
@ Override
226
237
public List <ProducerRecord <?, ?>> fromMessage (Message <?> message , String defaultTopic ) {
227
238
throw new UnsupportedOperationException ();
0 commit comments