@Async

Add the @async annotation to a method header that requires asynchronous operations, and add the @enableAsync annotation to the main method header to enable asynchronous operations.

@Scheduled

Add this annotation to methods that require scheduled tasks, along with the @enablescheduled annotation in the main method header to enable asynchronous operations.

An 🌰 :

@Scheduled(cron = "0 * * * * MON-FRI")
Copy the code

Methods marked by the above annotation will run once every 0 seconds (full second) Monday through Friday.

Cron expressions have six bits separated by Spaces:

[Second, minute, hour, day of month, month, day of week]

[0-59, 0-59, 0-23, 1-31, 1-12, 0-7] The last 0 and 7 represent Sundays

The Cron expression is used to mean per time unit, for example, in the second position for per minute.

Cron expressions can use ‘,’, ‘-‘, ‘/’, ‘? ‘represents different time intervals. Take the first digit (second) :

  • 1, 2, 3It is executed every 1,2,3 seconds
  • 1-3The effect of is consistent with the above, indicating that the interval is consistent, that is, it is executed every 1,2,3 seconds
  • 0/4Indicates that the command is executed every four seconds
  • ?It’s used for conflict matching

For more information: crontab.guru/

mail

Boot scenario: spring-boot-starter-mail

Necessary configuration for using mail:

Hostname, password, and host are the user name, password, and sending server of the sender respectively.

To send a simple message (without complex features like attachments), create an instance of SimpleMailMessage and use messageObject.setSubject(” subject “)

MessageObject. SetText (” content “)

Messageobject.sentto (” to “)

Messageobject.sentfrom (” sender “)

To construct mail

If the mail server needs to establish secure communication, you can configure this parameter:

Spring.mail.properties.mail.smtp.ssl.enable = true

To send complex emails, you need to create MimeMessage and MimeMessageHelper instances

Do Addattinfringement.

Reference:

Video tutorial

Cron expression generator👇