欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring定时器  

程序员文章站 2022-06-12 21:11:19
...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="rebateJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass">
			<value>com.rebate.scheduled.job.RebateQuartz</value>
		</property>
	</bean>

	<!-- 复杂触发器 -->
	<bean id="rebateCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="rebateJob" />
		</property>
		<property name="cronExpression">
			<!-- 配置时间 6至9时每5分钟执行一次-->
			<value>0 0/5 6-9 * * ?</value>
			<!--测试用的-->
			<!--<value>0/3 * * * * ?</value>-->
		</property>
	</bean>
	<bean id="scheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<ref bean="rebateCronTrigger" />
		</property>
		<property name="autoStartup">
			<value>false</value>
		</property>
	</bean>
</beans>


定时运行的东西
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class RebateQuartz extends QuartzJobBean {

	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {}
}


启动
import org.quartz.impl.StdScheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutoRebate {
	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath*:/spring/*.xml");
		StdScheduler scheduler = (StdScheduler) context.getBean("scheduler");
		scheduler.start();
	}
}