您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > spring整合任务调度
Spring-Quartz定时调度器一:SimpleTrigger触发器案例目的实现每3秒钟进行一次任务提醒,定时器在10秒钟后关闭。一共执行了四次。1:导入包log4j-1.2.17.jarquartz-all-1.8.6.jarslf4j-api-1.6.0.jarslf4j-log4j12-1.6.0.jar2:在entity包下创建【计划实体类】/***计划实体类*/publicclassPlan{privateStringdate;//日期privateStringtask;//任务publicPlan(Stringdate,Stringtask){this.date=date;this.task=task;}@OverridepublicStringtoString(){returnthis.date+:+this.task;}}3:在service包下创建【提醒服务类】/***提醒服务类*/publicclassRemindService{publicvoidouputPlan(StringuserName){ListPlanplansForToday=getPlansForToday(userName);System.out.print(userName+的提醒信息:\n);for(Planplan:plansForToday){System.out.print(plan+\n);}}publicListPlangetPlansForToday(StringuserName){//模拟数据库查询,仅为说明问题ListPlanlist=newArrayListPlan();list.add(newPlan(2003-8-99:00,研讨会|地点:会议室C01));list.add(newPlan(2003-8-914:00,汇报|地点:总裁办公室));returnlist;}}4:在job包下创建【提醒任务类】/***提醒任务类*/publicclassRemindJobimplementsJob{publicRemindJob(){}privateRemindServiceremindService;publicvoidexecute(JobExecutionContextjobContext)throwsJobExecutionException{remindService=newRemindService();remindService.ouputPlan(张三);}}5:在test包下创建【调度定时器任务类】/***调度定时器任务类*@authorAdministrator*/publicclassTestJob{publicvoiddoRemind()throwsSchedulerException,InterruptedException{//创建一个任务JobDetailjob=newJobDetail(RemindJob,group1,RemindJob.class);//创建一个触发器SimpleTriggersimTrig=newSimpleTrigger(trigRemindJob,SimpleTrigger.REPEAT_INDEFINITELY,3000);simTrig.setStartTime(newDate(System.currentTimeMillis()+1000));//创建调度者工厂SchedulerFactorysfc=newStdSchedulerFactory();//创建一个调度者Schedulersched=sfc.getScheduler();//注册并进行调度sched.scheduleJob(job,simTrig);//启动调度sched.start();//sleep10sThread.sleep(10000);//关闭调度sched.shutdown();}publicstaticvoidmain(String[]args){try{TestJobtestJob=newTestJob();testJob.doRemind();}catch(Exceptione){e.printStackTrace();}}}6:实现数据的传递在test包下找到TestJob.java类中增加:JobDataMapdataMap=job.getJobDataMap();dataMap.put(user,张三);在job包下找到RemindJob.java类中增加://获取调度上下文的信息System.out.print(TriggerName:+jobContext.getTrigger().getName()+&TriggerTime:+newDate()+&JobName:+jobContext.getJobDetail().getName()+\n);//获取用户信息JobDataMapdataMap=jobContext.getJobDetail().getJobDataMap();StringuserName=(String)dataMap.get(user);二:CronTrigger触发器1:语法和表达式介绍2:在test包下创建TestJobWithCron.java/***CronTrigger触发器*/publicclassTestJobWithCron{publicvoiddoRemind()throwsException{//创建一个任务JobDetailjob=newJobDetail(remindJob,group1,RemindJob.class);JobDataMapdataMap=job.getJobDataMap();dataMap.put(user,张三);//创建Cron触发器//1/31代表起始值3代表增量含义是:过一秒后,每隔三秒触发CronTriggercronTrig=newCronTrigger(remindJob,group1,1/3****?);//创建调度者工厂SchedulerFactorysfc=newStdSchedulerFactory();//创建一个调度者Schedulersched=sfc.getScheduler();//注册并进行调度sched.scheduleJob(job,cronTrig);//启动调度sched.start();//sleep10sThread.sleep(10000);//关闭调度sched.shutdown();}publicstaticvoidmain(String[]args){try{TestJobWithCrontestJob=newTestJobWithCron();testJob.doRemind();}catch(Exceptione){e.printStackTrace();}}}039287?//每年的7月28号班庆,早上8点发送邮件通知大家!08287*?2014三:Calender除某日设置1:在test包下创建TestJobCalendar.java/***除元旦之外*@authorAdministrator*/publicclassTestJobCalendar{publicvoiddoRemind()throwsException{JobDetailjob=newJobDetail(remindJob,group1,RemindJob.class);JobDataMapdataMap=job.getJobDataMap();dataMap.put(user,张三);//每个工作日9:30触发,除元旦那天!CronTriggercronTrig=newCronTrigger(remindJob,group1,0309?*MON-FRI);SchedulerFactorysfc=newStdSchedulerFactory();Schedulersched=sfc.getScheduler();//创建CalendarAnnualCalendarcal=newAnnualCalendar();//构建日期CalendarmyCal=GregorianCalendar.getInstance();myCal.set(Calendar.MONTH,Calendar.JANUARY);myCal.set(Calendar.DATE,1);//设定要排除的日期cal.setDayExcluded(myCal,true);//注册Calendarsched.addCalendar(NewYearHoliday,cal,true,true);//和触发器进行关联cronTrig.setCalendarName(NewYearHoliday);sched.scheduleJob(job,cronTrig);sched.start();}publicstaticvoidmain(String[]args){try{TestJobCalendartestJob=newTestJobCalendar();testJob.doRemind();}catch(Exceptione){e.printStackTrace();}}}四:springQuartz-SimpleTrigger1导包spring.jarcommons-logging-1.1.1.jar导入spring3.1\lib下面的包2在springQuartz1包下创建RemindJob.javapublicclassRemindJobextendsQuartzJobBean{publicRemindJob(){}privateStringuserName;privateRemindServiceremindService;publicvoidsetUserName(StringuserName){this.userName=userName;}publicvoidsetRemindService(RemindServiceremindService){this.remindService=remindService;}@OverrideprotectedvoidexecuteInternal(JobExecutionContextjobContext)throwsJobExecutionException{remindService.ouputPlan(userName);}}3在springQuartz1包下创建applicationContext.xml?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:p=:schemaLocation=://!--配置提醒服务bean该bean的id是remindService--beanid=remindServiceclass=service.RemindService/bean!--配置job--beanid=remindJobclass=org.springframework.scheduling.quartz.JobDetailBean!--指定jobClass是RemindJob--propertyname=jobClassvalue=springQuartz1.Remi
本文标题:spring整合任务调度
链接地址:https://www.777doc.com/doc-2859860 .html