@@ -573,14 +573,14 @@ private Object findDeserializerClass(Map<String, Object> props, boolean isValue)
573573 }
574574 }
575575
576- private void subscribeOrAssignTopics (final Consumer <? super K , ? super V > consumer ) {
576+ private void subscribeOrAssignTopics (final Consumer <? super K , ? super V > subscribingConsumer ) {
577577 if (KafkaMessageListenerContainer .this .topicPartitions == null ) {
578578 ConsumerRebalanceListener rebalanceListener = new ListenerConsumerRebalanceListener ();
579579 if (this .containerProperties .getTopicPattern () != null ) {
580- consumer .subscribe (this .containerProperties .getTopicPattern (), rebalanceListener );
580+ subscribingConsumer .subscribe (this .containerProperties .getTopicPattern (), rebalanceListener );
581581 }
582582 else {
583- consumer .subscribe (Arrays .asList (this .containerProperties .getTopics ()), rebalanceListener );
583+ subscribingConsumer .subscribe (Arrays .asList (this .containerProperties .getTopics ()), rebalanceListener );
584584 }
585585 }
586586 else {
@@ -592,7 +592,7 @@ private void subscribeOrAssignTopics(final Consumer<? super K, ? super V> consum
592592 new OffsetMetadata (topicPartition .initialOffset (), topicPartition .isRelativeToCurrent (),
593593 topicPartition .getPosition ()));
594594 }
595- consumer .assign (new ArrayList <>(this .definedPartitions .keySet ()));
595+ subscribingConsumer .assign (new ArrayList <>(this .definedPartitions .keySet ()));
596596 }
597597 }
598598
@@ -695,7 +695,7 @@ public void run() {
695695 try {
696696 pollAndInvoke ();
697697 }
698- catch (WakeupException e ) {
698+ catch (@ SuppressWarnings ( "unused" ) WakeupException e ) {
699699 // Ignore, we're stopping
700700 }
701701 catch (NoOffsetForPartitionException nofpe ) {
@@ -814,7 +814,7 @@ public void wrapUp() {
814814 try {
815815 this .consumer .unsubscribe ();
816816 }
817- catch (WakeupException e ) {
817+ catch (@ SuppressWarnings ( "unused" ) WakeupException e ) {
818818 // No-op. Continue process
819819 }
820820 }
@@ -900,7 +900,7 @@ private void processAck(ConsumerRecord<K, V> record) {
900900 try {
901901 ackImmediate (record );
902902 }
903- catch (WakeupException e ) {
903+ catch (@ SuppressWarnings ( "unused" ) WakeupException e ) {
904904 // ignore - not polling
905905 }
906906 }
@@ -950,6 +950,7 @@ private void invokeBatchListener(final ConsumerRecords<K, V> records) {
950950 @ SuppressWarnings ({ UNCHECKED , RAW_TYPES })
951951 private void invokeBatchListenerInTx (final ConsumerRecords <K , V > records ,
952952 final List <ConsumerRecord <K , V >> recordList ) {
953+
953954 try {
954955 this .transactionTemplate .execute (new TransactionCallbackWithoutResult () {
955956
@@ -972,15 +973,34 @@ public void doInTransactionWithoutResult(TransactionStatus s) {
972973 this .logger .error ("Transaction rolled back" , e );
973974 AfterRollbackProcessor <K , V > afterRollbackProcessorToUse =
974975 (AfterRollbackProcessor <K , V >) getAfterRollbackProcessor ();
975- if (recordList == null ) {
976- afterRollbackProcessorToUse .process (createRecordList (records ), this .consumer , e , false );
976+ if (afterRollbackProcessorToUse .isProcessInTransaction () && this .transactionTemplate != null ) {
977+ this .transactionTemplate .execute (new TransactionCallbackWithoutResult () {
978+
979+ @ Override
980+ protected void doInTransactionWithoutResult (TransactionStatus status ) {
981+ batchAfterRollback (records , recordList , e , afterRollbackProcessorToUse );
982+ }
983+
984+ });
977985 }
978986 else {
979- afterRollbackProcessorToUse . process ( recordList , this . consumer , e , false );
987+ batchAfterRollback ( records , recordList , e , afterRollbackProcessorToUse );
980988 }
981989 }
982990 }
983991
992+ private void batchAfterRollback (final ConsumerRecords <K , V > records ,
993+ final List <ConsumerRecord <K , V >> recordList , RuntimeException e ,
994+ AfterRollbackProcessor <K , V > afterRollbackProcessorToUse ) {
995+
996+ if (recordList == null ) {
997+ afterRollbackProcessorToUse .process (createRecordList (records ), this .consumer , e , false );
998+ }
999+ else {
1000+ afterRollbackProcessorToUse .process (recordList , this .consumer , e , false );
1001+ }
1002+ }
1003+
9841004 private List <ConsumerRecord <K , V >> createRecordList (final ConsumerRecords <K , V > records ) {
9851005 return StreamSupport .stream (records .spliterator (), false )
9861006 .collect (Collectors .toList ());
@@ -1020,7 +1040,7 @@ private RuntimeException doInvokeBatchListener(final ConsumerRecords<K, V> recor
10201040 throw er ;
10211041 }
10221042 }
1023- catch (InterruptedException e ) {
1043+ catch (@ SuppressWarnings ( "unused" ) InterruptedException e ) {
10241044 Thread .currentThread ().interrupt ();
10251045 }
10261046 return null ;
@@ -1101,7 +1121,7 @@ private void invokeRecordListener(final ConsumerRecords<K, V> records) {
11011121 * Invoke the listener with each record in a separate transaction.
11021122 * @param records the records.
11031123 */
1104- @ SuppressWarnings ({ UNCHECKED , RAW_TYPES } )
1124+ @ SuppressWarnings (RAW_TYPES )
11051125 private void invokeRecordListenerInTx (final ConsumerRecords <K , V > records ) {
11061126 Iterator <ConsumerRecord <K , V >> iterator = records .iterator ();
11071127 while (iterator .hasNext ()) {
@@ -1132,20 +1152,40 @@ public void doInTransactionWithoutResult(TransactionStatus s) {
11321152 }
11331153 catch (RuntimeException e ) {
11341154 this .logger .error ("Transaction rolled back" , e );
1135- List <ConsumerRecord <K , V >> unprocessed = new ArrayList <>();
1136- unprocessed .add (record );
1137- while (iterator .hasNext ()) {
1138- unprocessed .add (iterator .next ());
1139- }
1140- ((AfterRollbackProcessor <K , V >) getAfterRollbackProcessor ())
1141- .process (unprocessed , this .consumer , e , true );
1155+ recordAfterRollback (iterator , record , e );
11421156 }
11431157 finally {
11441158 TransactionSupport .clearTransactionIdSuffix ();
11451159 }
11461160 }
11471161 }
11481162
1163+ private void recordAfterRollback (Iterator <ConsumerRecord <K , V >> iterator , final ConsumerRecord <K , V > record ,
1164+ RuntimeException e ) {
1165+
1166+ List <ConsumerRecord <K , V >> unprocessed = new ArrayList <>();
1167+ unprocessed .add (record );
1168+ while (iterator .hasNext ()) {
1169+ unprocessed .add (iterator .next ());
1170+ }
1171+ @ SuppressWarnings (UNCHECKED )
1172+ AfterRollbackProcessor <K , V > afterRollbackProcessorToUse =
1173+ (AfterRollbackProcessor <K , V >) getAfterRollbackProcessor ();
1174+ if (afterRollbackProcessorToUse .isProcessInTransaction () && this .transactionTemplate != null ) {
1175+ this .transactionTemplate .execute (new TransactionCallbackWithoutResult () {
1176+
1177+ @ Override
1178+ protected void doInTransactionWithoutResult (TransactionStatus status ) {
1179+ afterRollbackProcessorToUse .process (unprocessed , ListenerConsumer .this .consumer , e , true );
1180+ }
1181+
1182+ });
1183+ }
1184+ else {
1185+ afterRollbackProcessorToUse .process (unprocessed , this .consumer , e , true );
1186+ }
1187+ }
1188+
11491189 private void doInvokeWithRecords (final ConsumerRecords <K , V > records ) {
11501190 Iterator <ConsumerRecord <K , V >> iterator = records .iterator ();
11511191 while (iterator .hasNext ()) {
@@ -1499,7 +1539,7 @@ private void commitIfNecessary() {
14991539 this .consumer .commitAsync (commits , this .commitCallback );
15001540 }
15011541 }
1502- catch (WakeupException e ) {
1542+ catch (@ SuppressWarnings ( "unused" ) WakeupException e ) {
15031543 // ignore - not polling
15041544 this .logger .debug ("Woken up during commit" );
15051545 }
0 commit comments