SpringBoot常用的三个功能(课时二十五)

三个小Demo理解三个功能:

案例中的pom.xml文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.4
         
    
    com.hu
    springboot-09
    0.0.1-SNAPSHOT
    springboot-09
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-mail
            test
        


        
            org.springframework.boot
            spring-boot-starter-security
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


第一部分 异步

用户需求的的内容:我现在将一个方法定时了3秒,我想问一个问题 如果让用户让他等三秒 用户直接会投诉

@Service
public class AsyncService {

    public  void  hellow(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理");
    }

控制层的信息

@RestController
public class AsyncController {

   AsyncService asyncService =new AsyncService();

    @RequestMapping("/hello")
    public String hello() {
        //停止三秒
        asyncService.hellow();
  
        return "ok";
    }

@Async告诉Spring是异步方法

 

第二部分 邮件

SpringBoot常用的三个功能(课时二十五)_第1张图片

 

 简单的邮件信息

package com.hu.springboot09;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
 public  class Springboot09ApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        //一个简单的邮件
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setSubject("小矿山");
        mailSender.setTest("谢谢你的课程");
        //f发送人
        mailSender.setTo("[email protected]");
        mailSender.setFrom("[email protected]");

    }

 

复杂的邮件

  MimeMessageHelper messageHelper = new MimeMessageHelper();
        //复杂邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组件
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            //正文
            helper.setSubject("小矿山");
            helper.setText("

",true); //附件 helper.addAttachment("1.jpg",new File("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Picture")); helper.addAttachment("2.jpg",new File("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Picture")); //f发送人 helper.setTo("[email protected]"); helper.setFrom("[email protected]"); mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); }

复杂的邮件方法

  /**
     *
     * @param html
     * @param subject
     * @param text
     * @throws MessagingException
     * @Author
     */
        public void SendMail(Boolean html,String subject,String text) throws MessagingException{
            //复杂邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            //正文
            helper.setSubject(subject);
            helper.setText(text,true);
            //附件
            helper.addAttachment("1.jpg",new File("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Picture"));
            helper.addAttachment("2.jpg",new File("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Picture"));
            //f发送人
            helper.setTo("[email protected]");
            helper.setFrom("[email protected]");
            mailSender.send(mimeMessage);
        }
    }

 

 

第三部分 定时

cron表达式格式:

{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}

cron表达式各占位符解释:

{秒数}{分钟} ==> 允许值范围: 0~59 ,不允许为空值,若值不合法,调度器将抛出SchedulerException异常
“*” 代表每隔1秒钟触发;
“,” 代表在指定的秒数触发,比如”0,15,45”代表0秒、15秒和45秒时触发任务
“-“代表在指定的范围内触发,比如”25-45”代表从25秒开始触发到45秒结束触发,每隔1秒触发1次
“/”代表触发步进(step),”/”前面的值代表初始值(““等同”0”),后面的值代表偏移量,比如”0/20”或者”/20”代表从0秒钟开始,每隔20秒钟触发1次,即0秒触发1次,20秒触发1次,40秒触发1次;”5/20”代表5秒触发1次,25秒触发1次,45秒触发1次;”10-45/20”代表在[10,45]内步进20秒命中的时间点触发,即10秒触发1次,30秒触发1次
{小时} ==> 允许值范围: 0~23 ,不允许为空值,若值不合法,调度器将抛出SchedulerException异常,占位符和秒数一样
{日期} ==> 允许值范围: 1~31 ,不允许为空值,若值不合法,调度器将抛出SchedulerException异常
{星期} ==> 允许值范围: 1~7 (SUN-SAT),1代表星期天(一星期的第一天),以此类推,7代表星期六(一星期的最后一天),不允许为空值,若值不合法,调度器将抛出SchedulerException异常
{年份} ==> 允许值范围: 1970~2099 ,允许为空,若值不合法,调度器将抛出SchedulerException异常

注意:除了{日期}和{星期}可以使用”?”来实现互斥,表达无意义的信息之外,其他占位符都要具有具体的时间含义,且依赖关系为:年->月->日期(星期)->小时->分钟->秒数

cron表达式经典案例:

