gpt4 book ai didi

java - Axis2创建持久连接

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

我想使用 .NET 中创建的 Web 服务。为了生成源代码,我使用了基于 Web 服务 WSDL 的 Axis2。我启动了没有选项的 Axis。

我可以调用网络服务的第一个方法。这是Web服务的验证方法“Connect”。之后,我再次调用一个新方法,但网络服务表明我不再连接。 Web服务的支持告诉我这是因为在我调用connect方法后,连接被java关闭了。并且,网络服务假设我已断开连接并关闭我的 session 。

为了解决这个问题,我对两个方法的成功调用应该保持实时连接。但是,我不确定 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

如果我按照答案,在我的 stub 对象上应用方法,我会收到此错误 Exception in thread "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)

最佳答案

您有两个问题。我不知道为什么你没有连接,但你可以为大容量请求创建一个 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);
}

关于java - Axis2创建持久连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201045/

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