How to run Google app engine endpoint methods from

2020-08-01 06:51发布

问题:

I have followed all the configuration steps for my WarmupServlet in my app engine project and I see it run at startup, but still I see my first endpoint call as a loading request which takes up to 25 seconds which is absolutely unacceptable. I need to be able to warm up each endpoint individually so that there will be no loading requests. (Apparently just setting up a warmp-up servlet is not enough.) So, my question is, how can I call a method in endpoints so that the endpoint is properly warmed up to serve from my WarmupServlet? I tried below with no success:

MyEndpoint me = new MyEndpoint();
me.getMyEntity(1L);

where

@ApiMethod(name = "getMyEntity")
public MyEntity getMyEntity(@Named("id") Long id) {
    EntityManager mgr = getEntityManager();
    MyEntity myEntity = null;
    try {
        myEntity = mgr.find(MyEntity.class, id);
    } finally {
        mgr.close();
    }
    return myEntity;
}

回答1:

After adding the client endpoints jar file as a library, this properly warms up MyEndpoint from the Java backend:

NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

Myendpoint.Builder endpointBuilder = new Myendpoint.Builder(
          HTTP_TRANSPORT,
          JSON_FACTORY,
          null);

endpointBuilder.setApplicationName("My App");
Myendpoint endpoint = endpointBuilder.build();
endpoint.getMyEntity(1L).execute();