最大线程问题

时间:2012-10-19 13:27:04

标签: restlet restlet-2.0

首先,我检查了有关此问题的讨论,但无法找到问题的答案,这也是我打开此问题的原因。

我使用restlet 2.0.15设置了一个Web服务。该实现仅适用于服务器。与服务器的连接是通过网页进行的,因此我没有使用ClientResource。

线程池问题耗尽的大部分答案都表明包含了

#exhaust + #release

Web服务的过程可以描述为单个函数。从网页接收GET个请求,查询数据库,以XML格式化结果并返回最终表示。我使用Filter来覆盖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();

这些参数是本论坛讨论的结果,也是我个人为了最大限度地提高效率而进行的修改。

来到应用程序,代码如下:

@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可能存在一些错误,但我下载了稳定版本以验证这不是问题。网络服务每天有大约5000个请求,这并不多。注意:在#release method返回错误和引用警告ServerResource or OriginFilter

中插入("WARNING: A response with a 200 (Ok) status should have an entity (...)")

请指导。 谢谢!

1 个答案:

答案 0 :(得分:1)

通过阅读此site,我所描述的驻留在服务器端的问题已通过将Restlet分发升级到2.1 version来解决。 您需要更改一些代码。您应该查阅相应的migration guide