Environment: IDEA version 2017.3.1X64, JDK1.8, SpringBoot2.1.1

Asynchronous tasks

  • Annotate @async for services where asynchrony is required
@service public class AsyncService {// Tell SpringBoot that this is an asynchronous task and SpringBoot will automatically start a thread to execute @async public voidtestAsyncService(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Asynchronous execution succeeded"); }}Copy the code
  • Add enable asynchronous annotation to the main configuration class: @enableAsync
@ EnableAsync / / open the asynchronous annotation function public class SpringbootMybatisApplication {Copy the code

Timing task

  • Add annotations to services that need to enable scheduled tasks@Scheduled(cron = "0 * * * * MON-SAT")
/* {seconds} {minutes} {hours} {date} {month} {week} {year (empty)} * The six cron symbols correspond to the above time units, separated by Spaces * * to indicate all values; *? Represents an unspecified value, that is, does not care what value it is; * - represents a specified range; *, indicating that a possible value is appended; * / indicates the start time before the symbol, and each increment value after the symbol; */ @Service public class ScheduledService { @Scheduled(cron ="0 * * * * MON-SAT")
    public void testSchedule(){
        System.out.println("Test scheduled task succeeded"); }}Copy the code

  • Enable scheduled task annotation on the main configuration class: @enablesCheduling

The mail task

  • Introduce mail dependent components
<! -- Import mail, if found that the injection failed, <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>Copy the code

Possible errors: Injection failed (you can download the jar from the maven website and put it in the corresponding folder) :

  • Mailbox Enable the POP3/SMTP service

  • Configure mailbox parameters in the master configuration file (in YML mode)

Spring: mail: username: [email protected] password: XXXXXX // Obtain the authorization code from the service options host: smtp.qq.com // QQ email server properties: mail: smtp: ssl:enable: true// Enable the secure connectionCopy the code
  • Test Email sending
@Autowired JavaMailSenderImpl mailSender; /** * create a simple message */ @test public voidtestMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("That's the theme.");
        message.setText("This is the content."); // message.setto ("[email protected]"); // Message.setFrom ("[email protected]"); mailSender.send(message); } /** * create complex message mail */ @test public voidtestMail02() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("This is the subject of a complex message.");
        helper.setText(" This is a complex message message content ".true); // addAttachment 1 helper.addattglide ("1.jpg",new File("E:\\desktop\\8234.jpg")); // addAttachment 2 helper.addattglide ("2.docx",new File("E:\\desktop\\ Situation and Policy coursework.docx")); // Recipient helper.setto ("[email protected]"); // The sender helper.setFrom("[email protected]");
        mailSender.send(mimeMessage);
    }



Copy the code

Test success

More Spring Boot integration can be found at malizhi.cn