简体   繁体   中英

com.google.apphosting.api.ApiProxy$CancelledException: The API call urlfetch.Fetch() was explicitly cancelled. Code 503 App Engine Backend Error

I am using Google App Engine as backend for GCM. It is working fine except below error:

 {  "error": {   "errors": [    {
     "domain": "global",
     "reason": "backendError",
     "message": "com.google.apphosting.api.ApiProxy$CancelledException: The API call urlfetch.Fetch() was explicitly cancelled."    }   ],  
 "code": 503,   "message":
 "com.google.apphosting.api.ApiProxy$CancelledException: The API call
 urlfetch.Fetch() was explicitly cancelled."  } }

To find cause I searched urlfetch.Fetch() method in my entire code, but I dint find it.

Does anyone got this same error before? If so then please tell me how you resolved it?

For more info, I added below code to send push notification for more than 1000 users.

private List<RegistrationRecord> regIdTrim(List<RegistrationRecord> wholeList, final int start) {

        List<RegistrationRecord> parts = wholeList.subList(start,(start+1000)> wholeList.size()? wholeList.size() : start+1000);
        return parts;
    }
    /**
     * Send to the first 1000 devices (You can modify this to send to any number of devices or a specific device)
     *
     * @param message The message to send
     */
    public void sendMessage(@Named("message") String message) throws IOException {

        int count = ofy().load().type(RegistrationRecord.class).count();


        if(count<=1) {
            List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(count).list();
            sendMsg(records,message);
        }else
        {
            int msgsDone=0;
            List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).list();

            do {
                List<RegistrationRecord> regIdsParts = regIdTrim(records, msgsDone);
                msgsDone+=1000;
                sendMsg(regIdsParts,message);
            }while(msgsDone<count);

        }
    }
    private void sendMsg(List<RegistrationRecord> records,@Named("message") String msgToSend) throws IOException {

        if (msgToSend == null || msgToSend.trim().length() == 0) {
            log.warning("Not sending msgToSend because it is empty");
            return;
        }

        Sender sender = new Sender(API_KEY);
       // Message msg = new Message.Builder().addData("msgToSend", msgToSend).build();
        Message msg = new Message.Builder().
                addData("notification_title", msgToSend)
                .addData("description","Check ").build();

        // crop longer messages
        if (msgToSend.length() > 1000) {
            msgToSend = msgToSend.substring(0, 1000) + "[...]";
        }
        for (RegistrationRecord record : records) {
            Result result = sender.send(msg, record.getRegId(), 5);

            if (result.getMessageId() != null) {
                log.info("Message sent to " + record.getRegId());
                String canonicalRegId = result.getCanonicalRegistrationId();
                if (canonicalRegId != null) {
                    // if the regId changed, we have to update the datastore
                    log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
                    record.setRegId(canonicalRegId);
                    ofy().save().entity(record).now();
                }
            } else {
                String error = result.getErrorCodeName();
                if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                    log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
                    // if the device is no longer registered with Gcm, remove it from the datastore
                    ofy().delete().entity(record).now();
                } else {
                    log.warning("Error when sending msgToSend : " + error);
                }
            }
        }
    }

It looks like server is blocking you :/ I believe it has something to do with the 1000 network requests you're shooting at it.

Send the server a list of the 1000 ids you want to push to them, and let the server handle the pushes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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