Commit a1ec4a0
GH-4097: Fix SmartMessageConverter support in batch listeners
Fixes: #4097
`BatchMessagingMessageConverter` was missing `setMessagingConverter()` method that exists in `MessagingMessageConverter`, causing `SmartMessageConverter` configured via `@KafkaListener(contentTypeConverter)` to be ignored in batch listeners.
This inconsistency between regular and batch listeners leads to `ClassCastException` when `byte[]` values aren't converted to the expected `String` type, breaking the contract that `SmartMessageConverter` should work the same way regardless of listener type.
The fix ensures `SmartMessageConverter` propagation works consistently by:
- Adding setMessagingConverter() to `BatchMessagingMessageConverter` that delegates to underlying `MessagingMessageConverter`
- Overriding setMessagingConverter() in `BatchMessagingMessageListenerAdapter` to propagate the converter to batch converter
- Maintaining the same `SmartMessageConverter` behavior between regular and batch listeners
Integration testing revealed the root cause of GH-4097. When Spring processes
a `@KafkaListener` with `contentTypeConverter` and `batch="true"`, the framework:
1. Calls `setBatchMessageConverter()` on the adapter
2. This internally calls `setMessageConverter()` which sets `converterSet=true`
3. Spring then tries to apply `contentTypeConverter` by calling `setMessagingConverter()`
4. The parent's validation `Assert.isTrue(!this.converterSet, ...)` blocks this
The unit test didn't catch this because it bypassed the adapter and Spring
framework integration entirely.
Changes:
- `BatchMessagingMessageListenerAdapter.setMessagingConverter()`: Override now
directly applies `SmartMessageConverter` to batch converter (which propagates
to record converter) without calling super, bypassing the validation that
doesn't apply to the batch listener workflow
- `BatchSmartMessageConverterTests`: Replaced unit test with full integration
test using `@SpringJUnitConfig`, `@EmbeddedKafka`, `ConcurrentKafkaListenerContainerFactory`,
and `@KafkaListener` to verify the complete framework flow
- Added minimal `ByteArrayToStringConverter` (24 lines) for testing as no
existing Spring Framework converter provides simple byte[] to String
conversion needed for this test scenario
All tests pass and checkstyle validation successful.
- Use pattern matching for instanceof in setMessagingConverter() to avoid explicit casting
- Fix constructor parameter indentation to use tabs only (not mixed spaces)
- Address checkstyle violations per reviewer feedback
These changes improve code readability without affecting functionality.
* Fix remaining indentation issues in method parameters
- Fix `toMessagingMessage()` parameter indentation in `BatchMessagingMessageListenerAdapter`
- Fix `toMessage()` parameter indentation in `BatchMessagingMessageConverter`
- Use single tab indentation consistently per Spring code style
Problem:
When using `@KafkaListener(contentTypeConverter = "...")` with a batch listener
(`batch = "true"`), the SmartMessageConverter was not being applied during
message conversion, resulting in ClassCastException when trying to process
messages that required conversion (e.g., byte[] to String).
Root Cause:
The issue stemmed from two problems:
1. `BatchMessagingMessageConverter` default constructor created an instance
without a `RecordMessageConverter` (`this(null)`), preventing proper
per-record conversion within batch processing.
2. `BatchMessagingMessageListenerAdapter` did not override
`setMessagingConverter()` to propagate the SmartMessageConverter from the
annotation configuration to the batch converter's record converter.
Additionally, the initial approach of bypassing the parent's validation in
`setMessagingConverter()` conflicted with the framework's design to prevent
the "paradox of choice" - users should configure converters either via factory
setter OR annotation attribute, not both.
Solution:
1. Changed `BatchMessagingMessageConverter` default constructor to always
create a `MessagingMessageConverter` by default (`this(new MessagingMessageConverter())`).
This ensures batch converters always have a record converter for per-record
conversion within batches, and enables annotation-only configuration without
requiring users to set converters on the factory.
2. Added `setMessagingConverter()` method to `BatchMessagingMessageConverter`
that propagates the SmartMessageConverter to its internal record converter
when it's an instance of `MessagingMessageConverter`.
3. Overrode `setMessagingConverter()` in `BatchMessagingMessageListenerAdapter`
to:
- Call `super.setMessagingConverter(messageConverter)` first, which applies
the same validation as the parent class (`Assert.isTrue(!this.converterSet)`)
to prevent configuration conflicts between factory setter and annotation.
- Propagate the SmartMessageConverter to the batch converter's record converter
for proper message conversion in batch processing.
This approach:
- Respects the framework's validation to prevent the "paradox of choice"
- Ensures both batch and per-record conversion paths work correctly
- Allows users to configure converters via annotation without needing to set
converters on the factory (annotation-only configuration)
- Avoids the complexity and potential issues of cloning converters
- Works for multiple listeners with different contentTypeConverter values
Testing:
Added comprehensive integration test `BatchSmartMessageConverterTests` that:
- Verifies SmartMessageConverter works with batch listeners using
`@KafkaListener(contentTypeConverter = "...")`
- Tests multiple listeners with different converters to ensure isolation
- Uses `ConcurrentKafkaListenerContainerFactory` with batch mode to verify
annotation attributes propagate correctly to the record converter
* Polish documentation and test code
- Improve Javadoc clarity with technical explanation for method constraints
- Remove redundant phrase from `BatchMessagingMessageConverter` documentation
- Generalize test class description for future extensibility
- Clean up test assertions by removing redundant size checks
- Use descriptive variable names in test data
* Fix Javadoc links in `setMessagingConverter` method
- Add proper links for `SmartMessageConverter` and `BatchMessageConverter`
- Use code formatting for `batchMessageConverter` field reference
- Simplify `setBatchMessageConverter` method link format
Signed-off-by: Jujuwryy <georgemahfoud2@gmail.com>
# Conflicts:
# spring-kafka/src/main/java/org/springframework/kafka/support/converter/BatchMessagingMessageConverter.java1 parent 14a82e5 commit a1ec4a0
File tree
3 files changed
+311
-7
lines changed- spring-kafka/src
- main/java/org/springframework/kafka
- listener/adapter
- support/converter
- test/java/org/springframework/kafka/listener
3 files changed
+311
-7
lines changedLines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| |||
107 | 109 | | |
108 | 110 | | |
109 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
110 | 136 | | |
111 | 137 | | |
112 | 138 | | |
| |||
Lines changed: 28 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| 69 | + | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
87 | | - | |
| 89 | + | |
| 90 | + | |
88 | 91 | | |
89 | 92 | | |
90 | | - | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
| |||
136 | 139 | | |
137 | 140 | | |
138 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
139 | 154 | | |
140 | 155 | | |
141 | 156 | | |
| |||
267 | 282 | | |
268 | 283 | | |
269 | 284 | | |
270 | | - | |
| 285 | + | |
271 | 286 | | |
272 | 287 | | |
273 | 288 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
278 | 299 | | |
279 | 300 | | |
280 | 301 | | |
| |||
spring-kafka/src/test/java/org/springframework/kafka/listener/BatchSmartMessageConverterTests.java
Lines changed: 257 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
0 commit comments