The App Engine Cron Service allows you to configure regularly scheduled tasks that operate at defined times or regular intervals. These tasks are commonly known as cron jobs. These cron jobs are automatically triggered by the App Engine Cron Service. For instance, you might use this to send out a report email on a daily basis, to update some cached data every 10 minutes, or to update some summary information once an hour.

A cron job will invoke a URL, using an HTTP GET request, at a given time of day. A URL invoked by cron is subject to the same limits and quotas as a normal HTTP request, including the request time limit. Cron jobs run only for the default version of your application.

For example:

web.xml :

<!--?xml version="1.0" encoding="UTF-8"?> xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;

           version="2.5">

<context-param>

<param-name>contextConfigLocation</param-name>

      /WEB-INF/applicationContext-quartz.xml</param-value>

</context-param>

 
</web-app>

ApplicationContext-quartz.xml:

<?xml version="1.0" encoding="UTF-8"?>

xmlns=http://www.springframework.org/schema/beans&#8221;

      xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean class=“org.springframework.scheduling.quartz.SchedulerFactoryBean”>

      <property name="triggers"> 
        <list> 
            // Add more cron job to be trigger.... 
        <ref local="userEngagementDailyJobTrigger"/> 
                ------2-----
                         -------3----- 
          <ref local="--n--"/> 
        </list> 
      </property> 
   </bean>

<bean id=“userEngagementDailyJob” class=“org.springframework.scheduling.quartz.JobDetailBean”>

      <property name="jobClass">
         <value>com.projectname.application.cron.UserEngagementDailyCron</value> 
     </property>
   </bean>

<bean id=“userEngagementDailyJobTrigger” class=“org.springframework.scheduling.quartz.CronTriggerBean”>

       <property name="jobDetail"> 
         <ref bean="userEngagementDailyJob"/> 
       </property> <property name="cronExpression">
              <!-- set a schedule time when to run the cron... (see the cron expression for time below)-->       
        <value>0 0/10 * * * ?</value>
       </property> 
    </bean> 
</beans>

package com.project_name.application.cron;

public class UserEngagementDailyCron extends QuartzJobBean implements org.quartz.StatefulJob{

Logger log=Logger.getLogger(UserEngagementDailyCron.class.getName());

  public void executeInternal(JobExecutionContext context){

try{

     UserEngagementService userEngagementService=(UserEngagementService)AppContext
                                              .getBean("userEngagementService"); 
  Date date=new Date(); 
  String fromDate=DateHelper.getDBFormat(DateHelper.substractDaysFromCalendarDate(date,
               MyWallSubscribe.DAILY_VALUE));
    String toDate=DateHelper.getDBFormat(date); 

  log.info("Started User Engagement Daily cron ");

List<User> users = userEngagementService.getMyWallSubscribeUsers(MyWallSubscribe.DAILY);

 
    for(User user:users){ 
    String body=userEngagementService.getMailTemplate(user, fromDate, toDate);

if(!body.equals(“”)){

          userEngagementService.sendMyWallMail(user, body,MyWallSubscribe.DAILY); 
    } 
  }

log.info(“Finished User Engagement Daily cron “);

  }

catch (Exception e) {

      // TODO: handle exception 
log.fatal("Error running DailyCron", e); 
} 
}

}

Cron expression for time :

#Expression                                 Meaning

# “0 0 12 * * ?” Fire at 12pm (noon) every day

# "0 15 10 ? * *"              Fire at 10:15am every day 
# "0 15 10 * * ?"              Fire at 10:15am every day 
# "0 15 10 * * ? *"            Fire at 10:15am every day 
# "0 15 10 * * ? 2005"         Fire at 10:15am every day during the year 2005 
# "0 * 14 * * ?"               Fire every minute starting at 2pm and ending at 2:59pm, every day 
# "0 0/5 14 * * ?"             Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 
# "0 0/5 14,18 * * ?"          Fire every 5 minutes starting at 2pm and ending at 2:55pm,  AND fire every
                             5 minutes starting at 6pm and ending at 6:55pm, every day 
# "0 0-5 14 * * ?"             Fire every minute starting at 2pm and ending at 2:05pm, every day 
# "0 10,44 14 ? 3 WED"         Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 
# "0 15 10 ? * MON-FRI"        Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 
# "0 15 10 15 * ?"             Fire at 10:15am on the 15th day of every month 
# "0 15 10 L * ?"              Fire at 10:15am on the last day of every month 
# "0 15 10 ? * 6L"             Fire at 10:15am on the last Friday of every month 
# "0 15 10 ? * 6L"             Fire at 10:15am on the last Friday of every month 
# "0 15 10 ? * 6L 2002-2005"   Fire at 10:15am on every last Friday of every month during the years 2002,
                             2003, 2004 and 2005 
# "0 15 10 ? * 6#3"           Fire at 10:15am on the third Friday of every month
Advertisements
Comments

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s