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 ****");
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
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!
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 annotations, add these dependencies
<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
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
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?
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!
Views
Likes
Replies