首先,我检查了有关此问题的讨论,但找不到我的问题的答案,这就是我打开这个问题的原因。
我已经使用 restlet 2.0.15 设置了一个 Web 服务。该实现仅适用于服务器。与服务器的连接是通过网页进行的,因此我没有使用 ClientResource。
大多数关于线程池耗尽问题的答案都建议包含
#exhaust + #release
Web服务的过程可以描述为一个单一的功能GET
。接收来自网页的请求,查询数据库,将结果框在XML中并返回最终表示。我使用过滤器来覆盖 beforeHandle 和 afterHandle。
组件创建代码的代码:
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8188);
component.getContext().getParameters().add("maxThreads", "512");
component.getContext().getParameters().add("minThreads", "100");
component.getContext().getParameters().add("lowThreads", "145");
component.getContext().getParameters().add("maxQueued", "100");
component.getContext().getParameters().add("maxTotalConnections", "100");
component.getContext().getParameters().add("maxIoIdleTimeMs", "100");
component.getDefaultHost().attach("/orcamento2013", new ServerApp());
component.start();
这些参数是本论坛中讨论的结果,并由我修改以试图最大限度地提高效率。
来到Application,代码如下:
@Override
public synchronized Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attach("/{taxes}", ServerImpl.class);
//router.attach("/acores/{taxes}", ServerImplAcores.class);
System.out.println(router.getRoutes().size());
OriginFilter originFilter = new OriginFilter(getContext());
originFilter.setNext(router);
return originFilter;
}
我也使用了在此处的讨论中找到的示例过滤器。实现如下:
public OriginFilter(Context context) {
super(context);
}
@Override
protected int beforeHandle(Request request, Response response) {
if (Method.OPTIONS.equals(request.getMethod())) {
Form requestHeaders = (Form) request.getAttributes().get("org.restlet.http.headers");
String origin = requestHeaders.getFirstValue("Origin", true);
Form responseHeaders = (Form) response.getAttributes().get("org.restlet.http.headers");
if (responseHeaders == null) {
responseHeaders = new Form();
response.getAttributes().put("org.restlet.http.headers", responseHeaders);
responseHeaders.add("Access-Control-Allow-Origin", origin);
responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,DELETE");
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
responseHeaders.add("Access-Control-Allow-Credentials", "true");
response.setEntity(new EmptyRepresentation());
return SKIP;
}
}
return super.beforeHandle(request, response);
}
@Override
protected void afterHandle(Request request, Response response) {
if (!Method.OPTIONS.equals(request.getMethod())) {
Form requestHeaders = (Form) request.getAttributes().get("org.restlet.http.headers");
String origin = requestHeaders.getFirstValue("Origin", true);
Form responseHeaders = (Form) response.getAttributes().get("org.restlet.http.headers");
if (responseHeaders == null) {
responseHeaders = new Form();
response.getAttributes().put("org.restlet.http.headers", responseHeaders);
responseHeaders.add("Access-Control-Allow-Origin", origin);
responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,DELETE"); //
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
responseHeaders.add("Access-Control-Allow-Credentials", "true");
}
}
super.afterHandle(request, response);
Representation requestRepresentation = request.getEntity();
if (requestRepresentation != null) {
try {
requestRepresentation.exhaust();
} catch (IOException e) {
// handle exception
}
requestRepresentation.release();
}
Representation responseRepresentation = response.getEntity();
if(responseRepresentation != null) {
try {
responseRepresentation.exhaust();
} catch (IOException ex) {
Logger.getLogger(OriginFilter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
}
responseRepresentation
没有,因为#release method
它使进程崩溃并发出警告WARNING: A response with a 200 (Ok) status should have an entity (...)
ServerResource 实现的代码如下:
public class ServerImpl extends ServerResource {
String itemName;
@Override
protected void doInit() throws ResourceException {
this.itemName = (String) getRequest().getAttributes().get("taxes");
}
@Get("xml")
public Representation makeItWork() throws SAXException, IOException {
DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML);
DAL dal = new DAL();
String ip = getRequest().getCurrent().getClientInfo().getAddress();
System.out.println(itemName);
double tax = Double.parseDouble(itemName);
Document myXML = Auxiliar.getMyXML(tax, dal, ip);
myXML.normalizeDocument();
representation.setDocument(myXML);
return representation;
}
@Override
protected void doRelease() throws ResourceException {
super.doRelease();
}
}
我已经尝试过其他线程中提供的解决方案,但它们似乎都不起作用。首先,似乎没有使用设置为warnings state that the thread pool available is 10
. 如前所述,maxThreads
价值的增加似乎只是推迟了结果。
Example: INFO: Worker service tasks: 0 queued, 10 active, 17 completed, 27 scheduled.
可能有一些错误Restlet version
,但我下载了稳定版本以验证这不是问题。Web 服务每天有大约 5000 个请求,这并不多。注意:#release method
在ServerResource or OriginFilter
返回错误和引用的警告中插入("WARNING: A response with a 200 (Ok) status should have an entity (...)")
请指导。谢谢!