1
- // Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
1
+ // Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
2
2
//
3
3
// This software, the RabbitMQ Java client library, is triple-licensed under the
4
4
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -73,9 +73,9 @@ public void run() {
73
73
74
74
for (SelectionKey selectionKey : selector .keys ()) {
75
75
SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState ) selectionKey .attachment ();
76
- if (state .getConnection () != null && state .getConnection (). getHeartbeat () > 0 ) {
77
- long now = System .currentTimeMillis ();
78
- if ((now - state .getLastActivity ()) > state .getConnection (). getHeartbeat () * 1000 * 2 ) {
76
+ if (state .getConnection () != null && state .getHeartbeatNanoSeconds () > 0 ) {
77
+ long now = System .nanoTime ();
78
+ if ((now - state .getLastActivity ()) > state .getHeartbeatNanoSeconds () * 2 ) {
79
79
try {
80
80
handleHeartbeatFailure (state );
81
81
} catch (Exception e ) {
@@ -91,7 +91,7 @@ public void run() {
91
91
if (!writeRegistered && registrations .isEmpty () && writeRegistrations .isEmpty ()) {
92
92
// we can block, registrations will call Selector.wakeup()
93
93
select = selector .select (1000 );
94
- if (selector .keys ().size () == 0 ) {
94
+ if (selector .keys ().isEmpty () ) {
95
95
// we haven't been doing anything for a while, shutdown state
96
96
boolean clean = context .cleanUp ();
97
97
if (clean ) {
@@ -135,11 +135,9 @@ public void run() {
135
135
if (!key .isValid ()) {
136
136
continue ;
137
137
}
138
-
139
- if (key .isReadable ()) {
140
- final SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState ) key .attachment ();
141
-
142
- try {
138
+ final SocketChannelFrameHandlerState state = (SocketChannelFrameHandlerState ) key .attachment ();
139
+ try {
140
+ if (key .isReadable ()) {
143
141
if (!state .getChannel ().isOpen ()) {
144
142
key .cancel ();
145
143
continue ;
@@ -175,14 +173,14 @@ public void run() {
175
173
}
176
174
}
177
175
178
- state .setLastActivity (System .currentTimeMillis ());
179
- } catch (final Exception e ) {
180
- LOGGER .warn ("Error during reading frames" , e );
181
- handleIoError (state , e );
182
- key .cancel ();
183
- } finally {
184
- buffer .clear ();
176
+ state .setLastActivity (System .nanoTime ());
185
177
}
178
+ } catch (final Exception e ) {
179
+ LOGGER .warn ("Error during reading frames" , e );
180
+ handleIoError (state , e );
181
+ key .cancel ();
182
+ } finally {
183
+ buffer .clear ();
186
184
}
187
185
}
188
186
}
@@ -222,9 +220,8 @@ public void run() {
222
220
continue ;
223
221
}
224
222
225
- if (key .isWritable ()) {
226
- boolean cancelKey = true ;
227
- try {
223
+ try {
224
+ if (key .isWritable ()) {
228
225
if (!state .getChannel ().isOpen ()) {
229
226
key .cancel ();
230
227
continue ;
@@ -243,17 +240,12 @@ public void run() {
243
240
written ++;
244
241
}
245
242
outputStream .flush ();
246
- if (!state .getWriteQueue ().isEmpty ()) {
247
- cancelKey = true ;
248
- }
249
- } catch (Exception e ) {
250
- handleIoError (state , e );
251
- } finally {
252
- state .endWriteSequence ();
253
- if (cancelKey ) {
254
- key .cancel ();
255
- }
256
243
}
244
+ } catch (Exception e ) {
245
+ handleIoError (state , e );
246
+ } finally {
247
+ state .endWriteSequence ();
248
+ key .cancel ();
257
249
}
258
250
}
259
251
}
@@ -269,7 +261,7 @@ protected void handleIoError(SocketChannelFrameHandlerState state, Throwable ex)
269
261
} else {
270
262
try {
271
263
state .close ();
272
- } catch (IOException e ) {
264
+ } catch (IOException ignored ) {
273
265
274
266
}
275
267
}
@@ -284,7 +276,7 @@ protected void handleHeartbeatFailure(SocketChannelFrameHandlerState state) {
284
276
} else {
285
277
try {
286
278
state .close ();
287
- } catch (IOException e ) {
279
+ } catch (IOException ignored ) {
288
280
289
281
}
290
282
}
0 commit comments