Configuration changes for AEM Schedulers?

后端 未结 2 751
你的背包
你的背包 2021-01-29 05:56

I am trying to implement simple scheduler for my project requirement, my project is using Adobe AEM. As of now I gone through Adobe site and tried to implement the

2条回答
  •  伪装坚强ぢ
    2021-01-29 06:48

    Following solution worked as required, here three cases are handled

    • with addJob() : executes the job every minute
    • with addPeriodicJob() : executes the job every 3 minutes
    • with fireJobAt() : executes the job at a specific date (date of deployment + delay of 30 seconds)

      import java.io.Serializable;
      import java.util.Date;
      import java.util.HashMap;
      import java.util.Map;
      
      import org.apache.felix.scr.annotations.Component;
      import org.apache.felix.scr.annotations.Reference;
      import org.apache.sling.commons.scheduler.Scheduler;
      import org.osgi.service.component.ComponentContext;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      
      @Component
      public class ScheduledPeriodicJob {
      
      /** Default log. */
      protected final Logger log = LoggerFactory.getLogger(this.getClass());
      
      /** The scheduler for rescheduling jobs. */
      @Reference
      private Scheduler scheduler;
      
      
      protected void activate(ComponentContext componentContext) throws Exception {
          //case 1: with addJob() method: executes the job every minute
                  String schedulingExpression = "0 * * * * ?";
                  String jobName1 = "case1";
                  Map config1 = new HashMap();
                  boolean canRunConcurrently = true;
                  final Runnable job1 = new Runnable() {
                      public void run() {
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                          log.info("\n\nExecuting");
                      }
                  };
                  try {
                      this.scheduler.addJob(jobName1, job1, config1, schedulingExpression, canRunConcurrently);
                  } catch (Exception e) {
                      job1.run();
                  }
      
          //case 2: with addPeriodicJob(): executes the job every 3 minutes
                  String jobName2 = "case2";
                  long period = 180;
                  Map config2 = new HashMap();
                  final Runnable job2 = new Runnable() {
                      public void run() {
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                          log.info("\nExecuting 2");
                      }
                  };
                  try {
                      this.scheduler.addPeriodicJob(jobName2, job2, config2, period, canRunConcurrently);
                  } catch (Exception e) {
                      job2.run();
                  }
      
          //case 3: with fireJobAt(): executes the job at a specific date (date of deployment + delay of 30 seconds)
                  String jobName3 = "case3";
                  final long delay = 30*1000;
                  final Date fireDate = new Date();
                  fireDate.setTime(System.currentTimeMillis() + delay);
                  Map config3 = new HashMap();
                  final Runnable job3 = new Runnable() {
                      public void run() {
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                          log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
                      }
                  };
                  try {
                      this.scheduler.fireJobAt(jobName3, job3, config3, fireDate);
                  } catch (Exception e) {
                      job3.run();
                  }
      }
      
      protected void deactivate(ComponentContext componentContext) {
          log.info("Deactivateddbye!");
      }
      
      }
      

    Below is pom.xml

    
    
        4.0.0
        
        
        
        
            com.adobe.cq
            schedule
            1.0-SNAPSHOT
        
    
        
        
        
    
        schedule-bundle
        bundle
        My Project Bundle
    
        
            
                org.osgi
                org.osgi.compendium
            
            
                org.osgi
                org.osgi.core
            
    
            
                org.apache.felix
                org.apache.felix.scr.annotations
            
            
                org.slf4j
                slf4j-api
            
    
    
            
                junit
                junit
            
    
            
                javax.mail
                mail
                1.4
            
    
            
                com.tagic
                logger
                0.0.1-SNAPSHOT
                system
                ${project.basedir}/lib/logger-0.0.1-SNAPSHOT.jar
            
    
            
                com.tagic
                org.apache.sling.commons.scheduler
                0.0.1-SNAPSHOT
                system
                ${project.basedir}/lib/org.apache.sling.commons.scheduler-2.3.3-R1232965.jar
            
    
    
    
        
        
        
        
            
                
                    org.apache.felix
                    maven-scr-plugin
                    
                        
                            generate-scr-descriptor
                            
                                scr
                            
                        
                    
                
                
                    org.apache.felix
                    maven-bundle-plugin
                    true
                    
                        
                            com.adobe.cq.schedule-bundle
                        
                    
                
                
                    org.apache.sling
                    maven-sling-plugin
                    
                        http://${crx.host}:${crx.port}/apps/myproject/install
                        true
                    
                
                
                    org.apache.maven.plugins
                    maven-javadoc-plugin
                    
                        
                            *.impl
                        
                    
                
            
        
    
    

提交回复
热议问题