Axis2创建持久连接

时间:2012-08-30 16:04:00

标签: java wsdl axis2 keep-alive

我想使用.NET制作的web服务。为了生成我的源代码,我使用了基于Web服务的WSDL的Axis2。我没有选择就推出了Axis。

我可以调用webservice的第一个方法。这是Web服务的身份验证方法“Connect”。在此之后,我再次调用一个新方法,但是webservice表明我没有更多连接。 Web服务的支持告诉我,因为在我调用connect方法之后,连接被java关闭。而且,web服务假设我已断开连接并关闭我的会话。

要解决这个问题,我对两种方法的成功调用应该保持实时连接。但是,我不确定Axis2是否提出了这些功能,这是真的吗?

如何保持不同来电之间的联系?

这是我的java代码:

WinbooksWebServiceStub stub = new WinbooksWebServiceStub("http://myUrl/Service.asmx");
Connect c = new Connect();
c.setKey("KEY");
c.setUser("USER");
c.setPass("PSW");

ConnectResponse resp = stub.connect(c);
ConnectParser connecterParser = new ConnectParser();//works and return the Token

HelloWorld h = new HelloWorld();
HelloWorldResponse Hresp = stub.helloWorld(h); //the result tell me that I'm not connected

如果我按照答案,我在我的存根对象上应用方法,我在线程“main”中得到此错误异常

org.apache.axis2.AxisFault: Attempted read on closed stream.
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 
at org.apache.axis2.builder.SOAPBuilder.processDocument(SOAPBuilder.java:64)
at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:179)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:145)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:108)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:67)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:354)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.logidrive.ws.WinbooksWebServiceStub.connect(WinbooksWebServiceStub.java:4251)
at Main.main(Main.java:44) Caused by: java.io.IOException: Attempted read on closed stream.
at org.apache.commons.httpclient.AutoCloseInputStream.isReadAllowed(AutoCloseInputStream.java:183)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at org.apache.axiom.om.util.DetachableInputStream.read(DetachableInputStream.java:147)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at java.io.PushbackInputStream.read(PushbackInputStream.java:169)
at org.apache.axis2.builder.BuilderUtil.getCharSetEncoding(BuilderUtil.java:294)
at org.apache.axis2.builder.SOAPBuilder.processDocument(SOAPBuilder.java:51)

1 个答案:

答案 0 :(得分:1)

你有两个问题。我不确定为什么你没有连接,但你可以为高容量请求做一个http连接池。

以下是一篇博客,介绍了如何执行此操作Axis2 Blog

此外,以下是Here

设置的一些代码

编辑更新代码

import org.apache.axis2.client.Stub;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public static void setContextProperties(Stub stub) {

    // Set the max connections to 20 and the timeout to 20 seconds
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(20);
    params.setMaxTotalConnections(20);
    params.setSoTimeout(20000);
    params.setConnectionTimeout(20000);
    multiThreadedHttpConnectionManager.setParams(params);

    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);

    stub._getServiceClient().getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
}