首页 > 其他分享 >简单的异步任务,邮件任务,定时执行任务

简单的异步任务,邮件任务,定时执行任务

时间:2023-01-16 23:24:10浏览次数:59  
标签:异步 import springframework 任务 org mail com public 邮件

@EnableAsync  //开启异步注解   ~@Async  配套
@EnableScheduling //开启定时功能的注解  ~@Scheduled 配套
@SpringBootApplication
public class Springboot09TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }
}

 

1.简单异步任务

 1.1AsyncService.java

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {

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

 

1.2AsyncController.java

import com.zxy.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsyncController {

    @Autowired
    AsyncService asyncService;

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

 

2.邮件任务(需要在主方法上添加启动注解@EnableAsync //开启异步注解 ~@Async 配套)

2.1依赖

       <!-- javax.mail邮件依赖       -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>        

 

2.2配置文件

[email protected]
spring.mail.password=yegwqubhbuaoiafc
spring.mail.host=smtp.qq.com
#开启加密验证   QQ需要设置ssl加密   其他邮件不需要
spring.mail.properties.mail.smtp.ssl.enable=true

 

2.3简单邮件和复杂邮件的编写

package com.zxy;

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 Springboot09TestApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

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

        mailMessage.setSubject("zxy你好!");  //主题
        mailMessage.setText("谢谢你的学习");   //内容
        mailMessage.setTo("[email protected]"); //收件人
        mailMessage.setFrom("[email protected]"); //发件人

        mailSender.send(mailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();//可以方法创建  也可以new MimeMailMessage()
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //true开启多文件   helper是   MimeMessage的实例       
        helper.setSubject("zxy你好啊 plus");
        helper.setText("<p style='color:red'>谢谢你zxy的努力学习!plus+</p>",true);  //后面下true开启html识别
        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\zxy\\Desktop\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\zxy\\Desktop\\1.jpg"));

        helper.setTo("[email protected]");
        helper.setFrom("[email protected]");

        mailSender.send(mimeMessage);
    }

}

 

3.定时执行任务(需要在主方法上添加启动注解@EnableScheduling //开启定时功能的注解 ~@Scheduled 配套)

@Service
public class ScheduledService {

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

    //cron表达式
    //秒 分 时  日 月 周几
    //0 35 20 * * ?   表示  每天20.35分 执行一次
    // 0 0/5 12,18  * * ?   表示  每天下午2点到6点 每隔5分钟执行一次
    @Scheduled(cron = "0 37 20 * * ?")  //? 代表需要管星期几   1-7表示星期一到日   0表示星期日
    public void hello(){
        System.out.println("hello,你被执行了");
    }

}

 

标签:异步,import,springframework,任务,org,mail,com,public,邮件
From: https://www.cnblogs.com/kidzxy/p/17056678.html

相关文章