简体   繁体   中英

Google Cloud Endpoint - How to create handler to call endpoints from cron

UPDATED QUESTION
I can't find any sample or tutorial to call endpoints from cron via handler.
Don't have any experience with Backend services!!

Old description and code
I have a GCE module deployed on AppEngine via Android Studio for my Android App. I am able to create and fetch info via endpoint client stubs in my Android app. For cleaning data on app engine, there is a simple method that compares and delete any data older than 5 days.
This method is running fine if called through java code.

There is an issue with the cron that I have created. I am not sure if it is running at all or calling the right method inside my endpoint class. Need help in running it automatically.

I have different ways to declare method name and path along with url entry in cron, but no success yet.

Other solutions on stackoverflow are not clear to me.

Endpoint class method

@ApiMethod(name = "cron.cleanData", path="cron/cleanData")
    public void cleanData(@Nullable @Named("cursor") String cursorString,
                                @Nullable @Named("count") Integer count) throws NotFoundException {
        System.out.print("cleanData... called");
        CollectionResponse<Data> listData = listData(cursorString, count);
        for (Data data : listData.getItems()) {
            System.out.print("for..");
                if(isOld(data.getTime(), data.getDate())){
                    removeData(data.getId());
                    System.out.print("removed.."+data.getId());
                }
        }
    }

cron.xml inside WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/cleanData</url>
        <description>Clean Storage</description>
        <schedule>every 1 minutes</schedule>
        <timezone>America/Hawaii</timezone>
    </cron>
</cronentries>

web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
   <servlet>
        <servlet-name>SystemServiceServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value><MY_PACKAGE>.MyEndpoint</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>SystemServiceServlet</servlet-name>
        <url-pattern>/_ah/spi/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>ObjectifyFilter</filter-name>
        <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>cron</web-resource-name>
            <url-pattern>/cron/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
</web-app>

You cannot call a Google Cloud Endpoint from a cron job. Instead, you should issue a request to a target that is served by a handler that's specified in your app's configuration file or in a dispatch file. That handler then calls the appropriate endpoint class and method.

You can find more details here .

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