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);
  19. modelService.save(productsRemovalCronJobModel);
  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);

Share this article on

Comment (4)

  1. Just changing the items.xml and doing system update (can be done on Production environment) is not enough ? I thought System update will take care of changes in the definition of Item type in items.xml fie ?

  2. Sir, I want to add the new field in registration page on the given hybris website.what are changes are to be made.plse let me known.

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *