@@ -164,7 +164,6 @@ public X509Certificate[] getAcceptedIssuers() {
164
164
private volatile ExecutorService keepAliveThreadExecutor ;
165
165
166
166
private final Lock connectLock = new ReentrantLock ();
167
- private final Lock keepAliveThreadExecutorLock = new ReentrantLock ();
168
167
169
168
/**
170
169
* Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).
@@ -771,51 +770,46 @@ public Thread newThread(Runnable runnable) {
771
770
return newNamedThread (runnable , "blc-keepalive-" + hostname + ":" + port );
772
771
}
773
772
});
774
- try {
775
- keepAliveThreadExecutorLock .lock ();
776
- threadExecutor .submit (new Runnable () {
777
- @ Override
778
- public void run () {
779
- while (!threadExecutor .isShutdown ()) {
773
+ threadExecutor .submit (new Runnable () {
774
+ @ Override
775
+ public void run () {
776
+ while (!threadExecutor .isShutdown ()) {
777
+ try {
778
+ Thread .sleep (keepAliveInterval );
779
+ } catch (InterruptedException e ) {
780
+ // expected in case of disconnect
781
+ }
782
+ if (threadExecutor .isShutdown ()) {
783
+ return ;
784
+ }
785
+ boolean connectionLost = false ;
786
+ if (heartbeatInterval > 0 ) {
787
+ connectionLost = System .currentTimeMillis () - eventLastSeen > keepAliveInterval ;
788
+ } else {
780
789
try {
781
- Thread . sleep ( keepAliveInterval );
782
- } catch (InterruptedException e ) {
783
- // expected in case of disconnect
790
+ channel . write ( new PingCommand () );
791
+ } catch (IOException e ) {
792
+ connectionLost = true ;
784
793
}
785
- if (threadExecutor .isShutdown ()) {
786
- return ;
787
- }
788
- boolean connectionLost = false ;
789
- if (heartbeatInterval > 0 ) {
790
- connectionLost = System .currentTimeMillis () - eventLastSeen > keepAliveInterval ;
791
- } else {
792
- try {
793
- channel .write (new PingCommand ());
794
- } catch (IOException e ) {
795
- connectionLost = true ;
796
- }
794
+ }
795
+ if (connectionLost ) {
796
+ if (logger .isLoggable (Level .INFO )) {
797
+ logger .info ("Trying to restore lost connection to " + hostname + ":" + port );
797
798
}
798
- if (connectionLost ) {
799
- if (logger .isLoggable (Level .INFO )) {
800
- logger .info ("Trying to restore lost connection to " + hostname + ":" + port );
801
- }
802
- try {
803
- terminateConnect ();
804
- connect (connectTimeout );
805
- } catch (Exception ce ) {
806
- if (logger .isLoggable (Level .WARNING )) {
807
- logger .warning ("Failed to restore connection to " + hostname + ":" + port +
808
- ". Next attempt in " + keepAliveInterval + "ms" );
809
- }
799
+ try {
800
+ terminateConnect ();
801
+ connect (connectTimeout );
802
+ } catch (Exception ce ) {
803
+ if (logger .isLoggable (Level .WARNING )) {
804
+ logger .warning ("Failed to restore connection to " + hostname + ":" + port +
805
+ ". Next attempt in " + keepAliveInterval + "ms" );
810
806
}
811
807
}
812
808
}
813
809
}
814
- });
815
- keepAliveThreadExecutor = threadExecutor ;
816
- } finally {
817
- keepAliveThreadExecutorLock .unlock ();
818
- }
810
+ }
811
+ });
812
+ keepAliveThreadExecutor = threadExecutor ;
819
813
}
820
814
821
815
private Thread newNamedThread (Runnable runnable , String threadName ) {
@@ -825,12 +819,7 @@ private Thread newNamedThread(Runnable runnable, String threadName) {
825
819
}
826
820
827
821
boolean isKeepAliveThreadRunning () {
828
- try {
829
- keepAliveThreadExecutorLock .lock ();
830
- return keepAliveThreadExecutor != null && !keepAliveThreadExecutor .isShutdown ();
831
- } finally {
832
- keepAliveThreadExecutorLock .unlock ();
833
- }
822
+ return keepAliveThreadExecutor != null && !keepAliveThreadExecutor .isShutdown ();
834
823
}
835
824
836
825
/**
@@ -1180,19 +1169,14 @@ public void disconnect() throws IOException {
1180
1169
}
1181
1170
1182
1171
private void terminateKeepAliveThread () {
1183
- try {
1184
- keepAliveThreadExecutorLock .lock ();
1185
- ExecutorService keepAliveThreadExecutor = this .keepAliveThreadExecutor ;
1186
- if (keepAliveThreadExecutor == null ) {
1187
- return ;
1188
- }
1189
- keepAliveThreadExecutor .shutdownNow ();
1190
- while (!awaitTerminationInterruptibly (keepAliveThreadExecutor ,
1191
- Long .MAX_VALUE , TimeUnit .NANOSECONDS )) {
1192
- // ignore
1193
- }
1194
- } finally {
1195
- keepAliveThreadExecutorLock .unlock ();
1172
+ ExecutorService keepAliveThreadExecutor = this .keepAliveThreadExecutor ;
1173
+ if (keepAliveThreadExecutor == null ) {
1174
+ return ;
1175
+ }
1176
+ keepAliveThreadExecutor .shutdownNow ();
1177
+ while (!awaitTerminationInterruptibly (keepAliveThreadExecutor ,
1178
+ Long .MAX_VALUE , TimeUnit .NANOSECONDS )) {
1179
+ // ignore
1196
1180
}
1197
1181
}
1198
1182
0 commit comments