@@ -163,6 +163,7 @@ public X509Certificate[] getAcceptedIssuers() {
163
163
private volatile ExecutorService keepAliveThreadExecutor ;
164
164
165
165
private final Lock connectLock = new ReentrantLock ();
166
+ private final Lock keepAliveThreadExecutorLock = new ReentrantLock ();
166
167
167
168
/**
168
169
* Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).
@@ -735,46 +736,51 @@ public Thread newThread(Runnable runnable) {
735
736
return newNamedThread (runnable , "blc-keepalive-" + hostname + ":" + port );
736
737
}
737
738
});
738
- threadExecutor .submit (new Runnable () {
739
- @ Override
740
- public void run () {
741
- while (!threadExecutor .isShutdown ()) {
742
- try {
743
- Thread .sleep (keepAliveInterval );
744
- } catch (InterruptedException e ) {
745
- // expected in case of disconnect
746
- }
747
- if (threadExecutor .isShutdown ()) {
748
- return ;
749
- }
750
- boolean connectionLost = false ;
751
- if (heartbeatInterval > 0 ) {
752
- connectionLost = System .currentTimeMillis () - eventLastSeen > keepAliveInterval ;
753
- } else {
739
+ try {
740
+ keepAliveThreadExecutorLock .lock ();
741
+ threadExecutor .submit (new Runnable () {
742
+ @ Override
743
+ public void run () {
744
+ while (!threadExecutor .isShutdown ()) {
754
745
try {
755
- channel . write ( new PingCommand () );
756
- } catch (IOException e ) {
757
- connectionLost = true ;
746
+ Thread . sleep ( keepAliveInterval );
747
+ } catch (InterruptedException e ) {
748
+ // expected in case of disconnect
758
749
}
759
- }
760
- if (connectionLost ) {
761
- if (logger .isLoggable (Level .INFO )) {
762
- logger .info ("Trying to restore lost connection to " + hostname + ":" + port );
750
+ if (threadExecutor .isShutdown ()) {
751
+ return ;
763
752
}
764
- try {
765
- terminateConnect ();
766
- connect (connectTimeout );
767
- } catch (Exception ce ) {
768
- if (logger .isLoggable (Level .WARNING )) {
769
- logger .warning ("Failed to restore connection to " + hostname + ":" + port +
770
- ". Next attempt in " + keepAliveInterval + "ms" );
753
+ boolean connectionLost = false ;
754
+ if (heartbeatInterval > 0 ) {
755
+ connectionLost = System .currentTimeMillis () - eventLastSeen > keepAliveInterval ;
756
+ } else {
757
+ try {
758
+ channel .write (new PingCommand ());
759
+ } catch (IOException e ) {
760
+ connectionLost = true ;
761
+ }
762
+ }
763
+ if (connectionLost ) {
764
+ if (logger .isLoggable (Level .INFO )) {
765
+ logger .info ("Trying to restore lost connection to " + hostname + ":" + port );
766
+ }
767
+ try {
768
+ terminateConnect ();
769
+ connect (connectTimeout );
770
+ } catch (Exception ce ) {
771
+ if (logger .isLoggable (Level .WARNING )) {
772
+ logger .warning ("Failed to restore connection to " + hostname + ":" + port +
773
+ ". Next attempt in " + keepAliveInterval + "ms" );
774
+ }
771
775
}
772
776
}
773
777
}
774
778
}
775
- }
776
- });
777
- keepAliveThreadExecutor = threadExecutor ;
779
+ });
780
+ keepAliveThreadExecutor = threadExecutor ;
781
+ } finally {
782
+ keepAliveThreadExecutorLock .unlock ();
783
+ }
778
784
}
779
785
780
786
private Thread newNamedThread (Runnable runnable , String threadName ) {
@@ -784,7 +790,12 @@ private Thread newNamedThread(Runnable runnable, String threadName) {
784
790
}
785
791
786
792
boolean isKeepAliveThreadRunning () {
787
- return keepAliveThreadExecutor != null && !keepAliveThreadExecutor .isShutdown ();
793
+ try {
794
+ keepAliveThreadExecutorLock .lock ();
795
+ return keepAliveThreadExecutor != null && !keepAliveThreadExecutor .isShutdown ();
796
+ } finally {
797
+ keepAliveThreadExecutorLock .unlock ();
798
+ }
788
799
}
789
800
790
801
/**
@@ -1134,14 +1145,19 @@ public void disconnect() throws IOException {
1134
1145
}
1135
1146
1136
1147
private void terminateKeepAliveThread () {
1137
- ExecutorService keepAliveThreadExecutor = this .keepAliveThreadExecutor ;
1138
- if (keepAliveThreadExecutor == null ) {
1139
- return ;
1140
- }
1141
- keepAliveThreadExecutor .shutdownNow ();
1142
- while (!awaitTerminationInterruptibly (keepAliveThreadExecutor ,
1143
- Long .MAX_VALUE , TimeUnit .NANOSECONDS )) {
1144
- // ignore
1148
+ try {
1149
+ keepAliveThreadExecutorLock .lock ();
1150
+ ExecutorService keepAliveThreadExecutor = this .keepAliveThreadExecutor ;
1151
+ if (keepAliveThreadExecutor == null ) {
1152
+ return ;
1153
+ }
1154
+ keepAliveThreadExecutor .shutdownNow ();
1155
+ while (!awaitTerminationInterruptibly (keepAliveThreadExecutor ,
1156
+ Long .MAX_VALUE , TimeUnit .NANOSECONDS )) {
1157
+ // ignore
1158
+ }
1159
+ } finally {
1160
+ keepAliveThreadExecutorLock .unlock ();
1145
1161
}
1146
1162
}
1147
1163
0 commit comments