gpt4 book ai didi

java - ReSTLet 客户端对 ReSTLet 服务器的重复调用挂起

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:03 26 4
gpt4 key购买 nike

我正在使用 ReSTLet 来实现 Web 服务。客户端(也使用 ReSTLet)对服务器进行多次连续调用,但在少数调用成功完成后,进一步的调用挂起服务器,显示消息:

INFO: Stop accepting new connections and transactions. Consider increasing the maximum number of threads.

我试过:

getContext().getParameters().add("maxThreads", "200");

但这并没有帮助。无论如何,客户端似乎应该能够进行无限次调用,而增加 maxThreads 只会增加限制。看起来我没有在每次客户端调用后释放一些资源或断开连接,但我不知道该怎么做。

以下(尽可能小)独立程序演示了该问题。它启动一个简单的服务器,然后客户端多次调用它:

/** You may copy, modify, and re-use this code as you see fit - Jim Irrer */
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.data.Status;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Directory;

public class SimpleServerPut extends Component implements Runnable {
private static final int PORT = 8080;

private static int readToByteArray(InputStream inputStream, byte[] buf) throws IOException {
int length = 0;
int b;
while ((b = inputStream.read()) != -1) {
buf[length++] = (byte)b;
}
return length;
}

@Override
public void run() {
getContext().getParameters().add("maxThreads", "200");

// Create the HTTP server and listen on port PORT
SimpleServerPut simpleServer = new SimpleServerPut();
Server server = new Server(Protocol.HTTP, PORT, simpleServer);
simpleServer.getClients().add(Protocol.FILE);

// Create an application
Application application = new Application(simpleServer.getContext()) {
@Override
public Restlet createRoot() {
return new Directory(getContext(), "C:");
}
};

// Attach the application to the component and start it
simpleServer.getDefaultHost().attach("/stuff/", application);
try {
server.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void handle(Request request, Response response) {
// assume the worst
response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
response.setEntity("No no - Bad client! Only do PUTs.", MediaType.TEXT_PLAIN);

try {
if (request.getMethod() == Method.PUT) {
InputStream inputStream = request.getEntity().getStream();
byte[] buf = new byte[64*1024];
int totalLength = readToByteArray(inputStream, buf);
response.setStatus(Status.SUCCESS_OK);
String msg = "Number of bytes received: " + totalLength;
response.setEntity(msg, MediaType.TEXT_PLAIN);
System.out.println("server: " + msg);
return;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

private static String callServer() throws IOException {
String urlText = "http://localhost:" + PORT + "/";
ClientResource clientResource = new ClientResource(urlText);
clientResource.setReferrerRef(urlText);

byte[] buf = new byte[1000];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte)((int)'a' + (i%26));
}
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
Representation representation = new InputRepresentation(byteArrayInputStream, MediaType.APPLICATION_OCTET_STREAM);
Representation representation2 = clientResource.put(representation);
byte[] responseBuf = new byte[16*1024];
int length = readToByteArray(representation2.getStream(), responseBuf);
Response response = clientResource.getResponse();
Status status = response.getStatus();
return "status: " + status + " message: " + new String(responseBuf, 0, length);
}

// Start server and call it a bunch of times
public static void main(String[] args) throws Exception {
SimpleServerPut simpleServer = new SimpleServerPut();
new Thread(simpleServer).start();
Thread.sleep(200); // cheap trick to make sure that server is running
// make a bunch of client calls
for (int t = 0; t < 100; t++) {
System.out.println("client count: " + (t+1) + " " + callServer());
}
System.exit(0);
}
}

最佳答案

我们只能通过直接停止 ClientResource 的关联客户端(使用 ReSTLet 版本 2.0.15)来解决问题:

Client c = (Client)clientResource.getNext();
try {
c.stop();
} catch (Exception e) {
//handle exception
}

关于java - ReSTLet 客户端对 ReSTLet 服务器的重复调用挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247869/

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