从Cron Job App Engine调用端点类 - Java

时间:2015-04-27 07:51:21

标签: java google-app-engine cron google-cloud-endpoints

在我的应用引擎应用中,我想更新来自cron服务的数据库entites,它将使用端点方法调用来更新数据。但是只要执行cron job,它就会返回HTTP 405 status code。我没有得到我错的地方。如果有人对此有任何想法,请帮我解决这个问题。 谢谢

cron.xml

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/cron/gaevalidatecronjob</url>
        <description>Cron Job that autoreset validity of poster.</description>
        <schedule>every day 00:00</schedule>
        <timezone>Asia/Kolkata</timezone>
    </cron>
</cronentries>

的web.xml

    <servlet>
            <servlet-name>ValidityCheckerCron</servlet-name>
            <servlet-class>com.jobaka.dekhbhai.ValidityCheckerCron</servlet-class>
   </servlet>
    <servlet-mapping>
            <servlet-name>ValidityCheckerCron</servlet-name>
            <url-pattern>/cron/gaevalidatecronjob</url-pattern>
    </servlet-mapping>

@SuppressWarnings("serial")
public class ValidityCheckerCron extends HttpServlet {

    private static final Logger _logger = Logger
            .getLogger(ValidityCheckerCron.class.getSimpleName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        super.doGet(req, resp);

        try {

            _logger.log(Level.INFO, "Cron Job has been executed");

            PosterMasterEndpoint endpoint = new PosterMasterEndpoint();
            CollectionResponse<PosterMaster> response = endpoint
                    .getStarRatedPoster(null, null, null);

            if (response != null && response.getItems() != null
                    && response.getItems().size() > 0) {

                Collection<PosterMaster> collResult = response.getItems();
                ArrayList<PosterMaster> lstResult = new ArrayList<PosterMaster>(
                        collResult);
                Date today = new Date();

                for (PosterMaster posterMaster : lstResult) {

                    Date validityDate = posterMaster.getValidityDate();
                    Calendar c = Calendar.getInstance();
                    c.setTime(validityDate);
                    c.add(Calendar.DATE, posterMaster.getValidity());
                    validityDate = c.getTime();

                    if (validityDate.getTime() < today.getTime()) {

                        posterMaster.setValidity(0);
                        posterMaster.setStarRated(false);
                        endpoint.updatePosterMaster(posterMaster);

                    }

                    _logger.log(Level.INFO, "Cron Job has been executed for = "
                            + posterMaster.getPosterUrl());
                }

            }

        } catch (Exception ex) {
            _logger.log(Level.INFO, "Problem in ValidityCheckerCron");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
        doGet(req, resp);
    }
}

2 个答案:

答案 0 :(得分:1)

删除super.doGet应解决此问题。 servlet方法的默认实现是返回一个不允许的方法错误(405)。

答案 1 :(得分:0)

根据文章Scheduled Tasks With Cron for Java

  

调用Google Cloud端点

     

您无法通过cron作业调用Google Cloud Endpoint。相反,你   应该向由处理程序提供服务的目标发出请求   在应用程序的配置文件或调度文件中指定。那   然后,handler调用适当的端点类和方法。