How to start cron job after server startup automatically

In some cases, it is required to run cron job immediately after server starts

We have a property in hybris to start all cron jobs after server startup but to make a specific cron job to run after server startup, we don’t have any such property

Let’s see how can we achieve the same

We need to implement ServletContextListner interface and write the code to execute specific cron job on server start up

Create a java class by implementing ServletContextListner interface

  1. Package com.kb.listener;
  2. public class CronjobContextListener implements ServletContextListener {
  4.   @Override
  5.   public void contextDestroyed(ServletContextEvent arg0) {
  6.     //Code can be written to execute before server shuts down.  
  7.   }
  9.   @Override
  10.   public void contextInitialized(ServletContextEvent arg0) {
  11.     // Write a code that needs to be executed just after the server starts
  12. ProductsRemovalCronJobModel  productsRemovalCronJobModel =modelService.create(ProductsRemovalCronJobModel.class);
  14. // assign Job to CronJob
  15. ServicelayerJobModel  servicelayerJobModel = modelService.create(ServicelayerJobModel.class);
  16. servicelayerJobModel.setActive(true);
  17. servicelayerJobModel.setSpringId("productsRemovalJob");
  18. productsRemovalCronJobModel.setJob(servicelayerJobModel);
  20. cronJobService.performCronJob(productsRemovalCronJobModel);
  21.   }
  22. }
Package com.kb.listener;
public class CronjobContextListener implements ServletContextListener {

  public void contextDestroyed(ServletContextEvent arg0) {
    //Code can be written to execute before server shuts down.   

  public void contextInitialized(ServletContextEvent arg0) {
    // Write a code that needs to be executed just after the server starts
ProductsRemovalCronJobModel  productsRemovalCronJobModel =modelService.create(ProductsRemovalCronJobModel.class);

// assign Job to CronJob
ServicelayerJobModel  servicelayerJobModel = modelService.create(ServicelayerJobModel.class);

configure above context listener class in storefront extension’s web.xml as below

  1. <listener>
  2.     <listener-class>
  3.         com.kb.listener.CronjobContextListener
  4. </listener-class>
  5. </listener>

Now whenever server starts, immediately above cron job executes.

Note :
Please check CronJob article for how to create cron job

We can also override contextInitialized() method inside existing hybris context listener HybrisStorefrontContextLoaderListener instead of creating new one but we should not forget calling super.contextInitialized(event);

About the Author

Founder of
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to

Connect with me on Facebook for more updates

Share this article on