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.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);
}
}
Removing super.doGet should fix this issue. The default implementation of servlet methods is to return a method not allowed error (405).
According to the Article Scheduled Tasks With 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.
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.