任务调度的是三种方式

  • 异步任务

    @Service
    public class AsyncService {
        
        @Async   //告诉Spring,这是一个异步方法
        public void hello()  {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("数据正在处理......");
        }
    }
    
    
    @SpringBootApplication
    @EnableAsync//开启异步
    public class SpringboortTaskApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringboortTaskApplication.class, args);
        }
    }
    
  • 定时任务

    @EnableScheduling//开启定时功能的注解
    @Schedule
    
    @Service
    public class ScheduledService {
    
        /*在某一个特定的时间执行该方法~ Timer
        cron
        * 秒 分 时 日 月 星期
        * "0 *  *  *  * 0-7"
        * 0 26 19 * * ? 每天19点26分 执行一次
        * 30 0/10 4 10 * * ? 每天10点和4点,每隔10分钟执行一次
        */
        @Scheduled(cron = "0/2 * * * * ?")
        public void hello(){
            System.out.println("你被逮捕了!lwh");
        }
    }
    
  • 邮件任务

    • application.yml

      spring:
        mail:
          username: 2861184805@qq.com
          host: smtp.qq.com
          properties:
            mail:
              smtp:
                ssl:
                  enable:true1
          password: raaipltxzfqydcha测试类
      
    • 测试类

      @Autowired
          JavaMailSenderImpl javamail;
      
          @Test
          void contextLoads() {
              //简单的邮件
              SimpleMailMessage message = new SimpleMailMessage();
              message.setSubject("xiaobear,你好");
              message.setText("你是最棒的!");
              message.setTo("2861184805@qq.com");
              message.setFrom("2861184805@qq.com");
              javamail.send(message);
          }
          @Test
          void contextLoads2() throws MessagingException {
              //复杂的邮件
              MimeMessage message = javamail.createMimeMessage();
              //组装
              MimeMessageHelper helper = new MimeMessageHelper(message,true);
              helper.setSubject("xiaobear,你好!加油哦");
              helper.setText("<p>你是最棒的!</p>",true);
              //附件
              helper.addAttachment("升级锁的过程.png",new File("D:\\360MoveData\\Users\\Administrator\\Desktop\\升级锁的过程.png"));
              helper.setTo("2861184805@qq.com");
              helper.setFrom("2861184805@qq.com");
              javamail.send(message);
          }
      //封装成一个方法
          public void sendMail(Boolean html,String subject,......)throws MessagingException{
              //复杂的邮件
              MimeMessage message = javamail.createMimeMessage();
              //组装
              MimeMessageHelper helper = new MimeMessageHelper(message,true);
              helper.setSubject("xiaobear,你好!加油哦");
              helper.setText("<p>你是最棒的!</p>",true);
              //附件
              helper.addAttachment("升级锁的过程.png",new File("D:\\360MoveData\\Users\\Administrator\\Desktop\\升级锁的过程.png"));
              helper.setTo("2861184805@qq.com");
              helper.setFrom("2861184805@qq.com");
              javamail.send(message);
          }
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊学Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值