SpringBoot: asynchronous processing

Asynchronous Processing

Project structure

  1. Creating SpringBoot project, add a web support

  2. Write hello project

    @RestController
    public class AsyncController {
    
        @Autowired
        AsyncService asyncService;
    
        @GetMapping("/hello")
        public String hello(){
            asyncService.asynService();
            return "OK";
        }
    
    }

     

  3. Analog Delay

    @Service
     public  class AsyncService { 
    
        public  void asynService () {
             the try { 
                the Thread.sleep ( 3000 ); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
            System.out.println ( "Data Processing ..." ) ; 
        } 
    
    
    }

     

    Browser access, you need to wait until three seconds, now help us solve by SpringBoot

  4. SpringBoot open asynchronous processing

    Analogue delay class

    @Service
      public  class AsyncService { 
         // tell spring is a need for a method that asynchronous processing 
         @Async
          public  void asynService () {
              the try { 
                 the Thread.sleep ( 3000 ); 
             } the catch (InterruptedException E) { 
                 e.printStackTrace (); 
             } 
             System.out.println ( "data processing ..." ); 
         } 
     }

     

    The main program category

    //开启异步处理
     @EnableAsync
     @SpringBootApplication
     public class Springboot09AsyncApplication {
     ​
         public static void main(String[] args) {
             SpringApplication.run(Springboot09AsyncApplication.class, args);
         }
     ​
     }

     

    Send e-mail

    1, import-dependent

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

     

    2, the configuration file

    Configuration information of the sender

    = 649 318 307 spring.mail.username @ qq.com 
     spring.mail.password = csnfxxjqxkgibeja 
     spring.mail.host = smtp.qq.com 
     # QQ messages sent need to open the encrypted authentication 
     spring.mail.properties.mail.smtp.ssl. enable = to true
     

    3, send e-mail

    JavaMailSenderImpl class is a good package, for sending mail

    @SpringBootTest
      class Springboot09AsyncApplicationTests { 
         @Autowired 
         JavaMailSenderImpl javaMailSender; 
         // simple mail 
         @Test
          void contextLoads () { 
             SimpleMailMessage MailMessage = new new SimpleMailMessage (); 
             // title 
             mailMessage.setSubject ( "Hello stuff in the zxh" );
              // text 
             mailMessage.setText ( "I wish you happy !!" ); 
             // specify a sender 
             mailMessage.setFrom ( "[email protected]" );
              // specify the recipient
             mailMessage.setTo ( "[email protected]" ); 
             javaMailSender.send (MailMessage); 
         } 
         // complex messages 
         @Test
          void senderMail () throws MessagingException { 
             the MimeMessage MimeMessage = javaMailSender.createMimeMessage ();
          // assembled ~, open file upload 
             MimeMessageHelper Helper = new new MimeMessageHelper (MimeMessage, to true );
              // title 
             helper.setSubject ( "Hello stuff in the zxh" );
              // text, using html parsing 
             helper.setText ( "<p style = ' color: red;'> I wish you a happy !! </ p> ",true);
             //附件
             helper.addAttachment("术语.md", new File("C:\\Users\\HP\\Desktop\\术语.md"));
     ​
             helper.setFrom("[email protected]");
             helper.setTo("[email protected]");
     ​
             javaMailSender.send(mimeMessage);
         }
     ​
     }

    Scheduled Task

    SpringBoot comes with four objects on timed tasks:

    • @Scheduled: When execution

      • cron expression

    • @EnableScheduling: open timer

    • TaskExecutor: mandate holders

    • TaskScheduler: Task Scheduler

    1, regular tasks

    @Service
     public  class ScheduledService {
         // tell SpringBoot when executed
         // cron expression: the sun and moon seconds timeshare week, once every two seconds to perform 
        @Scheduled (cron = "0/2 * * * *?" )
         Public  void the Hello () { 
            System.out.println ( "Hello" ); 
        } 
    }

    2, open the regular tasks

     //开启定时功能
     @EnableScheduling
     @SpringBootApplication
     public class Springboot09AsyncApplication {
     ​
         public static void main(String[] args) {
             SpringApplication.run(Springboot09AsyncApplication.class, args);
         }
     ​
     }

     

     

     

     

Guess you like

Origin www.cnblogs.com/zxhbk/p/12468315.html