Spring Boot and tasks and email

Asynchronous tasks, scheduled tasks, mail tasks

1. Asynchronous tasks
In Java applications, interaction processing is achieved in a synchronous manner in most cases; however, when processing interactions with third-party systems, it is easy to cause a slow response, and most of them have been used before Multi-threaded to complete such tasks, in fact, after Spring 3.x, @Async has been built in to perfectly solve this problem.

Two annotations:
@EnableAysnc, @Aysnc

2. Scheduled tasks During
project development, you often need to perform some scheduled tasks. For example, you need to analyze the log information of the previous day every morning. Spring provides us with a way to perform task scheduling asynchronously and provides TaskExecutor and TaskScheduler interfaces.

Two annotations: @EnableScheduling, @Scheduled

cron expression:
1

@Service
public class ScheduledService {

    /**
     * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
     * 0 * * * * MON-FRI
     *  【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
     *  【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
     *  【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
     *  【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
     *  【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
     */
   // @Scheduled(cron = "0 * * * * MON-SAT")
    //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
   // @Scheduled(cron = "0-4 * * * * MON-SAT")
    @Scheduled(cron = "0/4 * * * * MON-SAT")  //每4秒执行一次
    public void hello(){
        System.out.println("hello ... ");
    }
}

Third, the principle of the mail task
:
5

  1. Mail sending needs to introduce spring-boot-starter-mail
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
  1. Spring Boot automatically configures MailSenderAutoConfiguration
@EnableAsync  //开启异步注解功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot04TaskApplication.class, args);
	}
}
  1. Define the content of MailProperties and configure it in application.yml
[email protected]
spring.mail.password=bskclqlghbdqfdfg
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.properties.mail.smtp.ssl.enable=true
  1. Test mail delivery
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot04TaskApplicationTests {

	@Autowired
	JavaMailSenderImpl mailSender;

	@Test
	public void contextLoads() {
		SimpleMailMessage message = new SimpleMailMessage();
		//邮件设置
		message.setSubject("通知-今晚开会");
		message.setText("今晚7:30开会");

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

		mailSender.send(message);
	}

	@Test
	public void test02() throws  Exception{
		//1、创建一个复杂的消息邮件
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		//邮件设置
		helper.setSubject("通知-今晚开会");
		helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);

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

		//上传文件
		helper.addAttachment("1.png",new File("D:\\Images\\1.png"));
		helper.addAttachment("2.png",new File("D:\\Images\\2.png"));

		mailSender.send(mimeMessage);

	}

}

Note:
When testing, you need to obtain its verification
2

Simple text test: test
3
with attachments:

4
Upload the complete source code later, thank you! ! !

By the way, my personal blog

Tianya Blog

Guess you like

Origin www.cnblogs.com/cainiaoxiaoxie/p/12722765.html