Expand my Community achievements bar.

SOLVED

Component Using OSGi DS not added to Sling Scheduler

Avatar

Level 3

I have 2 simple schedulers. They should both result in an message to the logs every 5min. However, The OSGi DS version does not get registered as a Scheduled Task. Is there something in addition to what is below that is needed to make this work?

Using Felix SCR

/**

* This service runs every 5 mins

*/

@Component(metatype = true, label = "A scheduled task",

    description = "Simple demo for cron-job like task with properties")

@Service(value = Runnable.class)

@Properties({

    @Property(name = "scheduler.expression", value = "0 0/5 * * * ?",

        description = "Cron-job expression"),

})

public class SampleScheduler implements Runnable {

    protected static final Logger log = LoggerFactory.getLogger(SampleScheduler.class);

    public static final String CONFIG_PID = "org.aem.coe.coetraining.core.schedulers.SampleScheduler";

    public static final String NAME = "Sample Scheduler";

    @Override

    public void run() {

        log.info("-----------> Scheduler Running: {}", this.getClass());

    }

}

Using OSGi DS

/**

* This service runs every 5 mins

*/

@Component(

        immediate = true,

        configurationPid = SampleScheduler.CONFIG_PID,

        service = Runnable.class

)

@Designate(ocd = SampleScheduler.Configuration.class)

public class SampleScheduler implements Runnable {

    protected static final Logger log = LoggerFactory.getLogger(SampleScheduler.class);

    public static final String CONFIG_PID = "com.aemcoe.coe63.schedulers.SampleScheduler";

    public static final String NAME = "Sample Scheduler";

    @Override

    public void run() {

        log.info("-----------> Scheduler Running: {}", this.getClass());

    }

    @ObjectClassDefinition(name=SampleScheduler.NAME)

    public @interface Configuration {

        @AttributeDefinition(

                name = "scheduler.expression",

                description = "Provide the cron expression for the scheduler",

                type = AttributeType.STRING

        )

        String scheduler_expression() default "0 0/5 * * * ?";

    }

}

I have both installed but when I look at http://localhost:4502/system/console/status-slingscheduler​ I do not see the one with the OSGi DS Annotation. I do see the other:

Job : org.aem.coe.coetraining.core.schedulers.SampleScheduler.15615, class: org.aem.coe.coetraining.core.schedulers.SampleScheduler, concurrent: true, bundleId: 550, serviceId: 15615

Trigger : Trigger 'DEFAULT.org.aem.coe.coetraining.core.schedulers.SampleScheduler.15615':  triggerClass: 'org.quartz.impl.triggers.CronTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Mon Sep 10 16:25:00 CDT 2018

Additionally, I do see the OSGi DS Component get registered, but it doesn't get a Service PID or show the meta properties.
1569071_pastedImage_4.png

Now, I know I could probably fix this by creating a configuration file or setting the configurations via the System Console... but is there not a way to just utilize the annotations and default values to add this to the scheduler? 

1 Accepted Solution

Avatar

Correct answer by
Level 3

That made all the difference in getting your sample to be added to the scheduler automatically. Which got me looking into why your sample worked while mine didn't. Turns out the activate method is required.

Adding the following caused everything to start working...

@Activate
protected void activate(final SampleScheduler.Configuration config) {

   //Required
}

So I played around with it by removing the parameter and once again it stopped getting added to the scheduler. So, it looks like you need to have an @Activate annotated method with your configuration as a parameter for the scheduler to grab your Scheduled Task.

View solution in original post

5 Replies

Avatar

Employee Advisor

I think that has something to do with the dots "." in the required property names.

You might want to check the XML files in your bundle's OSGI-INF folder and compare between the both types of annotations.

Avatar

Level 3

So the values are definitely different. Interestingly, I copied the example Arun provided and it has the same problem as my example with OSGi DS annotations, but it did show some extra properties provided below. I'm guessing most people don't see this problem during development because they are utilizing the system console to play around with the configurations. Once a configuration exists the task is scheduled and there isn't an issue, but in higher environments where access to modify configurations in that way is restricted or disabled this can cause problems.

Felix SCR

<?xml version="1.0" encoding="UTF-8"?>

<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">

    <scr:component name="org.aem.coe.coetraining.core.schedulers.SampleScheduler">

        <implementation class="org.aem.coe.coetraining.core.schedulers.SampleScheduler"/>

        <service servicefactory="false">

            <provide interface="java.lang.Runnable"/>

        </service>

        <property name="scheduler.expression" value="0 0/5 * * * ?"/>

        <property name="service.vendor" value="Adobe"/>

        <property name="service.pid" value="org.aem.coe.coetraining.core.schedulers.SampleScheduler"/>

    </scr:component>

</components>

OSGI DS

<?xml version="1.0" encoding="UTF-8"?>

<scr:component configuration-pid="com.aemcoe.coe63.schedulers.SampleScheduler" immediate="true" name="com.aemcoe.coe63.schedulers.SampleScheduler" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0">

    <implementation class="com.aemcoe.coe63.schedulers.SampleScheduler"/>

    <service>

        <provide interface="java.lang.Runnable"/>

    </service>

</scr:component>

Arun's Example: (Note it doesn't include scheduler.expression for some reason even though its defined)

<?xml version="1.0" encoding="UTF-8"?>

<scr:component activate="activate" name="com.aemcoe.coe63.schedulers.SimpleScheduledTask" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0">

    <implementation class="com.aemcoe.coe63.schedulers.SimpleScheduledTask"/>

    <service>

        <provide interface="java.lang.Runnable"/>

    </service>

    <property name="scheduler.concurrent" value="true" type="Boolean"/>

    <property name="myParameter" value="" type="String"/>

</scr:component>

I think in the short term I am just going to define the property via a configuration file or maybe as a private property in the @Component annotation... I tested and either way works as far as getting the scheduler to run. I just don't like double defining the property. I feel like this shouldn't require that to get picked up by the scheduler.

So in the end this was the easiest for my project and I still have the Configuration interface with the scheduler.expression defined with a default value so it shows up in the configMgr if someone had access or wanted to set a new value via system console.

@Component(

  immediate = true,

  configurationPid = SampleScheduler.CONFIG_PID,

  service = Runnable.class,

  property = {

   "scheduler.expression=0 0/5 * * * ?",

  }

)

Avatar

Community Advisor

Hi,

In my example I defined default value of property using defaultValue attribute just for demo thats why it is not showing but you can change and try below for scheduler_expression:

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

        String scheduler_expression() default "0 0/5  * * * ?";



Arun Patidar

Avatar

Correct answer by
Level 3

That made all the difference in getting your sample to be added to the scheduler automatically. Which got me looking into why your sample worked while mine didn't. Turns out the activate method is required.

Adding the following caused everything to start working...

@Activate
protected void activate(final SampleScheduler.Configuration config) {

   //Required
}

So I played around with it by removing the parameter and once again it stopped getting added to the scheduler. So, it looks like you need to have an @Activate annotated method with your configuration as a parameter for the scheduler to grab your Scheduled Task.