Spring 3实现定时任务

前面已经讲到了spring 3整合Quartz 2来实现时任务,其实从spring 3开始,它本身就已经自带了一套自主开发的定时任务工具Spring-Task,可以将它看成是一个轻量级的Quartz,而且使用起来十分简单,除spring相关的包外不需要额外的包,支持注解和配置文件两种形式。

第一种:配置文件方式

第一步:编写作业类,它是一个普通的Java类,不需要继承和实现任何类和接口:

@Service  
public class TaskJob {  
    public void job1() {  
        System.out.println("任务成功运行。。。");  
    }  
}

第二步:在spring配置文件头中添加spring-task的命名空间及描述:

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    ...  
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 


第三步:spring配置文件中设置具体的任务:

<task:scheduled-tasks>   
        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
</task:scheduled-tasks>  
  
<context:component-scan base-package=" com.task " /> 


说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式,具体写法这里就不介绍了。

<context:component-scan base-package="com.task" />这个配置不消多说了,spring扫描注解用的。

到这里配置就完成了,是不是很简单。

第二种:使用注解形式

从spring 2.5开始,可以方便的使用注解来声明bean,对于定时任务,同样提供了注解@Scheduled,我们该注解的定义:

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  
  
  public abstract long fixedDelay();  
  
  public abstract long fixedRate();  
}


可以看出该注解可以接收三个参数,分别表示的意思是:

cron:指定cron表达式

fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

下面我们使用注解来实现一下看看:

第一步:还是编写我们的任务类,和上面基本一样,只不过方法上添加了@Scheduled注解。

@Component("taskJob")  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void run() {  
        System.out.println("任务成功运行。。。");  
    }  
}


第二步:同样需要在spring配置文件头中添加spring-task的命名空间及描述,另外添加扫描spring-task的配置:

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    ...  
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
	
	...
	<!-- 开启这个配置,spring才能识别@Scheduled注解 --> 
	<task:annotation-driven/>
	...
</beans>	


配置完毕,我们的任务已经可以运行了。当然你也可以把cron参数换成另外的两个,自己尝试一下吧。spring-task还有很多的参数,这里就不一一解释了,具体可以查看官方的文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了一种简单而强大的方式来实现定时任务调度。下面是使用Spring Boot实现定时任务调度的步骤: 1. 添加依赖:在`pom.xml`文件中添加Spring Boot的定时任务依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 2. 创建定时任务类:创建一个继承自`QuartzJobBean`的定时任务类,并实现`executeInternal`方法,该方法中编写具体的定时任务逻辑。 ```java import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { // 定时任务逻辑 System.out.println("定时任务执行中..."); } } ``` 3. 配置定时任务:在`application.properties`或`application.yml`文件中配置定时任务的相关属性,例如触发时间、触发频率等。 ```yaml spring: quartz: job-store-type: memory properties: org: quartz: scheduler: instanceName: MyScheduler instanceId: AUTO job-details: my-job: cron: 0/5 * * * * ? job-class-name: com.example.MyJob ``` 4. 启动定时任务:在Spring Boot的启动类上添加`@EnableScheduling`注解,开启定时任务的自动配置。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 5. 运行定时任务:启动Spring Boot应用程序后,定时任务将按照配置的触发时间和频率执行。 以上就是使用Spring Boot实现定时任务调度的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值