异步任务,邮件任务,定时执行任务

1.异步任务

1.AsyncService @Async @EnableAsync

package com.yang.service;


import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {

    //告诉spring 这是一个异步的方法
    @Async
    public void hello(){

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理。。。");

    }

}

2.AsyncCointroller

@RestController
public class ASyncController {

    @Autowired
    AsyncService asyncService;


    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello(); //停止三秒
        return "ok";

    }
}

3.在springboot的主程序上面添加一个注解@EnableAsync

//开启异步注解功能
@EnableAsync


//开解定时功能的注解
@EnableScheduling

@SpringBootApplication
public class Springboot09TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }

}

总结: ,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况;在service上加@Async告诉spring这是一个异步的方法,同时在主启动类上面添加注解@EnableAsync开启异步注解功能

2.邮件任务

1.引入依赖

        <!--javax.mail:配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2.配置文件

spring.mail.username=2434257976@qq.com
#邮箱的授权码
spring.mail.password=ddjsyobrkpfhebgi
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.unable=true

3.测试类

package com.yang;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


@SpringBootTest
public class Springboot09TestApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    public void contextLoads() {


        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setSubject("邮件");
        mailMessage.setText("这是一封邮件吗");
        mailMessage.setTo("2434257976@qq.com");
        mailMessage.setFrom("2434257976@qq.com");

        mailSender.send(mailMessage);

    }

    @Test
    public void contextLoads2() throws MessagingException {

        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setSubject("邮件");
        helper.setText("<p style='color:red'>邮件222</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\杨成刚\\Desktop\\1.jpg"));


        helper.setTo("2434257976@qq.com");
        helper.setFrom("2434257976@qq.com");

        mailSender.send(mimeMessage);

    }
        //  /**加上回车
    /**
     *
     * @param html
     * @param setSubject
     * @throws MessagingException
     * @Author 杨成刚
     */
    //封装方法
    public void sendMail(Boolean html,String setSubject,String text) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        helper.setSubject("邮件");
        helper.setText("<p style='color:red'>邮件222</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\杨成刚\\Desktop\\1.jpg"));


        helper.setTo("2434257976@qq.com");
        helper.setFrom("2434257976@qq.com");

        mailSender.send(mimeMessage);

    }


}

3.定时执行任务

TaskExecutor接口 任务执行者

TaskScheduler接口 任务调度者

两个注解:

@EnableScheduling

@Scheduled

1.创建一个service

@Service
public class ScheduledService {


    //在一个特定的时间执行这个方法

    /*
    *    20 01 11 * * ?   每天11点零一分20秒执行一次这个方法
    *     20 0/5 11,18 * * ? 每天11点和18点,每隔五分钟执行一次
    * */


    //cron表达式
    //秒 分 时 日 月 周几
    @Scheduled(cron = "0/2 * * * * ?")
    public void hello(){
        System.out.println("hello,你被执行了");
    }

}

2.在主程序上加注解@EnableScheduling

//开启异步注解功能
@EnableAsync


//开解定时功能的注解
@EnableScheduling

@SpringBootApplication
public class Springboot09TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }

}

3.总结一个下常用的corn表达式

10/2 * * * * ?   表示每2秒 执行任务
(10 0/2 * * * ?   表示每2分钟 执行任务
(10 0 2 1 * ?   表示在每月的1日的凌晨2点调整任务
(20 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业
(30 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(40 0 10,14,16 * * ?   每天上午10点,下午2点,4点
(50 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
(60 0 12 ? * WED   表示每个星期三中午12点
(70 0 12 * * ?   每天中午12点触发
(80 15 10 ? * *   每天上午10:15触发
(90 15 10 * * ?     每天上午10:15触发
(100 15 10 * * ?   每天上午10:15触发
(110 15 10 * * ? 2005   2005年的每天上午10:15触发
(120 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发
(130 0/5 14 * * ?   在每天下午2点到下午2:55期间的每5分钟触发
(140 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(150 0-5 14 * * ?   在每天下午2点到下午2:05期间的每1分钟触发
(160 10,44 14 ? 3 WED   每年三月的星期三的下午2:102:44触发
(170 15 10 ? * MON-FRI   周一至周五的上午10:15触发
(180 15 10 15 * ?   每月15日上午10:15触发
(190 15 10 L * ?   每月最后一日的上午10:15触发
(200 15 10 ? * 6L   每月的最后一个星期五上午10:15触发
(210 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发
(220 15 10 ? * 6#3   每月的第三个星期五上午10:15触发
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值