我正在尝试为我的项目需求实现简单的调度程序,我的项目正在使用pixels
。到目前为止,我浏览了Adobe网站并尝试实现所提供的示例,但是没有一个示例更新我的Adobe AEM
文件。
error.log
因此,根据this,它应该可以工作
package sling.docu.examples;
import com.majesco.logger.service.impl.Logger;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
import com.majesco.dcf.common.util.CommonConstants;
@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.period", longValue = 10)
public class ScheduledPeriodicJob implements Runnable {
Logger logger = new Logger().getInstance(CommonConstants.COMMONSERVICE_MODULE_ID);
public void run() {
logger.debug("#########################################");
logger.debug("*****************************************");
logger.debug("*****************************************");
logger.debug("*****************************************");
logger.debug("******** ScheduledPeriodicJob ***********");
logger.debug("*****************************************");
logger.debug("*****************************************");
logger.debug("*****************************************");
logger.debug("#########################################");
}
}
的依赖性
pom.xml
是否需要进行其他任何更改才能使调度程序正常工作?请提出缺失的内容。
答案 0 :(得分:1)
您未设置immediate = true
。通过bundle-start自动启动/激活组件/服务。否则,仅当另一个(已启动)服务请求此服务(或具有依赖项)时,才启动该服务。
在OSGi中,所有服务都是延迟启动的,一旦没有人需要它们便立即停止。我同意,如果调度程序服务将自动启动它将触发的所有服务,则可以进行改进。就是这样。
这是一个有效的示例。
@Component(immediate = true, metatype = true)
@Service({ Runnable.class, AnotherServiceInterface.class})
@Properties({
// run every 5 seconds
@Property(name = "scheduler.period", longValue = 5),
// no concurrent execution
@Property(name = "scheduler.concurrent", propertyPrivate = true, boolValue = false)
})
答案 1 :(得分:0)
以下解决方案按要求工作,这里处理了三种情况
addJob()
:每分钟执行一次工作addPeriodicJob()
:每3分钟执行一次作业 with fireJobAt()
:在特定日期(部署日期+ 30秒的延迟)执行作业
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class ScheduledPeriodicJob {
/** Default log. */
protected final Logger log = LoggerFactory.getLogger(this.getClass());
/** The scheduler for rescheduling jobs. */
@Reference
private Scheduler scheduler;
protected void activate(ComponentContext componentContext) throws Exception {
//case 1: with addJob() method: executes the job every minute
String schedulingExpression = "0 * * * * ?";
String jobName1 = "case1";
Map<String, Serializable> config1 = new HashMap<String, Serializable>();
boolean canRunConcurrently = true;
final Runnable job1 = new Runnable() {
public void run() {
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
log.info("\n\nExecuting");
}
};
try {
this.scheduler.addJob(jobName1, job1, config1, schedulingExpression, canRunConcurrently);
} catch (Exception e) {
job1.run();
}
//case 2: with addPeriodicJob(): executes the job every 3 minutes
String jobName2 = "case2";
long period = 180;
Map<String, Serializable> config2 = new HashMap<String, Serializable>();
final Runnable job2 = new Runnable() {
public void run() {
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
log.info("\nExecuting 2");
}
};
try {
this.scheduler.addPeriodicJob(jobName2, job2, config2, period, canRunConcurrently);
} catch (Exception e) {
job2.run();
}
//case 3: with fireJobAt(): executes the job at a specific date (date of deployment + delay of 30 seconds)
String jobName3 = "case3";
final long delay = 30*1000;
final Date fireDate = new Date();
fireDate.setTime(System.currentTimeMillis() + delay);
Map<String, Serializable> config3 = new HashMap<String, Serializable>();
final Runnable job3 = new Runnable() {
public void run() {
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
log.info("\nExecuting at date: {} with a delay of: {} seconds", fireDate, delay/1000);
}
};
try {
this.scheduler.fireJobAt(jobName3, job3, config3, fireDate);
} catch (Exception e) {
job3.run();
}
}
protected void deactivate(ComponentContext componentContext) {
log.info("Deactivateddbye!");
}
}
下面是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<!-- ====================================================================== -->
<!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
<!-- ====================================================================== -->
<parent>
<groupId>com.adobe.cq</groupId>
<artifactId>schedule</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- ====================================================================== -->
<!-- P R O J E C T D E S C R I P T I O N -->
<!-- ====================================================================== -->
<artifactId>schedule-bundle</artifactId>
<packaging>bundle</packaging>
<name>My Project Bundle</name>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.tagic</groupId>
<artifactId>logger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/logger-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>com.tagic</groupId>
<artifactId>org.apache.sling.commons.scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/org.apache.sling.commons.scheduler-2.3.3-R1232965.jar</systemPath>
</dependency>
</dependencies>
<!-- ====================================================================== -->
<!-- B U I L D D E F I N I T I O N -->
<!-- ====================================================================== -->
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-descriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>com.adobe.cq.schedule-bundle</Bundle-SymbolicName>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>maven-sling-plugin</artifactId>
<configuration>
<slingUrl>http://${crx.host}:${crx.port}/apps/myproject/install</slingUrl>
<usePut>true</usePut>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>
*.impl
</excludePackageNames>
</configuration>
</plugin>
</plugins>
</build>
</project>