1,pom.xml 配置(部分必须的配置)。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.2</version>
</dependency>
以上为必须要依赖的包,其他为一个maven项目的基本配置。
2,java 文件实现
public class DataQuartzJob implements Job{
private static ApplicationContext context;
private static XxService xxService;
static {
context = new ClassPathXmlApplicationContext(new String[] { "classpath:spring.xml" });
xxService = context.getBean("xxServiceImpl", XxService.class);
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
/ userService.doing();
System.out.println(new Date().toString() +" doing");
}
}
3,spring 配置
<bean id="dataQuartzJob" class="com.quartz.DataQuartzJob"></bean>
<!--job定时任务-->
<bean id="dataQuartzJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>com.quartz.DataQuartzJob</value>
</property>
</bean>
<bean id="dataQuartzJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="dataQuartzJobDetail"/>
<property name="cronExpression" value="*/1 * * * * ?"/><!-- 每隔1秒钟触发一次 -->
<!-- <property name="cronExpression" value="0 0 23 * * ?"/>每天23点执行一次 -->
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="dataQuartzJobTrigger"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
以上三部配置完之后,启动服务器即可。