采用开发框架调度管理Job
1.所需要的第三方包:
quartz-1.5.2.jar
commons-beanutils.jar
commons-digester-1.7.jar
commons-logging.jar
commons-logging-api.jar
log4j-1.2.11.jar
2.定义自己的job:
public class ScanJob implements Job {
public void SanJob(){}
public void execute(JobExecutionContext arg0) throws JobExecutionException {
Logger logger = null;
logger = Logger.getLogger(this.getClass());
logger.info("test job is running");
System.out.println("======================");
System.out.println("this is my 11111111111111111 job");
System.out.println("this is my 22222222222222222 job");
Date abc=new Date();
System.out.println("-------------"+ abc.getSeconds());
}
}
3.在 web.xml加入:
QuartzInitializer
org.quartz.ee.servlet.QuartzInitializerServlet
shutdown-on-unload
true
config-file
quartz.properties
2
4.在classes的目录下建立quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = QuartzScheduler
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1000
#org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
5.在classes的目录下建立quartz_jobs.xml
test
DEFAULT
testJobhere
testTrigger
DEFAULT
test
DEFALUT
0/5 * * * * ?
6.用main方法测试:
public class QuartzMain {
public static void main(String[] args) {
QuartzMain server = new QuartzMain();
try {
server.startScheduler();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void startScheduler() throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail jobDetail =
new JobDetail("testJob", Scheduler.DEFAULT_GROUP, ScanJob.class);
//结束时间
long end = System.currentTimeMillis() + 9000L;
//执行10次,每3秒执行一次,到9秒后结束
SimpleTrigger trigger = new SimpleTrigger("test",null,new Date(),new Date(end),10,3000L);
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
7.在应用服务器中发布,就可以在console看到
======================
this is my 11111111111111111 job
this is my 22222222222222222 job
-------------35
日志.
阅读(2643) | 评论(0) | 转发(0) |