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

时间:2021-04-14 23:09:38

In my app engine app, i want to update datstore entites from cron service, which will use endpoint method call to update data. But whenever cron job is executed, it returns HTTP 405 status code. I am not getting where i am going wrong. If anybody has any idea regarding then please help me to solve this problem. Thank you

在我的应用程序引擎应用程序中,我想从cron服务更新数据库entites,它将使用端点方法调用来更新数据。但是每当执行cron作业时,它都会返回HTTP 405状态代码。我没有得到我错的地方。如果有人对此有任何想法,请帮我解决这个问题。谢谢

cron.xml

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

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 个解决方案

#1


1  

Removing super.doGet should fix this issue. The default implementation of servlet methods is to return a method not allowed error (405).

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

#2


0  

According to the Article Scheduled Tasks With Cron for Java

根据Cron for Java的文章计划任务

Calling Google Cloud Endpoints

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.

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

#1


1  

Removing super.doGet should fix this issue. The default implementation of servlet methods is to return a method not allowed error (405).

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

#2


0  

According to the Article Scheduled Tasks With Cron for Java

根据Cron for Java的文章计划任务

Calling Google Cloud Endpoints

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.

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