How To Execute Cron Job In Testing Environment

Posted By : Manish Kumar Narang | 28-Feb-2018

I have been using Grails from quite some time. People who are coming from Java background will find it very familiar. In fact, you can write java code in grails and it will work perfectly. The world of grails is more uncluttered where the idea is to follow convention over configuration.

One of my tasks was to schedule the cron job. To understand the basics, this tutorial is quite sufficient. It was quite easy to schedule cron job using the Quartz plugin (we used quartz 1.0.2).

 

    class SampleJob {

      static triggers = {
          cron cronExpression: "0 30 22 * * ? *"    // execute job everyday at 10:30 pm
       }

       def execute() {}
            
   }
        

The above code defines a cron job named SampleJob. The time at which this cron job is triggered is determined by the cron expression and execute() method contains the code which will be executed. The fields of the cron expression can be understood as follows -

cronExpression: "s m h D M W Y"
                 | | | | | | `- Year [optional]
                 | | | | | `- Day of Week
                 | | | | `- Month
                 | | | `- Day of Month
                 | | `- Hour
                 | `- Minute
                 `- Second

But the stumbling block for our team was that the code wouldn't execute in the testing environment. We went through the Plugin Documentation and found the following - 

By default, jobs will not be executed when running under the test environment.

But the documentation doesn't clearly state how to execute them under the test environment if one wants to. In fact it turns out that it can also be done quite simply by enabling it in the config file of the quartz plugin - <h> DefaultQuartzConfig.groovy</h> as follows. 

         quartz {
               autoStartup = true
               jdbcStore = false
               waitForJobsToCompleteOnShutdown = true
               exposeSchedulerInRepository = false

          props {
               scheduler.skipUpdateCheck = true
               }
            }

              environments {
                  test {
                    quartz {
                      autoStartup = true
                       }
                   }
             }   
        
   

It can be observed that jobs can be enabled/disabled for other environments also. 

 

About Author

Author Image
Manish Kumar Narang

Manish is an experienced Backend Developer with several years of industry experience in the IT field. He possesses a wide range of skills, including expertise in Backend languages like Core Java, J2EE, Hibernate, Spring/Spring Boot, and Python. Manish is also proficient in relational databases such as MySQL, PostgreSQL, and Oracle. He has hands-on experience in API implementations, web services development, testing, and deployments. Manish has contributed to various internal and client projects, including PMO, Catalyst, Communication-Scaffold, Oodles-Dashboard, and Devops Support, delivering significant business value. He is known for his innovative mindset and excellent problem-solving abilities. He keeps himself updated with new technologies by reading about them. He is skilled at collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and manage expectations. Manish conducts regular project status meetings, ensuring regular updates to clients and stakeholders regarding project progress, risks, and issues. Additionally, he serves as a mentor and coach to junior developers, offering guidance on project management best practices and fostering their skills development.

Request for Proposal

Name is required

Comment is required

Sending message..