Cron job on Google App Engine for Java
In this tutorial, we will show you how to create a scheduler task or cron job on Google App Engine, for Java. In GAE, cron job uses HTTP GET request to call an URL, the duration of the cron job running time is limited to run up to 10 minutes only, if excess, GAE kill your job.
To create a cron job on GAE, just define cron jobs in a file “cron.xml“, and put in the “WEB-INF” folder.
Cron Job Tutorial
Now, we will create a cron job on GAE, and schedule call an URL from Spring MVC REST example.
- Google App Engine Java SDK 1.6.3.1
- Spring 3.1.1
- JDK 1.6
- Eclipse 3.7 + Google Plugin for Eclipse
1. Spring Controller
A simple Spring REST controller.
package com.mkyong.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/cron")
public class CronController {
static int total = 0;
@RequestMapping(value="/count", method = RequestMethod.GET)
public String getCount(ModelMap model) {
model.addAttribute("total", total);
return "list";
}
//cron job running this
@RequestMapping(value="/addCount/{num}", method = RequestMethod.GET)
public String addCount(@PathVariable int num, ModelMap model) {
total += num;
model.addAttribute("total", total);
return "list";
}
}
File : list.jsp – Page to display the total counts.
<html>
<body>
<h1>GAE + Spring 3 MVC REST + CRON Example</h1>
<h2>Counts : ${total} </h2>
</body>
</html>
2. cron.xml
Create a “cron.xml” and put in the “WEB-INF” folder. This cron job will call URL “/cron/addCount/1” every 1 minutes.
File : cron.xml
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/cron/addCount/1</url>
<description>Add count + 1 every 1 minutes</description>
<schedule>every 1 minutes</schedule>
</cron>
</cronentries>
The cron job schedule format is a simple English-like format. Please read this GAE scheduler format for more detail.
every N (hours|mins|minutes) ["from" (time) "to" (time)]
3. Output
Deployed on GAE, you can view the cron job status on application administrator page.
URL : http://mkyong-springmvc-cron.appspot.com/cron/count , result after one minutes
Download Source Code
Due to large file size, all Spring and GAE jars are excluded.
What is the performance payload of using Spring instead simple Java EE servlets? Specially in ram? And what about using states: on of the main microservice’s requirements is that all resources must be stateless so if it calls the same servlet in another instance the result of the count may be different. Isn’t it?
Hi mkyong, can I change timing for some specific cron job at run time, like my specific job is running exactly at 12 oçlock in night, but ow I want to change timing from night to day, so can I do this at runtime?
Regards,
Krishan
Hi mkyong, can I change timing for some specific cron job at run time, like my specific job is running exactly at 12 oçlock in night, but ow I want to change timing from night to day, so can I do this at runtime?
Regards,
Krishan
thank you very much doctor!!