1
1
/*
2
- * Copyright 2016-2022 the original author or authors.
2
+ * Copyright 2016-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -751,6 +751,10 @@ private Producer<K, V> doCreateProducer(@Nullable String txIdPrefix) {
751
751
return getOrCreateThreadBoundProducer ();
752
752
}
753
753
synchronized (this ) {
754
+ if (this .producer != null && this .producer .closed ) {
755
+ this .producer .closeDelegate (this .physicalCloseTimeout , this .listeners );
756
+ this .producer = null ;
757
+ }
754
758
if (this .producer != null && expire (this .producer )) {
755
759
this .producer = null ;
756
760
}
@@ -765,7 +769,7 @@ private Producer<K, V> doCreateProducer(@Nullable String txIdPrefix) {
765
769
766
770
private Producer <K , V > getOrCreateThreadBoundProducer () {
767
771
CloseSafeProducer <K , V > tlProducer = this .threadBoundProducers .get ();
768
- if (tlProducer != null && (this .epoch .get () != tlProducer .epoch || expire (tlProducer ))) {
772
+ if (tlProducer != null && (tlProducer . closed || this .epoch .get () != tlProducer .epoch || expire (tlProducer ))) {
769
773
closeThreadBoundProducer ();
770
774
tlProducer = null ;
771
775
}
@@ -834,21 +838,11 @@ private boolean removeConsumerProducer(CloseSafeProducer<K, V> producerToRemove,
834
838
* Remove the single shared producer and a thread-bound instance if present.
835
839
* @param producerToRemove the producer.
836
840
* @param timeout the close timeout.
837
- * @return always true.
841
+ * @return true if the producer was closed .
838
842
* @since 2.2.13
839
843
*/
840
- protected final synchronized boolean removeProducer (CloseSafeProducer <K , V > producerToRemove , Duration timeout ) {
841
- if (producerToRemove .closed ) {
842
- if (producerToRemove .equals (this .producer )) {
843
- this .producer = null ;
844
- producerToRemove .closeDelegate (timeout , this .listeners );
845
- }
846
- this .threadBoundProducers .remove ();
847
- return true ;
848
- }
849
- else {
850
- return false ;
851
- }
844
+ protected final boolean removeProducer (CloseSafeProducer <K , V > producerToRemove , Duration timeout ) {
845
+ return producerToRemove .closed ;
852
846
}
853
847
854
848
/**
@@ -1204,7 +1198,12 @@ public void close(@Nullable Duration timeout) {
1204
1198
}
1205
1199
1206
1200
void closeDelegate (Duration timeout , List <Listener <K , V >> listeners ) {
1207
- this .delegate .close (timeout == null ? this .closeTimeout : timeout );
1201
+ try {
1202
+ this .delegate .close (timeout == null ? this .closeTimeout : timeout );
1203
+ }
1204
+ catch (Exception ex ) {
1205
+ LOGGER .warn (ex , () -> "Failed to close " + this .delegate );
1206
+ }
1208
1207
listeners .forEach (listener -> listener .producerRemoved (this .clientId , this ));
1209
1208
this .closed = true ;
1210
1209
}
0 commit comments