在异常中发送2000异步休息请求

时间:2017-12-31 12:53:05

标签: java android rest

我尝试发送许多休息请求(例如2k或更多)。

TestCase使用http://httpbin.org/post

创建OkHttpClient的方法:

private static OkHttpClient getDefaultHttpClient() {
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost(5);
    dispatcher.setMaxRequests(10);
    return new OkHttpClient().newBuilder()
            .build();
}

接下来,我使用此客户端发送休息请求:

public void sentRest(View view) {
    Log.d(TAG,"sentRest start");
    OkHttpClient client = getDefaultHttpClient();

        for (int i=0;i<1000;i++) {
        MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");
        final Request request = new Request();

        String requestJson = "{Accept=[application/json], Accept-Encoding=[gzip], Content-Type=[application/json], Authorization=[Bearer AbCdEf123456], Content-Length=[103]} ";
            request.setBody(requestJson);
            request.setNumber(i);
            request.setUrl("http://httpbin.org/post");
            RequestBody requestBody = RequestBody.create(MEDIA_TYPE_MARKDOWN, requestJson);


        Log.d(TAG, "getMaxRequests " + client.dispatcher().getMaxRequests()
                + " per Host " + client.dispatcher().getMaxRequestsPerHost()
                + " queuedCallsCount " + client.dispatcher().queuedCallsCount()
                + " request.url " + request.getUrl()
                + " request.no " + request.getNumber());

        Log.d(TAG, "requestBody " + requestBody);
        okhttp3.Request requestOkHttp = new okhttp3.Request.Builder()
                .url(request.getUrl())
                .post(requestBody)
                .build();

            client.newCall(requestOkHttp).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "onFailure: " + e.getMessage()
                            + " url " + request.getUrl() + " no " + request.getNumber()
                            + (e.getCause() != null ? (" cause " + e.getCause().toString()) : "" ));
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.d(TAG, "OnResponse: "
                            + " resp " + response.message()
                            + " " + response.isSuccessful()
                            + " " + response.code()
                            + " " + response.sentRequestAtMillis()
                            + " " + request.getNumber());
                    if (!response.isSuccessful())
                        Log.d(TAG, "onResponse notSuccessful: " + response.body().string() + " no " + request.getNumber());


                }
            });
        }
    }

其余的一些我发送好,但我也得到例外:

12-31 13:36:05.402 24514-5388/rbinek.asyncresttest E/SentRest: onFailure: Failed to connect to httpbin.org/54.243.173.79:80 url http://httpbin.org/post no 545 cause java.net.ConnectException: failed to connect to httpbin.org/54.243.173.79 (port 80) after 10000ms: connect failed: EMFILE (Too many open files)
12-31 13:36:05.522 24514-5378/rbinek.asyncresttest E/SentRest: onFailure: Failed to connect to httpbin.org/54.243.173.79:80 url http://httpbin.org/post no 547 cause java.net.ConnectException: failed to connect to httpbin.org/54.243.173.79 (port 80) after 10000ms: connect failed: EMFILE (Too many open files)
12-31 13:36:05.532 24514-5388/rbinek.asyncresttest E/SentRest: onFailure: Unable to resolve host "httpbin.org": No address associated with hostname url http://httpbin.org/post no 552 cause android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-31 13:36:05.542 24514-5375/rbinek.asyncresttest E/SentRest: onFailure: Failed to connect to httpbin.org/54.243.173.79:80 url http://httpbin.org/post no 548 cause java.net.ConnectException: failed to connect to httpbin.org/54.243.173.79 (port 80) after 10000ms: connect failed: EMFILE (Too many open files)
12-31 13:36:05.552 24514-5378/rbinek.asyncresttest E/SentRest: onFailure: Unable to resolve host "httpbin.org": No address associated with hostname url http://httpbin.org/post no 553
12-31 13:36:05.562 24514-5387/rbinek.asyncresttest E/SentRest: onFailure: Failed to connect to httpbin.org/54.243.173.79:80 url http://httpbin.org/post no 549 cause java.net.ConnectException: failed to connect to httpbin.org/54.243.173.79 (port 80) after 10000ms: connect failed: EMFILE (Too many open files)
12-31 13:36:05.562 24514-5369/rbinek.asyncresttest E/SentRest: onFailure: Failed to connect to httpbin.org/54.243.173.79:80 url http://httpbin.org/post no 550 cause java.net.ConnectException: failed to connect to httpbin.org/54.243.173.79 (port 80) after 10000ms: connect failed: EMFILE (Too many open files)

正确发送的休息次数不同,但每次测试都会出现错误。 这是三星Galaxy android 5.5.1的测试 有帮助吗? :)

0 个答案:

没有答案