Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Asynchronous, timing and Mail tasks in Springboot

Shulou Source: shulou.com Published: 2022-06-02 19:18:55 10月27日 Update

This article mainly introduces the example analysis of asynchronous, timing and mail tasks in Springboot, which is very detailed and has certain reference value. Friends who are interested must finish it!

1. Asynchronous tasks 1. Write a class AsyncService

Asynchronous processing is still very common. For example, when we send an email on a website, the backend will send the email, and the foreground will cause the response to remain motionless, and the response will not be successful until the email is sent. Therefore, we usually use a multi-threaded approach to deal with these tasks.

Package com.rk.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncService {public void hello () {try {System.out.println ("data processing ~"); Thread.sleep (3000); / / stop for three seconds} catch (InterruptedException e) {e.printStackTrace () Write an AsyncController class package com.rk.controller;import com.rk.service.AsyncService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class AsyncController {@ Autowired AsyncService asyncService; @ GetMapping ("/ hello") public String hello () {asyncService.hello (); return "success";}}

Now start the project for testing, and it will take three seconds for success to appear. It is not asynchronous yet.

3. Turn on asynchronous @ Async// and tell spring that this is an asynchronous method public void hello () {try {System.out.println ("data processing ~"); Thread.sleep (3000); / / stop for three seconds} catch (InterruptedException e) {e.printStackTrace () } @ EnableAsync// enables the asynchronous annotation function @ SpringBootApplicationpublic class Springboot09TestApplication {public static void main (String [] args) {SpringApplication.run (Springboot09TestApplication.class, args) Second, Mail Task 1, introduce dependency org.springframework.boot spring-boot-starter-mail 2, configure mail# username spring.mail.username=1624603357@qq.com# password spring.mail.password=yblyxhvmnsurbbci# send mail server spring.mail.host=smtp.qq.com# turn on encryption verification sslspring.mail.properties.mail.smtp.ssl.enable=true3, test

Simple email

@ Autowired JavaMailSenderImpl mailSender; @ Test void contextLoads () {SimpleMailMessage mailMessage = new SimpleMailMessage (); mailMessage.setSubject ("Hello, rk"); / / email title mailMessage.setText ("Test email"); / / email mailMessage.setTo ("r1624603357@126.com"); / / recipient mailbox mailMessage.setFrom ("1624603357@qq.com") / / Sender mailbox mailSender.send (mailMessage);}

Complex mail

@ Test void contextLoads2 () throws MessagingException {/ / A complex email MimeMessage mimeMessage = mailSender.createMimeMessage (); / / Assembly MimeMessageHelper helper = new MimeMessageHelper (mimeMessage, true); / / body helper.setSubject ("Hello, rk"); helper.setText ("Test email", true) / / attachment helper.addAttachment ("1.png", new File ("D:\\ QLDownloadGame\\ 2\ 1.png"); helper.addAttachment ("rk.docx", new File ("E:\\ desktop\\ rk.docx"); / / sender / recipient helper.setTo ("r1624603357@126.com"); helper.setFrom ("1624603357@qq.com") / / send mailSender.send (mimeMessage);}

3. Scheduled tasks 1. Write a ScheduledService class @ Servicepublic class ScheduledService {/ / seconds, hours, days, months and weeks / / 0 * MON-FRI / / pay attention to the use of cron expressions Execute the method @ Scheduled (cron = "0 28 20 * * 0-7") public void hello () {System.out.println ("it's 20:28"); System.out.println ("hello.");}} every day at 0 seconds at 20:28

The hello method is executed at 20:28:00 every day after the project starts.

2. Add annotation @ EnableAsync// to enable asynchronous annotation function @ EnableScheduling// enable timing function note @ SpringBootApplicationpublic class Springboot09TestApplication {public static void main (String [] args) {SpringApplication.run (Springboot09TestApplication.class, args);}}

Cron expression exercise

/ *

[00pep 5 14jue 18 * *?] 14:00 and 18:00 sharp every day, every 5 minutes

[0 15 10? * 1-6] every month at 10:15 on Mondays and Saturdays

[0 0 2? * 6L] executed at 2: 00 a.m. on the last Saturday of every month.

[0 02 LW *? ] the last working day of every month is executed at 2: 00 a. M.

[0 02-4? * 1: 1] every hour is performed on the first Monday of every month from 2: 00 a.m. to 4: 00 a.m.

, /

The above is all the contents of this article entitled "sample Analysis of Asynchronous, timed, and email tasks in Springboot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Email tasks annotations processing testing functions methods examples analysis complexity hello content recipients data data processing articles expressions mailboxes projects success Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker vpn MySQL Shulou Technology