从App Engine灵活到Firebase FCM的API调用失败

时间:2017-12-30 17:56:01

标签: google-app-engine firebase google-api app-engine-flexible

API调用,例如在从<env>vm</env> appengine-web.xml切换到之前,Firebase https://fcm.googleapis.com/fcm/send工作正常 env: flex app.yaml灵活。

使用mvn appengine:deploy进行部署时,一切都很好,但是当API调用被触发时(之前有效),我会得到以下异常:

Exception in thread "Thread-14" com.google.apphosting.api.ApiProxy$CallNotFoundException: 
Can't make API call urlfetch.Fetch in a thread that is neither the original request thread nor a thread created by ThreadManager 
at com.google.apphosting.api.ApiProxy$CallNotFoundException.foreignThread(ApiProxy.java:800) 
at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:112) 
at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:40) 
at com.google.api.client.extensions.appengine.http.UrlFetchRequest.execute(UrlFetchRequest.java:74) 
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981) 
at com.qweez.flexenv.service.GameCounterService.run(GameCounterService.java:80)

在调用下面的request.execute()时崩溃:

import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

// stuff

   try {
           HttpRequest request = HttpRequestUtil.getHttpRequestFactory().buildPostRequest(new GenericUrl(GlobalConfig.MESSAGING_SERVER_URL), ByteArrayContent.fromString("application/json", requestBody));
           HttpResponse response = request.execute();


                        //logger.warning(LOG_TAG+" ++++++ response status=" + response.getContent());    
                    } catch (IOException e) {
                        logger.warning(LOG_TAG+" sent messge to topic error: " + e.getMessage());
                    }

帮助程序类httpRequestUtil看起来像这样

import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import java.io.IOException;

public class HttpRequestUtil {
    private static HttpTransport httpTransport;
    private static HttpRequestFactory requestFactory;
    static {
        httpTransport = UrlFetchTransport.getDefaultInstance();
        requestFactory = httpTransport.createRequestFactory(new HttpRequestInitializer() {
            @Override
            public void initialize(com.google.api.client.http.HttpRequest httpRequest) throws IOException {
                HttpHeaders hh = new HttpHeaders();

                hh.setAuthorization(GlobalConfig.SERVER_KEY);
                hh.setContentType("application/json");
                httpRequest.setHeaders(hh);
            }
        });
    }
    public static HttpRequestFactory getHttpRequestFactory(){
        return requestFactory;
    }

现在有什么东西被弃用吗? 或者现在的问题是什么?

任何帮助非常感谢。谢谢!

0 个答案:

没有答案