首页 > 技术文章 > SpringBoot三种任务

Liuyunsan 2021-12-05 17:19 原文

任务

  • 异步任务

启动类加上@EnableAsync注解

@EnableAsync
@SpringBootApplication
public class Springboot10MessionyibuApplication {

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

}

将方法标注为异步方法 @Async

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

}
  • 定时任务

有关接口
public interface TaskExecutor extends Executor
public interface TaskScheduler

主启动类添加@EnableScheduling注解

@EnableAsync   //开启异步注解功能
@EnableScheduling //开启注解定时功能
@SpringBootApplication
public class Springboot10MessionyibuApplication {

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

}

在方法前加@Scheduld注解声明

@Service
public class ScheduledService {

    //在一个特定时间执行这个方法  Timer
   //String cron() default ""; cron表达式
                    //秒 分 时 日 月 年 周几  //0-7 每一天
    @Scheduled(cron = "0 1 19 * * ?")  //什么时候执行
    public void hello(){
        System.out.println ("hello,你被执行了");
    }
}

cron表达式

计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思。在Linux中,我们经常用到 cron 服务器来完成这项工作。cron服务器可以根据配置文件约定的时间来执行特定的任务。

  • 邮件任务

导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

开启qq邮箱中Smtp服务

配置文件

spring.mail.username=xxxxxxxxx@qq.com
spring.mail.password=xxxxxxxxxxxx
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

编写内容测试

//一般放到Controller或着封装为一个方法
@SpringBootTest
class Springboot10MessionyibuApplicationTests {
    @Autowired
    JavaMailSender javaMailSender;
    @Test
    //简单邮件发送
    void contextLoads() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage ();

        simpleMailMessage.setSubject ("jsp 你好");
        simpleMailMessage.setText ("谢谢");
        simpleMailMessage.setTo ("xxxxxxxxx@qq.com");
        simpleMailMessage.setFrom ("xxxxxxxxx@qq.com");

        javaMailSender.send (simpleMailMessage);
    }
    @Test
        //复杂邮件发送
    void contextLoads1() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage ();

        //组装
        MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
        helper.setSubject ("slla");
        helper.setText ("<p>xxxx<p>",true);//html样式
        helper.setTo ("xxxxxxxxx@qq.com");
        helper.setFrom ("xxxxxxxxx@qq.com");
        //附件
        helper.addAttachment ("1.jpg",new File ("C:\\Users\\Administrator\\Desktop\\1.jpg"));
        javaMailSender.send (mimeMessage);
    }
     //封装为一个工具类
    /**
    * @Param html
    * @Param subject
    * @throws MessagingException
    * @Author Liuyunsan
    * */
    public void sendMail(Boolean html, String  subject,String text,String to,String from,String Filename,String Filepath) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage ();

        //组装
        MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
        helper.setSubject (subject);
        helper.setText (text,html);//html样式
        helper.setTo (to);
        helper.setFrom (from);
        //附件
        helper.addAttachment (Filename,new File (Filepath));
        javaMailSender.send (mimeMessage);
    }

}

推荐阅读