我创建了一个非常简单的调度程序,每分钟之后执行如下:
@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
添加更多值时,它就不会调用运行方法。当激活调用但运行Menthod未执行时,我可以看到正确的数组长度。有什么想法吗?
使用吊索文档https://sling.apache.org/documentation/bundles/scheduler-service-commons--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){
}
}
我怀疑删除@Modified
方法会解决问题。如果您没有,则在OSGI配置更改时,您的组件将被停用和重新激活,并且Sling Scheduler子系统应正确拾取更改。