以下是“Java Web实现添加定时任务的方法示例”的完整攻略,分为两条示例:
示例一:使用Quartz框架实现Java Web定时任务
1. 引入相关依赖
在 pom.xml 文件中引入 Quartz 相关依赖:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
2. 编写定时任务类
在项目中创建类,继承 Job 接口并实现 execute 方法,在该方法内编写具体的定时任务逻辑。
示例代码如下:
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
// 定时任务逻辑
// ...
}
}
3. 配置定时任务
在项目中创建 Quartz 配置文件,指定具体的定时任务信息。
示例代码如下:
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob"/>
<property name="targetMethod" value="execute"/>
</bean>
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail"/>
<property name="cronExpression" value="0 0/5 * * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
4. 启动定时任务
在项目启动时,创建并启动 scheduler,即可启动定时任务。
public class QuartzConfig {
private static final String CRON_EXPRESSION = "0 0/5 * * * ?";
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably()
.build();
}
@Bean
public Trigger myTrigger() {
return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule(CRON_EXPRESSION))
.build();
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobDetails(myJobDetail());
schedulerFactoryBean.setTriggers(myTrigger());
return schedulerFactoryBean;
}
}
示例二:使用Spring集成Quartz实现Java Web定时任务
1. 引入相关依赖
在 pom.xml 文件中引入 Spring 相关依赖和 Quartz 相关依赖:
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>RELEASE</version>
</dependency>
<!-- Quartz依赖 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
2. 编写定时任务类
同示例一中的编写方式。
3. 配置定时任务
在 Spring 配置文件中配置定时任务的 cron 触发器。
示例代码如下:
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob"/>
<property name="targetMethod" value="execute"/>
</bean>
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail"/>
<property name="cronExpression" value="0 0/5 * * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
4. 启动定时任务
在项目启动时,通过 Spring 的 SchedulerFactoryBean 获取 scheduler,即可启动定时任务。
public class QuartzConfig {
private static final String CRON_EXPRESSION = "0 0/5 * * * ?";
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob")
.storeDurably()
.build();
}
@Bean
public Trigger myTrigger() {
return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule(CRON_EXPRESSION))
.build();
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobDetails(myJobDetail());
schedulerFactoryBean.setTriggers(myTrigger());
return schedulerFactoryBean;
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java Web实现添加定时任务的方法示例 - Python技术站