Axis2如何设置连接超时时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Axis2如何设置连接超时时间相关的知识,希望对你有一定的参考价值。
客户端调用webservice服务端的时候,我想设置一下连接超时的时间,客户端代码如下,测了几次,要80多秒才抛连接超时,想自己设一个,15秒左右的,高手指点下怎么弄
RPCServiceClient serviceClient = null;
Map<String, String> map = null;
long begin = 0;
try
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用webservice的url
EndpointReference targetEPR = new EndpointReference(
"-------------url---------------------------------");
options.setTimeOutInMilliSeconds((long)30000);
options.setTo(targetEPR);
// 指定方法的参数值
Object[] opAddEntryArgs = new Object[] "-3e7a6595:134abc78829:-7fbd", 222222, "555555" ;
// 指定方法的返回值的数据类型的class对象
Class[] classes = new Class[] String.class ;
// 指定要调用的方法及wsdl文件的命名空间,对应xml中的targetnamespace值
QName opAddEntry = new QName("----------------------/xsd", "getMessage");
// 调用方法(当有返回值时)
begin = System.currentTimeMillis();
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0].toString());
catch (AxisFault e1)
// TODO Auto-generated catch block
e1.printStackTrace();
finally
long end = System.currentTimeMillis();
System.out.println("-----------------"+(end-begin)+"------------------");
options.setTimeOutInMilliSeconds((long)30000);
这句话根本不起作用,急啊!!!
org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 100 ms
at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346) 参考技术A MultiThreadedHttpConnectionManager conmgr = new MultiThreadedHttpConnectionManager();
conmgr.getParams().setSoTimeout( 10000 ); //10秒
HttpClient httpclient = new HttpClient(conmgr);
....
options.setProperty( HTTPConstants.CACHED_HTTP_CLIENT, httpclient );
......
serviceClient.setOptions(options);
python websockets,如何设置连接超时
【中文标题】python websockets,如何设置连接超时【英文标题】:python websockets, how to setup connect timeout 【发布时间】:2019-01-29 12:25:56 【问题描述】:假设 WebSocket 服务器暂时关闭,它丢弃传入的数据包(而不是拒绝它们)
目前,连接尝试和TimeoutError
之间大约需要 95 秒
我似乎找不到减小该窗口的方法(所以我可以尝试另一个 WebSocket 服务器)
这是我正在运行的演示代码:(刚刚取自official docs
#!/usr/bin/env python
import asyncio
import websockets
import os
import socket
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s [%(name)s.%(funcName)s:%(lineno)d]: %(message)s', datefmt='%m-%d %H:%M:%S', )
host = os.environ.get('SERVER_URL','localhost:9090')
self_id = os.environ.get('SELF_ID',socket.gethostname())
connect_url =f'ws://host/self_id'
logging.info(f'Connect to: connect_url')
async def hello(uri):
logging.info(f'Connecting to uri')
async with websockets.connect(uri, timeout=1, close_timeout=1) as websocket:
logging.info(f"Conected to uri")
async for message in websocket:
await websocket.send(message)
asyncio.get_event_loop().run_until_complete(
hello(connect_url))
【问题讨论】:
【参考方案1】:你可以像这样使用 asyncio 的 wait_for():
import asyncio
from concurrent.futures import TimeoutError as ConnectionTimeoutError
# whatever url is your websocket server
url = 'ws://localhost:9090'
# timeout in seconds
timeout = 10
try:
# make connection attempt
connection = await asyncio.wait_for(websockets.connect(url), timeout)
except ConnectionTimeoutError as e:
# handle error
print('Error connecting.')
它会引发一个<class 'concurrent.futures._base.TimeoutError'>
异常,可以用except ConnectionTimeoutError
块捕获。
在 python3.8 中,它引发了一个 TimeoutError
,可以用 except asyncio.exceptions.TimeoutError
块捕获。
【讨论】:
缺少import asyncio
行,这使它有点不完整,但感谢您的回答:-)
不客气。这是我在实现中使用的,到目前为止效果很好。以上是关于Axis2如何设置连接超时时间的主要内容,如果未能解决你的问题,请参考以下文章
axis2 调用 webService 报超时错误 org.apache.axis2.AxisFault: Connection reset