gpt4 book ai didi

java - Oracle java函数超时

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:06 25 4
gpt4 key购买 nike

我的 oracle 数据库中有下一个 java 函数:

public String SnConectividad(String pServidor, int pPuerto)
{
try{

Socket s = new Socket();

InetSocketAddress addr = new InetSocketAddress(pServidor,pPuerto);

s.connect(addr, 7000);

if (s.isConnected())

return "S";

}catch(Exception e){}


return "N";

}

此函数在发送邮件之前验证互联网连接...问题是指令超时(7000 mils)“s.connect(addr, 7000);”不起作用..

所以,调用这个函数需要很长时间才能响应..

如果我在 Windows 上的“netbeans”上执行相同的函数...超时工作正常..

任何想法..'???

最佳答案

ThreadExecutorService一起使用:

   String flag="S";
Runnable thrd=new Thread(){ @Override
public void run() {
try{

Socket s = new Socket();

InetSocketAddress addr = new InetSocketAddress(pServidor,pPuerto);

s.connect(addr);

if (s.isConnected())

flag="S";
return;

}catch(Exception e){}


flag="N";
}};
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(thrd);
executor.shutdown();
try {
future.get(700, TimeUnit.SECONDS);
}
catch (Exception e) {
// Handle the exceptions
}
if (!executor.isTerminated())
executor.shutdownNow();

关于java - Oracle java函数超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com