  • “30 * * * * ?” 每半分钟触发任务

  • “30 10 * * * ?” 每小时的10分30秒触发任务

  • “30 10 1 * * ?” 每天1点10分30秒触发任务

  • “30 10 1 20 * ?” 每月20号1点10分30秒触发任务

  • “30 10 1 20 10 ? *” 每年10月20号1点10分30秒触发任务

  • “30 10 1 20 10 ? 2011” 2011年10月20号1点10分30秒触发任务

  • “30 10 1 ? 10 * 2011” 2011年10月每天1点10分30秒触发任务

  • “30 10 1 ? 10 SUN 2011” 2011年10月每周日1点10分30秒触发任务

  • “15,30,45 * * * * ?” 每15秒,30秒,45秒时触发任务

  • “15-45 * * * * ?” 15到45秒内,每秒都触发任务

  • “15/5 * * * * ?” 每分钟的每15秒开始触发,每隔5秒触发一次

  • “15-30/5 * * * * ?” 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次

  • “0 0/3 * * * ?” 每小时的第0分0秒开始,每三分钟触发一次

  • “0 15 10 ? * MON-FRI” 星期一到星期五的10点15分0秒触发任务

  • “0 15 10 L * ?” 每个月最后一天的10点15分0秒触发任务

  • “0 15 10 LW * ?” 每个月最后一个工作日的10点15分0秒触发任务

  • “0 15 10 ? * 5L” 每个月最后一个星期四的10点15分0秒触发任务

  • “0 15 10 ? * 5#3” 每个月第三周的星期四的10点15分0秒触发任务

SpringBoot常用的三个功能(课时二十五)_第2张图片

 

package com.hu.springboot09.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.AsyncService;
import service.ScheduledService;

@RestController
public class AsyncController {

   AsyncService asyncService =new AsyncService();
   ScheduledService scheduledService = new ScheduledService();
    @RequestMapping("/hello")
    public String hello() {
        //停止三秒
        asyncService.hellow();
        scheduledService.hello1();
        return "ok";
    }

}

启动程序

package com.hu.springboot09;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

//开启功能
@EnableAsync
@EnableScheduling//开启定时注解
@SpringBootApplication
public class Springboot09Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot09Application.class, args);
    }

}
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=56121:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideospringboot\springboot-09\target\classes;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.7.4\spring-boot-starter-web-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter\2.7.4\spring-boot-starter-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot\2.7.4\spring-boot-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.4\spring-boot-autoconfigure-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.7.4\spring-boot-starter-logging-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.7.4\spring-boot-starter-json-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.4\jackson-databind-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.4\spring-boot-starter-tomcat-2.7.4.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar" com.hu.springboot09.Springboot09Application

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.4)

2022-10-05 20:32:51.731  INFO 26304 --- [           main] c.h.s.Springboot09Application            : Starting Springboot09Application using Java 1.8.0_171 on HELLOWWORLD with PID 26304 (D:\maven-workspace\spaceVideospringboot\springboot-09\target\classes started by MZFAITHDREAM in D:\maven-workspace\spaceVideospringboot\springboot-09)
2022-10-05 20:32:51.736  INFO 26304 --- [           main] c.h.s.Springboot09Application            : No active profile set, falling back to 1 default profile: "default"
2022-10-05 20:32:53.313  INFO 26304 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-10-05 20:32:53.324  INFO 26304 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-05 20:32:53.325  INFO 26304 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-05 20:32:53.571  INFO 26304 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-05 20:32:53.571  INFO 26304 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1768 ms
2022-10-05 20:32:53.922  INFO 26304 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-05 20:32:53.940  INFO 26304 --- [           main] c.h.s.Springboot09Application            : Started Springboot09Application in 2.843 seconds (JVM running for 4.093)
2022-10-05 20:33:17.233  INFO 26304 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-10-05 20:33:17.233  INFO 26304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-10-05 20:33:17.234  INFO 26304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
数据正在处理
hello 什么方法被执行了

不管前方的路有多苦,只要走的方向正确,不管多么崎岖不平,都比站在原地更接近幸福。

 

你可能感兴趣的:(SpringBoot,分布式)