JSch会话超时限制

编程入门 行业动态 更新时间:2024-10-28 19:27:34
本文介绍了JSch会话超时限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用JSch 0.1.50为我的CI Jenkins插件建立与远程服务器的连接。让我们假设我在这里使用 session.connect(60000); 超时60秒:

I'm using JSch 0.1.50 to set up a connection to the remote server for my CI Jenkins plugin. Let's assume I'm trying to use here session.connect(60000); for the timeout 60 sec:

Session session = null; try { JSch jsch = new JSch(); if (rsaIdentity != null && !rsaIdentity.equals("")) { jsch.addIdentity(rsaIdentity.trim()); } session = jsch.getSession(serverLogin, serverHost, Integer.parseInt(serverPort)); session.setPassword(getDescriptor().getOpenPassword(encryptedPasswordString)); session.setConfig("StrictHostKeyChecking", "no"); // not use RSA key int timeOut = Integer.parseInt(getDescriptor().getConnectionTimeOut()); session.connect(60000); } catch (SocketTimeoutException e) { logger.error(e.getMessage()); return false; } catch (JSchException e) { logger.error(e.getMessage()); return false; }

但实际上在连接到相当慢的服务器期间执行此代码期间我每次约20秒内面临超时异常:

But in fact during the execution of this code during the connection to pretty slow sever I'm facing the timeout Exception in approximately 20 seconds every time:

2016-01-25 13:15:55.982 [INFO] Connecting to server: devsrv26:22 as [user] ... 2016-01-25 13:16:16.991 [ERROR] java.ConnectException: Connection timed out: connect 2016-01-25 13:16:16.992 com.jcraft.jsch.JSchException: java.ConnectException: Connection timed out: connect 2016-01-25 13:16:16.992 at com.jcraft.jsch.Util.createSocket(Util.java:389) 2016-01-25 13:16:16.993 at com.jcraft.jsch.Session.connect(Session.java:215) 2016-01-25 13:16:16.993 at com.mycomp.jenkins.MyPlugin.perform(MyPlugin.java:225)

76991-55982 = 21008毫秒

76991-55982=21008 msec

有谁知道这20秒的原因是什么超时?

Does anyone know what is the reason for this 20 seconds timeout?

解决方案

如果你检查如何实现 Util.createSocket ,你会看到超时仅定义连接的上限,而不是下限,因为 timeout 奇怪地没有传递给基础 Socket 。

If you check how the Util.createSocket is implemented, you will see that the timeout defines an upper limit of the connection only, not a lower limit, because the timeout is strangely not passed to an underlying Socket.

这20秒可能是操作系统级别的默认限制。

Those 20 seconds is probably an OS-level default limit.

To覆盖它,尝试实施 SocketFactory 并使用 Session.setSocketFactory 。

To override it, try implementing the SocketFactory and attach it to the session using the Session.setSocketFactory.

在工厂中使用 Socket.connect(SocketAddress endpoint,int timeout) 。

In the factory use the Socket.connect(SocketAddress endpoint, int timeout).

类似:

public class SocketFactoryWithTimeout implements SocketFactory { public Socket createSocket(String host, int port) throws IOException, UnknownHostException { socket=new Socket(); int timeout = 60000; socket.connect(new InetSocketAddress(host, port), timeout); return socket; } public InputStream getInputStream(Socket socket) throws IOException { return socket.getInputStream(); } public OutputStream getOutputStream(Socket socket) throws IOException { return socket.getOutputStream(); } }

更多推荐

JSch会话超时限制

本文发布于:2023-08-01 18:46:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!