简体   繁体   English

使用Future在App引擎上异步urlfetch Http发布

[英]Async urlfetch Http post on App engine using Future

My goal is to rapidly make posts to a server from appengine(java). 我的目标是从appengine(java)快速向服务器发布帖子。 I am attempting to do this using UrlFetchService.fetchAsync. 我正在尝试使用UrlFetchService.fetchAsync做到这一点。 I have been basing my code after this blog post . 在撰写此博客文章之后,我一直基于我的代码。 I have been able to make the request using the code below, however I get some strange behavior: 我已经可以使用下面的代码发出请求,但是出现一些奇怪的行为:

private void futureRequests() {
    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
    URL url = new URL("https://someserver.com");

    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
    fetchOptions.doNotValidateCertificate();
    fetchOptions.setDeadline(60D);

    ArrayList<Future<HTTPResponse>> asyncResponses = new ArrayList<Future<HTTPResponse>>();

    for (int i = 0; i < postDatas.size(); i++) {

        HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, fetchOptions);
        request.setPayload(postDatas.get(i).getBytes(UTF8));
        HTTPHeader header = new HTTPHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        request.setHeader(header);
        header = new HTTPHeader("Content-Length", Integer.toString(postDatas.get(i).getBytes().length));
        request.setHeader(header);
        header = new HTTPHeader("Authorization", "auth=" + authToken);
        request.setHeader(header);
        Future<HTTPResponse> responseFuture = fetcher.fetchAsync(request);
        asyncResponses.add(responseFuture);
    }

    for (Future<HTTPResponse> future : asyncResponses) {
        HTTPResponse response;
        try {
            response = future.get();
            int responseCode = response.getResponseCode();
            resp.getWriter().println("response: " + responseCode);
            logger.warning("Response: " + responseCode);

        } catch (Exception e) {
        }
    }
}

The strange behavior is that I get duplicate posts on the server, and according to my appstats page I use 10x-20x more urlFetches than what was added with the code above. 奇怪的行为是我在服务器上收到了重复的帖子,并且根据我的appstats页面,我使用的urlFetches比上面的代码添加的多10到20倍。 Below is my appstats screen: 以下是我的appstats屏幕:

Appstats屏幕截图

There are more urlFetch calls that could not fit on the screen. 屏幕上无法容纳更多的urlFetch调用。 It appears that the requests are still completing in a synchronous fashion(circled items), but there are many urlFetches that appear to go on at the same time. 似乎请求仍以同步方式完成(圈出的项目),但是似乎同时进行了许多urlFetches。 My question is how am I getting all this calls to urlFetch when I only had 14 Future ?? 我的问题是,当我只有14个Future时,如何将所有这些调用传递给urlFetch? Could the server be giving an error or 503 and urlFetch retrying until it goes through? 服务器是否会给出错误消息或503和urlFetch重试直到通过? And how can I be getting 2 posts for each request?? 我如何为每个请求获得2个帖子?

I understand that I could use the task queue to do asyc request, however I am dealing with a relatively low number of request(20-100) and the cold start time of ramping up another instance would probably make this not a good option for my situation. 我知道可以使用任务队列执行asyc请求,但是我处理的请求数量相对较少(20-100),启动另一个实例的冷启动时间可能对我来说不是一个好选择情况。 Can anyone explain this behavior or have experience with this? 任何人都可以解释这种行为或对此有经验吗?

这仅仅是我的代码中的一个错误,导致我的应用程序发出了比我想象的更多的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM