首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AEM 6.3 :使用OSGi R6注释创建调度程序

AEM 6.3 :使用OSGi R6注释创建调度程序
EN

Stack Overflow用户
提问于 2017-10-21 08:30:14
回答 2查看 3.5K关注 0票数 1

我已经使用OSGi R6注释编写了一个调度程序,但它似乎不能运行:

代码语言:javascript
运行
复制
package com.aem.sites.interfaces;

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

@ObjectClassDefinition(name = "Scheduler Configuration for Weather", description = "Configuration file for Scheduler")
public @interface SchedulerConfiguration {

    @AttributeDefinition(
            name = "sample parameter",
            description="Sample String parameter",
            type = AttributeType.STRING
            )
    public String parameter() default "scheduler";

    @AttributeDefinition(
            name = "Concurrent",
            description = "Schedule task concurrently",
            type = AttributeType.BOOLEAN
        )
        boolean scheduler_concurrent() default true;

        @AttributeDefinition(
            name = "Expression",
            description = "Cron-job expression. Default: run every minute.",
            type = AttributeType.STRING
        )
        String scheduler_expression() default "0 * * * * ?";

}

代码语言:javascript
运行
复制
package com.aem.sites.schedulers;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.sites.interfaces.SchedulerConfiguration;

@Component(immediate = true,
        configurationPid = "com.aem.sites.schedulers.WeatherServiceScheduler")
@Designate(ocd=SchedulerConfiguration.class)
public class WeatherServiceScheduler implements Runnable {

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

     private String myParameter;

    @Override
    public void run() {
         logger.info("*******************************************Sample OSGi Scheduler is now running", myParameter);

    }
    @Activate
    public void activate(SchedulerConfiguration config) {
        logger.info("*******************************************weather service scheduler"+ myParameter);
        myParameter = config.parameter();
    }

}

我正在遵循这个https://github.com/nateyolles/aem-osgi-annotation-demo/blob/master/core/src/main/java/com/nateyolles/aem/osgiannotationdemo/core/schedulers/SampleOsgiScheduledTask.java,但看起来我在做一些错误的事情。但不确定是什么。

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2019-05-18 03:28:51

在您的WeatherSchedulerService类中,您没有将其注册为服务。你可以像这样做service = Runnable.class,而不是configurationPid,。

使用OSGi R6注释创建SlingScheduler的正确方法如下所示:

  1. 创建OSGi配置类

代码语言:javascript
运行
复制
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name = "Sling Scheduler Configuration", description = "This configuration is used to demonstrates a sling scheduler in action")
public @interface SchedulerConfiguration {

    @AttributeDefinition(
            name = "Scheduler name", 
            description = "Name of the scheduler", 
            type = AttributeType.STRING)
    public String name() default "Custom Sling Scheduler";

    @AttributeDefinition(
            name = "Enabled", 
            description = "Flag to enable/disable a scheduler", 
            type = AttributeType.STRING)
    public boolean enabled() default false;

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

    @AttributeDefinition(
            name = "Custom parameter", 
            description = "Custom parameter to showcase the usage of a sling scheduler", 
            type = AttributeType.STRING)
    public String customParameter();
}

  1. 将排定程序类创建为服务。为了使用R6注释创建OSGi服务,我们使用@Component(service=<your-interface>.class,...)。因此,请按如下方式创建服务:

代码语言:javascript
运行
复制
import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
import org.redquark.aem.learning.core.configurations.SchedulerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Component(immediate = true, service = Runnable.class)
@Designate(ocd = SchedulerConfiguration.class)
public class CustomScheduler implements Runnable {

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

    // Custom parameter that is to be read from the configuration
    private String customParameter;

    // Id of the scheduler based on its name
    private int schedulerId;

    // Scheduler instance injected
    @Reference
    private Scheduler scheduler;

    /**
     * Activate method to initialize stuff
     * 
     * @param schedulerConfiguration
     */
    @Activate
    protected void activate(SchedulerConfiguration schedulerConfiguration) {
        schedulerId = schedulerConfiguration.name().hashCode();
        customParameter = schedulerConfiguration.customParameter();
    }

    /**
     * Modifies the scheduler id on modification
     * 
     * @param schedulerConfiguration
     */
    @Modified
    protected void modified(SchedulerConfiguration schedulerConfiguration) {
        // Removing scheduler
        removeScheduler();
        // Updating the scheduler id
        schedulerId = schedulerConfiguration.name().hashCode();
        // Again adding the scheduler
        addScheduler(schedulerConfiguration);
    }

    /**
     * This method deactivates the scheduler and removes it
     * 
     * @param schedulerConfiguration
     */
    @Deactivate
    protected void deactivate(SchedulerConfiguration schedulerConfiguration) {
        // Removing the scheduler
        removeScheduler();
    }

    /**
     * This method removes the scheduler
     */
    private void removeScheduler() {
        log.info("Removing scheduler: {}", schedulerId);
        // Unscheduling/removing the scheduler
        scheduler.unschedule(String.valueOf(schedulerId));
    }

    /**
     * This method adds the scheduler
     * 
     * @param schedulerConfiguration
     */
    private void addScheduler(SchedulerConfiguration schedulerConfiguration) {
        // Check if the scheduler is enabled
        if (schedulerConfiguration.enabled()) {
            // Scheduler option takes the cron expression as a parameter and run accordingly
            ScheduleOptions scheduleOptions = scheduler.EXPR(schedulerConfiguration.cronExpression());

            // Adding some parameters
            scheduleOptions.name(schedulerConfiguration.name());
            scheduleOptions.canRunConcurrently(false);

            // Scheduling the job
            scheduler.schedule(this, scheduleOptions);
            log.info("Scheduler added");
        } else {
            log.info("Scheduler is disabled");
        }
    }

    /**
     * Overridden run method to execute Job
     */
    @Override
    public void run() {
        log.info("Custom Scheduler is now running using the passed custom paratmeter, customParameter {}",
                customParameter);

    }

  • 在activate()方法中,我们正在读取所需的值。然后我们从调度程序名称中获取schedulerId。
  • ()方法重新计算schedulerId,以防OSGi配置被修改。
  • 在addScheduler()方法中,我们使用scheduler API注册调度程序。

有关更多信息和逐步执行的信息,您也可以查看我的博客文章- Day 13: Schedulers in AEM

我希望这能帮到你。祝你编码愉快!

票数 1
EN

Stack Overflow用户

发布于 2019-05-15 23:49:07

在类注释中没有必要使用configurationPid,而且您还遗漏了应该在immediate=true之后的service=Runnable.class,即类声明应该如下所示:

代码语言:javascript
运行
复制
@Component(immediate = true, service=Runnable.class)
@Designate(ocd=SchedulerConfiguration.class)
public class WeatherServiceScheduler implements Runnable {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46858957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档