添加数组值时,计划程序不会执行

时间:2015-05-20 16:12:20

标签: osgi cq5 aem

我创建了一个非常简单的调度程序,它在每分钟后执行如下:

@Service(value = Runnable.class)
@Component(name = "Job A", label = "Job A", description = "Job will run after 1 min", metatype = true, immediate = true)
@Properties({
        @Property(label = "Quartz Cron Expression", description = "Quartz Scheduler specific cron expression.", name = "scheduler.expression", value = "0 0/1 * 1/1 * ? *"),
        @Property(unbounded=PropertyUnbounded.ARRAY, value={"*"}, label = "Root Path", name = "domain.rootpath",
                description = "Root Page"),
        @Property(
                label = "Allow concurrent executions",
                description = "Allow concurrent executions of this Scheduled Service",
                name = "scheduler.concurrent",
                boolValue = true,
                propertyPrivate = true
        )
})
public class SimpleSchedular implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(SimpleSchedular.class);
    private String[] rootPath;
    public void run() {
        logger.info("JOB A ::: "+rootPath.length);

    }
    @Activate
    private void activate(final ComponentContext componentContext) {
        final Dictionary<?, ?> properties = componentContext.getProperties();
        this.rootPath = (String [])properties.get("domain.rootpath");
        logger.info("JOB A Length of array ::: "+this.rootPath.length); //prints correct length
    }
    @Modified
    private void modified(final ComponentContext componentContext) {
        activate(componentContext);
    }
}

当我构建代码时,此代码工作正常并在一分钟后打印JOB A ::: 1。但是当我通过OSGi控制台通过domain.rootpath添加更多值时,它不会调用run方法。激活调用时我可以看到正确的数组长度,但运行menthod不会执行。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

问题似乎是在运行时调用,并且在使用吊索文档后同时发生激活方法https://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html我使用了不同的方法来创建调度程序。以下是适用于我的示例代码。

@Component(name = "Hello World Schedular", label = "Simple Schedular", metatype = true)
@Properties(
        @Property(unbounded= PropertyUnbounded.ARRAY, value={"/content/PwC/en"}, label = "Root Path", name = "domain.rootpath",
                description = "Root Page to create the sitemap")
)
public class HelloWorldScheduledService {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    @Reference
    private Scheduler scheduler;
    protected void activate(ComponentContext componentContext) throws Exception {
        final Dictionary<?, ?> properties = componentContext.getProperties();
        final String arr[] = (String [])properties.get("domain.rootpath");
        String schedulingExpression = "0 0/1 * 1/1 * ? *";
        String jobName1 = "case1";
        Map<String, Serializable> config1 = new HashMap<String, Serializable>();
        boolean canRunConcurrently = true;
        final Runnable job1 = new Runnable() {
            public void run() {
                log.info("Executing job1"+arr.length);
            }
        };
        try {
            this.scheduler.addJob(jobName1, job1, config1, schedulingExpression, canRunConcurrently);
        } catch (Exception e) {
            job1.run();
        }
    }

    protected void deactivate(ComponentContext componentContext) {
        log.info("Deactivated, goodbye!");
    }

    protected void modified(ComponentContext componentContext){
       try{
           activate(componentContext);
       }catch (Exception e){

       }
    }

答案 1 :(得分:0)

我怀疑删除@Modified方法会解决问题。如果您没有这样做,那么当OSGi配置发生变化时,您的组件将被停用并重新激活,然后Sling调度程序子系统应该正确地获取更改。