依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
注解
1.cron
按顺序排列:位数6—7位(最后一位可以非必填项)
简单实例
@EnableScheduling //启动定时任务
@Configuration
public class ScheduledTask {
@Autowired
BookMapper bookMapper;
//每2秒插入数据
@Scheduled(cron="* 0/10 * * * * ")
public void task(){
Book book=new Book();
book.setBookName("java56");
bookMapper.insert(book);
System.out.println("插入book成功!");
}
}
/** 注意:@EnableScheduling可以注解在application上面,二选其一 **/
@SpringBootApplication
//@EnableScheduling
@MapperScan("com.mybatisplus.demo.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
异步方法实现定时任务:
一: 使用异步方法的前提
import com.zhkj.jtb.platform.commons.utils.DateUtil;
import com.zhkj.jtb.platform.report.ytsf.service.AlarmStatisticsSnapshotService;
import com.zhkj.jtb.platform.report.ytsf.service.GPSLiveService;
import com.zhkj.jtb.platform.report.ytsf.service.TripService;
import com.zhkj.jtb.platform.report.ytsf.service.VehicleAlarmService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
@Data
@Configuration
@EnableAsync
@Slf4j
@ConfigurationProperties(prefix = "zl.async")//配置在xml里面的参数,没有取到就是使用类里面的默认值
public class AsyncConfig implements AsyncConfigurer {
@Autowired
private VehicleAlarmService vehicleAlarmService;
@Autowired
private AlarmStatisticsSnapshotService snapshotService;
@Autowired
private TripService tripService;
@Autowired
private GPSLiveService gpsLiveService;
private Integer coreSize = 10;
private Integer maxSize = 20;
private Integer queueCapacity = 500;
@Override
public Executor getAsyncExecutor() {
//线程池
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(coreSize);
taskExecutor.setMaxPoolSize(maxSize);
taskExecutor.setQueueCapacity(queueCapacity);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncUncaughtExceptionHandler();
}
class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler{
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
//发生异常时的处理方式
}
}
}
二: 使用异步线程
//@Transactional(rollbackFor = Exception.class)//事务回滚,发生SQLException异常时回滚数据。(要么全执行,要么都不执行)
@Scheduled(cron ="0 0/5 0 1-2 * ?")//每天凌晨1-2每5分钟执行一次
@Async
public void monthStatistics(){
//定时任务处理的业务逻辑
}
**注意:@Async被该注解标识的方法是异步方法,
就是它会给你在后台开启一个线程来处理被标识的方法,
所有耗时任务会经常使用它