Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Scheduler not working in AEM 6.3

bunny87948290
Level 2
Level 2

Hello,

I have following code for scheduler, I am just trying to print the log statement to check if scheduler actually works. It is not firing any event. In the logs I see -

[java.lang.Runnable]] ServiceEvent REGISTERED

But nothing gets printed. Can you please help ? Here is the code I am using -

@Component(immediate = true )

@Properties({

    @Property(

            label = "Cron expression defining when this Scheduled Service will run",

            description = "[every minute = 0 * * * * ?] ",

            name = "scheduler.expression",

            value = "0 1 0 ? * *"

    ),

    @Property(

            label = "Allow concurrent executions",

            description = "Allow concurrent executions of this Scheduled Service. This is almost always false.",

            name = "scheduler.concurrent",

            propertyPrivate = true,

            boolValue = false

    )

})

@Service

public class RssfeedScheduler implements Runnable {

protected final Logger log = LoggerFactory.getLogger(this.getClass());

public void run() {

log.error("RSSfeed*** This is test to check if cron job is working********");

log.info("in info scheduler***");

}

@Activate

  public void activate() throws RepositoryException {

log.error("In scheduler ****");

log.info("In scheduler123 ****");

}

}

1 Accepted Solution
bunny87948290
Correct answer by
Level 2
Level 2

Ah! May be I missed the Cron expression. I thought it is set to run every minute so that I can verify The log statement getting printed. I will try the Cron expression that you mentioned, by changing it to 1 min instead of 30mins. Thank you!

View solution in original post

4 Replies
BrijeshYadav
Level 5
Level 5

Hi Sneha,


I would recommmend you to use osgi annotation instead of felix scr specific annotations, although there is no hard rule to adapt to the latest version

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.metatype.annotations.AttributeDefinition;

import org.osgi.service.metatype.annotations.Designate;

import org.osgi.service.metatype.annotations.ObjectClassDefinition;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Designate(ocd = RssfeedScheduler.Config.class)

@Component(service = Runnable.class)

public class RssfeedScheduler implements Runnable {

   @ObjectClassDefinition(name = "A scheduled task", description = "Cron-job like task with properties")

   public static @interface Config {

   @AttributeDefinition(name = "Cron-job expression")

  String scheduler_expression() default "0 0/15 * 1/1 * ? *";

   @AttributeDefinition(name = "Concurrent task", description = "Whether or not to schedule this task concurrently")

   boolean scheduler_concurrent() default false;

  }

   private final Logger log = LoggerFactory.getLogger(this.getClass());

   public void run() {

   log.error("RSSfeed*** This is test to check if cron job is working********");

   log.info(" in info scheduler***");

  }

   @Activate
   public void activate(final Config config) {

   log.error("In scheduler ****");

   log.info("In scheduler123 ****");

  }

}

Here is an example for OSGi R7 annotation, To use the OSGi R7 anno­ta­tions, add these depen­den­cies

<dependency>

   <groupId>org.osgi</groupId>

   <artifactId>osgi.core</artifactId>

   <version>6.0.0</version>

   <scope>provided</scope>

</dependency>

<dependency>

   <groupId>org.osgi</groupId>

   <artifactId>osgi.cmpn</artifactId>

   <version>6.0.0</version>

   <scope>provided</scope>

</dependency>

<dependency>

   <groupId>org.osgi</groupId>

   <artifactId>org.osgi.annotation</artifactId>

   <version>6.0.0</version>

   <scope>provided</scope>

</dependency>

<dependency>

   <groupId>org.osgi</groupId>

   <artifactId>org.osgi.service.component</artifactId>

   <version>1.4.0</version>

   <scope>provided</scope>

</dependency>

<dependency>

   <groupId>org.osgi</groupId>

   <artifactId>org.osgi.service.metatype.annotations</artifactId>

   <version>1.4.0</version>

   <scope>provided</scope>

</dependency>

/Brijesh

byswamy11
Level 1
Level 1

Hi Sneha,

Your code is working perfectly and it triggers everyday at 12:01 AM (as per mentioned cron value = "0 1 0 ? * *", your job triggers everyday at 12:01 AM).

If you want to trigger this job for every 30 seconds then change it to "*/30 * * * * ?" and verify.

Regards,

Yerriswamy Reddy

bunny87948290
Level 2
Level 2

This is the code I posted earlier and deleted as it was marking it correct answer and closing this question. i triEd OSGIR6 annotations and it is still not working. Logs statement is not getting printed. Can you please let me if this is actually working for you?

bunny87948290
Correct answer by
Level 2
Level 2

Ah! May be I missed the Cron expression. I thought it is set to run every minute so that I can verify The log statement getting printed. I will try the Cron expression that you mentioned, by changing it to 1 min instead of 30mins. Thank you!

View solution in original post