2015-05-20 3 views
0

Я создал очень простой Schedular, которые выполняются после каждой минуты, как показано ниже:Schedular не выполняется при добавлении массива значений

@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 после одного мин. Но когда я добавляю несколько значений через domain.rootpath через консоль OSGi, его вызов не вызывает метод run. Я могу видеть правильную длину массива при активации вызовов, но запуск menthod не выполняется. Есть идеи?

ответ

0

Проблема, похоже, вызывает вызов и активирует метод происходит в одно и то же время после использования документации sling 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){ 

     } 
    } 
0

Я подозреваю, что удаление метод @Modified бы исправить положение. Если этого не произошло, ваш компонент будет деактивирован и активизирован, когда изменится его конфигурация OSGi, и подсистема планировщика Sling должна правильно поднять изменения.

Смежные вопросы