|
55 | 55 | import org.apache.sshd.common.random.Random; |
56 | 56 | import org.apache.sshd.common.session.ConnectionService; |
57 | 57 | import org.apache.sshd.common.session.ReservedSessionMessagesHandler; |
| 58 | +import org.apache.sshd.common.session.Session; |
58 | 59 | import org.apache.sshd.common.session.SessionDisconnectHandler; |
59 | 60 | import org.apache.sshd.common.session.SessionListener; |
60 | 61 | import org.apache.sshd.common.session.UnknownChannelReferenceHandler; |
@@ -472,31 +473,58 @@ public void removePortForwardingEventListener(PortForwardingEventListener listen |
472 | 473 | } |
473 | 474 |
|
474 | 475 | protected void setupSessionTimeout(AbstractSessionFactory<?, ?> sessionFactory) { |
475 | | - // set up the the session timeout listener and schedule it |
476 | 476 | sessionTimeoutListener = createSessionTimeoutListener(); |
477 | 477 | addSessionListener(sessionTimeoutListener); |
478 | | - |
479 | | - timeoutListenerFuture = getScheduledExecutorService() |
480 | | - .scheduleAtFixedRate(sessionTimeoutListener, 1, 1, TimeUnit.SECONDS); |
481 | 478 | } |
482 | 479 |
|
483 | 480 | protected void removeSessionTimeout(AbstractSessionFactory<?, ?> sessionFactory) { |
484 | 481 | stopSessionTimeoutListener(sessionFactory); |
485 | 482 | } |
486 | 483 |
|
487 | 484 | protected SessionTimeoutListener createSessionTimeoutListener() { |
488 | | - return new SessionTimeoutListener(); |
| 485 | + return new SessionTimeoutListener() { |
| 486 | + |
| 487 | + @Override |
| 488 | + public void sessionCreated(Session session) { |
| 489 | + synchronized (this) { |
| 490 | + super.sessionCreated(session); |
| 491 | + if (!sessions.isEmpty()) { |
| 492 | + ensureTimeoutScheduled(); |
| 493 | + } |
| 494 | + } |
| 495 | + } |
| 496 | + |
| 497 | + @Override |
| 498 | + public void sessionClosed(Session s) { |
| 499 | + synchronized (this) { |
| 500 | + super.sessionClosed(s); |
| 501 | + if (sessions.isEmpty()) { |
| 502 | + cancelSessionTimeout(); |
| 503 | + } |
| 504 | + } |
| 505 | + } |
| 506 | + }; |
489 | 507 | } |
490 | 508 |
|
491 | | - protected void stopSessionTimeoutListener(AbstractSessionFactory<?, ?> sessionFactory) { |
492 | | - // cancel the timeout monitoring task |
| 509 | + private void ensureTimeoutScheduled() { |
| 510 | + if (timeoutListenerFuture == null) { |
| 511 | + timeoutListenerFuture = getScheduledExecutorService().scheduleAtFixedRate(sessionTimeoutListener, 1, 1, |
| 512 | + TimeUnit.SECONDS); |
| 513 | + } |
| 514 | + } |
| 515 | + |
| 516 | + private void cancelSessionTimeout() { |
493 | 517 | if (timeoutListenerFuture != null) { |
494 | 518 | try { |
495 | 519 | timeoutListenerFuture.cancel(true); |
496 | 520 | } finally { |
497 | 521 | timeoutListenerFuture = null; |
498 | 522 | } |
499 | 523 | } |
| 524 | + } |
| 525 | + |
| 526 | + protected void stopSessionTimeoutListener(AbstractSessionFactory<?, ?> sessionFactory) { |
| 527 | + cancelSessionTimeout(); |
500 | 528 |
|
501 | 529 | // remove the sessionTimeoutListener completely; should the SSH server/client be restarted, a new one |
502 | 530 | // will be created. |
|
0 commit comments