Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum threads issue

To begin with, I checked the discussions regarding this issue and couldn't find an answer to my problem and that's why I'm opening this question.

I've set up a web service using restlet 2.0.15.The implementation is only for the server. The connections to the server are made through a webpage, and therefore I didn't use ClientResource.

Most of the answers to the exhaustion of the thread pool problem suggested the inclusion of

#exhaust + #release

The process of web service can be described as a single function.Receive GET requests from the webpage, query the database, frame the results in XML and return the final representation. I used a Filter to override the beforeHandle and afterHandle.

The code for component creation code:

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();

The parameters are the result of a discussion present in this forum and modification by my part in an attempt to maximize efficiency.

Coming to the Application, the code is as follows:

@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;
}

I used an example Filter found in a discussion here, too. The implementation is as follows:

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 {

        }

    }

}

The responseRepresentation does not have a #release method because it crashes the processes giving the warning WARNING: A response with a 200 (Ok) status should have an entity (...)

The code of the ServerResource implementation is the following:

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();

}

}

I've tried the solutions provided in other threads but none of them seem to work. Firstly, it does not seem that the thread pool is augmented with the parameters set as the warnings state that the thread pool available is 10. As mentioned before, the increase of the maxThreads value only seems to postpone the result.

Example: INFO: Worker service tasks: 0 queued, 10 active, 17 completed, 27 scheduled.

There could be some error concerning the Restlet version, but I downloaded the stable version to verify this was not the issue.The Web Service is having around 5000 requests per day, which is not much.Note: the insertion of the #release method either in the ServerResource or OriginFilter returns error and the referred warning ("WARNING: A response with a 200 (Ok) status should have an entity (...)")

Please guide. Thanks!

like image 296
user1759364 Avatar asked Nov 13 '22 20:11

user1759364


1 Answers

By reading this site the problem residing in the server-side that I described was resolved by upgrading the Restlet distribution to the 2.1 version. You will need to alter some code. You should consult the respective migration guide.

like image 50
user1759364 Avatar answered Jan 04 '23 03:01

user1759364