[ 
https://issues.apache.org/jira/browse/SSHD-1278?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17566397#comment-17566397
 ] 

Lyor Goldstein commented on SSHD-1278:
--------------------------------------

{quote}
I can not use {{try-with-resource}} block since the logic is not limited in 
{{try-with-resource}} block.
{quote}
Not a problem:
{code:java}
ClientSession session = 
client.connect(...uri...).verify(CONNECT_TIMEOUT).getSession();
try {
      session.addPasswordIdentity(getCurrentTestName()); // or add key identity
      session.auth().verify(AUTH_TIMEOUT);
      handOverTheSession(session);   // whoever takes over the session should 
close it once done
      session = null;    // prevent auto-close at finally clause
} finally {
     if (session != null) {
          session.close()
     }
}
{code}
And you can even do it +asynchronously+ - though more complex:
{code:java}
ConnectFuture future = client.connect(...uri...);
future.addListener(f -> {
    if (f.isConnected()) {
        authenticateSession(f,getSession());    // can do the same aysnchronous 
authentication - make sure to close the session if auth. fails
    } else {
        f.getException();   // the problem
    }
});
{code}

> How to release client connection resources after connection timeout occurs ?
> ----------------------------------------------------------------------------
>
>                 Key: SSHD-1278
>                 URL: https://issues.apache.org/jira/browse/SSHD-1278
>             Project: MINA SSHD
>          Issue Type: Question
>    Affects Versions: 2.8.0
>         Environment: Java SE 8, NetBeans IDE 8.2
>            Reporter: dgü
>            Assignee: Lyor Goldstein
>            Priority: Major
>
> Hello!
> I want to release client connection resources after connection timeout occurs 
> ?
> This is the code:
> {code:java}
> ConnectFuture connectFuture = sshClient.connect(uri.toString());
> try {
>     //connectFuture.isConnected() returns false
>     connectFuture.verify(timeout);
> } catch (IOException e) {
>     //connectFuture.isConnected() returns true
>     connectFuture.cancel();
>     //connectFuture.isCanceled() returns false
>     //connectFuture.isConnected() returns true
>     throw e;
> }
> {code}
> I guess there is a race condition between client connection and 
> {{ConnectFuture#isConnected()}}. 
> For example, this code may be wrong:
> {code:java}
> if (!connectFuture.isConnected()) {
>   //connectFuture.isConnected() may return true
> }
> {code}
> In my case, session is connected after timeout. 
> How can I be sure that client is still not connected and release its 
> resources after connection timeout occurs ?
> Thanks in advance!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to