springboot下使用@Scheduled和@Async

有一点很重要(这是我放在最前面的原因):调用被@Async注解的方法时,调用方法不能与被调用方法放在同一类中,否则注解不生效
因此,我们应该这样做:
一个类中实现被调用方法,另一个类进行调用
在这里插入图片描述
在这里插入图片描述
在启动类上加@EnableAsync和@EnableScheduling

import com.spring4all.mongodb.EnableMongoPlus;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableAsync
@EnableScheduling
@EnableTransactionManagement(proxyTargetClass = true)
@EnableMongoPlus
@SpringBootApplication
public class MpfcontrolApplication {

    public static void main(String[] args) {
        // SpringApplication.run(MpfcontrolApplication.class, args);
        new SpringApplicationBuilder(MpfcontrolApplication.class).headless(false).run(args);

        ViewStart.run();
    }

}

使用 @Scheduled(fixedRate=10000)实现定时间间隔调用(单位毫秒)

 //定时调用批处理方法
    @Scheduled(fixedRate=10000)
    public void doTimer(){
//        System.out.println("定时执行");
        JSONObject json=new JSONObject();
        batchAddSystemLog(json,2);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值