lilele200706

 

SpringBoot之任务

任务

  • 异步任务

  • 定时任务

  • 邮件任务

一、异步任务

  1. 编写任务类

@Service
public class AsyncService {
   @Async//异步注解
   public void hello(){
       try {
           Thread.sleep(3000);
      } catch (InterruptedException e) {
           e.printStackTrace();
      }
       System.out.println("数据正在处理中");
  }
}
  1. 调用任务类

@RestController
public class AsyncController {
   @Autowired
   AsyncService asyncService;
   @RequestMapping("/hello")
   public String hello(){
       asyncService.hello();
       return "OK";
  }
}
  1. 开启异步注解

@SpringBootApplication
@EnableScheduling //开启定时任务
@EnableAsync //开启异步任务
public class Springboot06SecurityApplication {
   public static void main(String[] args) {
       SpringApplication.run(Springboot06SecurityApplication.class, args);
  }
}
  1. 启动测试

二、邮件任务

1.javaSE

导包

 <!--mail-->
<dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1.1</version>
</dependency>
<dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mail</artifactId>
   <version>1.4.7</version>
</dependency>

 

 @Test
   void contextLoads() throws GeneralSecurityException, MessagingException {
       //1.设置QQ邮件服务器,邮件发送协议,验证用户名密码
       // 创建Properties 类用于记录邮箱的一些属性
       Properties props = new Properties();
       // 表示SMTP发送邮件,必须进行身份验证
       props.put("mail.smtp.auth", "true");
       //此处填写SMTP服务器
       props.put("mail.smtp.host", "smtp.qq.com");
       //端口号,QQ邮箱端口号
       props.put("mail.smtp.port", "587");
       // 此处填写你的账号
       props.put("mail.user", "qq@qq.com");
       // 此处的密码就是前面说的16位STMP口令
       props.put("mail.password", "授权码");

       // 2.构建授权信息,用于进行SMTP进行身份验证
       Authenticator authenticator = new Authenticator() {
           protected PasswordAuthentication getPasswordAuthentication() {
               // 用户名、密码
               String userName = props.getProperty("mail.user");
               String password = props.getProperty("mail.password");
               return new PasswordAuthentication(userName, password);
          }
      };
       //3. 使用环境属性和授权信息,创建邮件会话
       Session mailSession = Session.getInstance(props, authenticator);
       //4. 创建邮件消息
       MimeMessage message = new MimeMessage(mailSession);
       // 设置发件人
       InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
       message.setFrom(form);
       // 设置收件人的邮箱
       InternetAddress to = new InternetAddress("qq@qq.com");
       message.setRecipient(Message.RecipientType.TO, to);
       // 设置邮件标题
       message.setSubject("请点击以下链接激活您的邮箱");
       // 设置邮件的内容体
       message.setContent("<html lang='zh-CN'><head ><meta charset='utf-8'>"
                       + "</head><body>您好,欢迎订阅Victoria,请点击以下链接确认激活您的邮箱!<br><br>"
                       + "</body></html>",
               "text/html;charset=utf-8");
       //5. 发送邮件
       Transport.send(message);
  }

2.SpringBoot之邮箱任务

  1. 导包

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  1. 编写配置文件

spring.mail.username=644037316@qq.com
spring.mail.password=rxxgbgjdzzyebahe
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=ture

3.编写任务类

//简单的邮件
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
   SimpleMailMessage mailMessage = new SimpleMailMessage();
   
   mailMessage.setSubject("邮箱任务");
   mailMessage.setText("SpringBoot之邮箱任务");
   mailMessage.setTo("644037316@qq.com");
   mailMessage.setFrom("644037316@qq.com");
   
   mailSender.send(mailMessage);
}
//复杂的邮件
@Test
void contextLoads2() throws MessagingException {
   MimeMessage mimeMessage = mailSender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
   helper.setSubject("复杂的邮件任务");
   helper.setText("SpringBoot之邮件任务",true);
   helper.addAttachment("1.png",new File("C:\\Users\\Administrator\\Pictures\\1.png"));
   helper.setTo("644037316@qq.com");
   helper.setFrom("644037316@qq.com");
   mailSender.send(mimeMessage);
}

3.定时任务

  1. 编写一个任务类


@Controller\\加到容器里
public class ScheduledController {
   //                 秒 分 时 日 月 周
   @Scheduled(cron = "0/10 * * * * ?")//每隔10秒执行一次
   public void hello(){
       System.out.println("Hello,SpringBoot之定时任务");
  }
}
  1. 开启定时任务

@SpringBootApplication
@EnableScheduling //开启定时任务
public class Springboot06SecurityApplication {
   public static void main(String[] args) {
       SpringApplication.run(Springboot06SecurityApplication.class, args);
  }
}
  1. 启动测试

posted on 2021-12-17 20:53  lilele200706  阅读(143)  评论(0编辑  收藏  举报

导航