任务

作者: 忒无聊了叭 | 来源:发表于2020-04-14 17:05 被阅读0次

任务

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

异步任务

在开发中,我们可能经常会遇到一些需要执行时间很长的任务,用户一直卡在那儿等待或者转圈圈,给人体验不好。为了改善这种体验,可以使用异步来解决。

//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Springboot08Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot08Application.class, args);
    }
}

Service层

@Service
public class WaitService {
    //告诉spring这是一个异步的方法
    @Async
    public void hello() {
        try {
            Thread.sleep(3000);//模拟处理业务
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理中。。。");
    }
}

Controller层

@RestController
public class wait {

    @Autowired
    WaitService waitService;

    @RequestMapping("/hello")
    public String hello(){
        waitService.hello();
        return "OK";
    }
}

使用异步任务,可以先将数据返回给用户,而需要执行的代码会在后台自己进行运行。

邮件任务

需要去qq邮箱中设置-》账户中开启 POP3/SMTP服务

在properties中进行邮件的配置

spring.mail.username=1277524222@qq.com
spring.mail.password=********* # 这个就是qq邮箱那个开启服务的密码
spring.mail.host=smtp.qq.com

# qq的加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

在测试类中进行发送邮件

package com.szw;

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 javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class Springboot08ApplicationTests {

    @Autowired
    JavaMailSenderImpl javaMailSender;

    //一个简单的邮件
    @Test
    void contextLoads() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setSubject("这里是邮件主题");
        mailMessage.setText("这里是文本框");

        mailMessage.setTo("1277524222@qq.com"); //发送到
        mailMessage.setFrom("1277524222@qq.com");//发送者
        javaMailSender.send(mailMessage);
    }

    //一个复杂的邮件
    @Test
    void contextLoads2() throws MessagingException {
        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();
        //组装,可以用MimeMessageHelper去配置mimeMailMessage
        MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage,true);
        helper.setSubject("复杂的邮件");
        helper.setText("<p style='color:red'>这是我第二次发送邮件</p>",true);
        //附件
        helper.addAttachment("bg.jpg",new File("C:\\Users\\shaoz\\Pictures\\Camera Roll\\bg.jpg"));
        helper.setTo("1277524222@qq.com");
        helper.setFrom("1277524222@qq.com");
        javaMailSender.send(mimeMailMessage);
    }

}

定时执行任务

@Service
public class timeService {
    //用cron表达式进行定时任务
    @Scheduled(cron = "0/2 * * * * ? ")
    public void hello(){
        System.out.println("你好,我被执行了~~");
    }
}
//开启定时任务注解
@EnableScheduling
@SpringBootApplication
public class Springboot08Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot08Application.class, args);
    }
}

cron常用表达式例子

(1)0/2 * * * * ? 表示每2秒 执行任务

(1)0 0/2 * * * ? 表示每2分钟 执行任务

(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务

(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业

(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作

(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

(6)0 0 12 ? * WED 表示每个星期三中午12点

(7)0 0 12 * * ? 每天中午12点触发

(8)0 15 10 ? * * 每天上午10:15触发

(9)0 15 10 * * ? 每天上午10:15触发

(10)0 15 10 * * ? 每天上午10:15触发

(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发

(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发

(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发

(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发

(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发

(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发

(18)0 15 10 15 * ? 每月15日上午10:15触发

(19)0 15 10 L * ? 每月最后一日的上午10:15触发

(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发

(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发

(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

cron表达式校验器:http://www.bejson.com/othertools/cronvalidate/

相关文章

网友评论

      本文标题:任务

      本文链接:https://www.haomeiwen.com/subject/wevtvhtx.html