java.net.SocketException:cxf中打开的文件太多

时间:2016-05-29 14:31:08

标签: java web-services soap cxf

我有一个soap客户Apache CXF

有时我想连接到服务器时会遇到异常:

javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135) ~[cxf-rt-frontend-jaxws-2.3.3.jar:2.3.3]
    at $Proxy81.transferFunds(Unknown Source) ~[na:na]
Caused by: java.net.SocketException: SocketException invoking http://10.20.112.30:40001/Service/JJMService?wsdl: Too many open files
    at sun.reflect.GeneratedConstructorAccessor418.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_05]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[na:1.7.0_05]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:2107) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:2092) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56) ~[cxf-rt-core-2.3.3.jar:2.3.3]
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:697) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62) ~[cxf-rt-core-2.3.3.jar:2.3.3]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) ~[cxf-api-2.3.3.jar:2.3.3]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516) ~[cxf-rt-core-2.3.3.jar:2.3.3]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313) ~[cxf-rt-core-2.3.3.jar:2.3.3]
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265) ~[cxf-rt-core-2.3.3.jar:2.3.3]
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73) ~[cxf-rt-frontend-simple-2.3.3.jar:2.3.3]
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124) ~[cxf-rt-frontend-jaxws-2.3.3.jar:2.3.3]
    ... 36 common frames omitted
Caused by: java.net.SocketException: Too many open files
    at java.net.Socket.createImpl(Socket.java:447) ~[na:1.7.0_05]
    at java.net.Socket.connect(Socket.java:577) ~[na:1.7.0_05]
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175) ~[na:1.7.0_05]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:378) ~[na:1.7.0_05]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:473) ~[na:1.7.0_05]
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:203) ~[na:1.7.0_05]
    at sun.net.www.http.HttpClient.New(HttpClient.java:290) ~[na:1.7.0_05]
    at sun.net.www.http.HttpClient.New(HttpClient.java:306) ~[na:1.7.0_05]
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995) ~[na:1.7.0_05]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931) ~[na:1.7.0_05]
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849) ~[na:1.7.0_05]
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1090) ~[na:1.7.0_05]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:2003) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1955) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:42) ~[cxf-api-2.3.3.jar:2.3.3]
    at org.apache.cxf.io.AbstractThresholdOutputStream.write(AbstractThresholdOutputStream.java:69) ~[cxf-api-2.3.3.jar:2.3.3]
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:2023) ~[cxf-rt-transports-http-2.3.3.jar:2.3.3]
    ... 45 common frames omitted

我通过以下代码构建代理客户端:

org.apache.cxf.jaxws.JaxWsProxyFactoryBean factoryBean = new org.apache.cxf.jaxws.JaxWsProxyFactoryBean();
factoryBean.setAddress("http://10.20.112.30:40001/Service/JJMService?wsdl");
factoryBean.setServiceClass(Service.class);
Service proxyObject = (Service) factoryBean.create();
HTTPConduit httpConduit = (HTTPConduit)ClientProxy.getClient(proxyObject).getConduit();
httpConduit.getClient().setConnectionTimeout(5000);
httpConduit.getClient().setReceiveTimeout(5000);

此代码调用一次,表示proxyObject是单例。

我有两个问题:

  1. 在服务器或我的客户端发生以上异常?
  2. 这个问题的解决方案是什么?
  3. 编辑:

    使用上面的代码片段我创建了一个客户端实例到Service接口,然后对于调用方法,我只调用Service接口方法,如下所示:

    proxyObject.notifyAccountStatement(acountNumber);
    

    注意调用Service接口是我在instanse上调用并发(proxyObject

    我的Apache CXF版本为: 2.3.3

1 个答案:

答案 0 :(得分:0)

我在Linux中遇到过类似的问题。在linux中,打开的连接数(套接字)受打开文件的系统限制的限制。 详细说明 - https://unix.stackexchange.com/questions/157351/why-are-tcp-ip-sockets-considered-open-files

要解决您的问题,请使用命令&#34; ulimit -n XXXXX&#34;来增加打开文件的系统限制。

例如:

[user@vm-srv02 ~]$ ulimit -n #default value. so few.
1024

修正:

[user@vm-srv02 ~]$ sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"
[user@vm-srv02 ~]$ ulimit -n
65535