Spring----Quartz总结



 

Quartz的关键元素结构图:

Spring----Quartz总结

 

二种Triger:

1.Simple triggers

 

2.Cron triggers

 

3.code

// Initiate a Schedule Factory
        SchedulerFactory schedulerFactory = new  StdSchedulerFactory();
        // Retrieve a scheduler from schedule factory
        Scheduler scheduler = schedulerFactory.getScheduler();
        
        // current time
        long ctime = System.currentTimeMillis(); 
        
        // Initiate JobDetail with job name, job group, and executable  job class
        JobDetail jobDetail = 
          new JobDetail("jobDetail-s1", "jobDetailGroup-s1",  SimpleQuartzJob.class);
        // Initiate SimpleTrigger with its name and group name
        SimpleTrigger simpleTrigger = 
          new SimpleTrigger("simpleTrigger", "triggerGroup-s1");
        // set its start up time
        simpleTrigger.setStartTime(new Date(ctime));
        // set the interval, how often the job should run (10 seconds  here) 
        simpleTrigger.setRepeatInterval(10000);
        // set the number of execution of this job, set to 10 times. 
        // It will run 10 time and exhaust.
        simpleTrigger.setRepeatCount(100);
        // set the ending time of this job. 
        // We set it for 60 seconds from its startup time here
        // Even if we set its repeat count to 10, 
        // this will stop its process after 6 repeats as it gets it endtime  by then.
        //simpleTrigger.setEndTime(new Date(ctime + 60000L));
        // set priority of trigger. If not set, the default is 5
        //simpleTrigger.setPriority(10);
        // schedule a job with JobDetail and Trigger
        scheduler.scheduleJob(jobDetail, simpleTrigger);
        
        // start the scheduler
        scheduler.start();

 

三、Spring中的配置参见结构和调用方式

<description>Quartz的本地Cron式执行任务配置</description>

	<!-- Quartz本地Schduler -->
	<bean id="localQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
		<!-- Triggers集成 -->
		<property name="triggers">
			<list>
				<ref bean="reportSenderTrigger" />
			</list>
		</property>
		<!-- Quartz配置 -->
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.threadCount">5</prop>
			</props>
		</property>
		<!-- 启动时延期3秒开始任务 -->
		<property name="startupDelay" value="15" />
	</bean>

	<!-- Cron式Trigger定义 -->
	<bean id="reportSenderTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="reportSenderDetail" />
		<!-- 每天,朝九晚五工作时间内每50mins执行一次 -->
		<property name="cronExpression" value="0 0/50 9-17 ? * *I" />
	</bean>

	<!-- Cron reportSenderDetail, 基于MethodInvokingJobDetailFactoryBean调用普通Spring Bean -->
	<bean id="reportSenderDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="reportSender" />
		<property name="targetMethod" value="execute" />
		<!-- 同一任务在前一次执行未完成而Trigger时间又到时是否并发开始新的执行, 默认为true. -->
		<property name="concurrent" value="true" />
	</bean>

	<!-- 被Cron执行的普通Spring Bean -->
	<bean id="reportSender" class="org.eric.mrm.schedule.reportSenderJob" />
 

你可能感兴趣的:(spring,工作,bean,quartz,UP)