Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Hi

Avatar

Level 1

How we can mention crone expression for shedulers in OSGI Configuration?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @jyothibontu19 

 

You can use the code as below :

 

@AttributeDefinition(
			name = "Cron Expression", 
			description = "Cron expression used by the scheduler", 
			type = AttributeType.STRING)
	public String cronExpression() default "0 * * * * ?";

You can refer the following documents for more info on the same :

https://blog.developer.adobe.com/aem-6-4-creating-a-scheduler-using-osgi-r6-annotations-4ad0b8c6fce7

https://aem.redquark.org/2018/10/day-13-schedulers-in-aem.html

 

Thanks.

 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @jyothibontu19 

 

You can use the code as below :

 

@AttributeDefinition(
			name = "Cron Expression", 
			description = "Cron expression used by the scheduler", 
			type = AttributeType.STRING)
	public String cronExpression() default "0 * * * * ?";

You can refer the following documents for more info on the same :

https://blog.developer.adobe.com/aem-6-4-creating-a-scheduler-using-osgi-r6-annotations-4ad0b8c6fce7

https://aem.redquark.org/2018/10/day-13-schedulers-in-aem.html

 

Thanks.

 

Avatar

Community Advisor

Refer below code for cron expression at OSGI config.

@AttributeDefinition(
name = "scheduler.expression",
description = "Cron expression used by the scheduler",
type = AttributeType.STRING)
public String scheduler_expression() default "0 0 14 1/1 * ? *";

Scheduler will run daily at 2pm with this deafult expression. Next dates be like 12022-09-09 Fri 14:00:002.2022-09-10 Sat 14:00:003.2022-09-11

Refer https://blog.developer.adobe.com/aem-6-4-creating-a-scheduler-using-osgi-r6-annotations-4ad0b8c6fce7

 

 

Avatar

Community Advisor

You can declare it in the properties.

 

package sling.docu.examples;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;

@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.expression", value = "0 * * * * ?")
public class ScheduledCronJob implements Runnable {

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

    public void run() {
    	log.info("Executing a cron job (job#1) through the whiteboard pattern");
    }
//
}

Checkout the documentation: Apache Sling :: Scheduler Service (commons scheduler)