Spring配合实现Java循环任务

定时任务如果不需要指定固定的时间点(比如晚上22:00),而只需要每隔一段时间执行任务,可以用jdk中自带的定时器来实现。Spring提供了很好的支持。

比如需要每隔一小时,就要向控制台中输出一行字符串“hello,world!”,只需要定义一个类,写一个Spring配置文件就可以了。

类用来实现具体的输出,需要继承自java.util.TimerTask。

[code]Package test.pupi;

import java.util.List;
import java.util.TimerTask;

public class PrintHelloworld extends TimerTask {


@Override
public void run() {
// TODO Auto-generated method stub
System.out.print("hello,world!");
}
}[/code]

spring配置文件则也很简单,


[code]<?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定义上面的那个实现定时操作的类 -->
<bean id="printHelloworld"
class="test.pupi.PrintHelloworld ">
</bean>


<!-- 这个bean定义循环定时任务,注意,period属性的单位是毫秒。3600000毫秒就是1小时 -->
<bean id="scheduledEmailTask"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="printHelloworld" />
<property name="period" value="3600000" />
</bean>

<!-- 这个bean定义当前应用对应的所有的定时任务 -->
<bean
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledEmailTask" />
</list>
</property>
</bean>


</beans>[/code]
  • 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、付费专栏及课程。

余额充值