@@ -2020,8 +2020,141 @@ public class AsynController {
2020
2020
2021
2021
### 2 、定时任务
2022
2022
2023
+ 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring 为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
2024
+
2025
+ cron表达式:
2026
+
2027
+ | 字段 | 允许值 | 允许的特殊字符 |
2028
+ | : -- - | : ---------------------- | : ---------------- |
2029
+ | 秒 | 0 - 59 | , - * / |
2030
+ | 分 | 0 - 59 | , - * / |
2031
+ | 小时 | 0 - 23 | , - * / |
2032
+ | 日期 | 1 - 31 | , - * ? / L W C |
2033
+ | 月份 | 1 - 12 | , - * / |
2034
+ | 星期 | 0 - 7 或SUN - SAT 0 ,7 是SUN | , - * ? / L C # |
2035
+
2036
+ 含义:
2037
+
2038
+ | 特殊字符 | 代表含义 |
2039
+ | -------- | -------------------------- |
2040
+ | , | 枚举 |
2041
+ | - | 区间 |
2042
+ | * | 任意 |
2043
+ | / | 步长 |
2044
+ | ? | 日/ 星期冲突匹配 |
2045
+ | L | 最后 |
2046
+ | W | 工作日 |
2047
+ | C | 和calendar联系后计算过的值 |
2048
+ | # | 星期,4 #2 ,第2 个星期四 |
2049
+
2050
+ 主要有@Scheduled注解,cron ()方法
2051
+
2052
+ ```java
2053
+ public @interface Scheduled {
2054
+
2055
+ /**
2056
+ * A cron-like expression, extending the usual UN*X definition to include triggers
2057
+ * on the second as well as minute, hour, day of month, month and day of week.
2058
+ * <p >E.g. {@code "0 * * * * MON-FRI" } means once per minute on weekdays
2059
+ * (at the top of the minute - the 0th second).
2060
+ * @return an expression that can be parsed to a cron schedule
2061
+ * @see org.springframework.scheduling.support.CronSequenceGenerator
2062
+ */
2063
+ String cron () default "";
2064
+ }
2065
+ ```
2066
+
2067
+ 测试类
2068
+
2069
+ ```java
2070
+ /**
2071
+ * @Author: cuzz
2072
+ * @Date: 2018/9/29 10:25
2073
+ * @Description:
2074
+ */
2075
+ @Service
2076
+ public class ScheduledService {
2077
+
2078
+ // 表示周一到周六当秒为0时执行一次
2079
+ @Scheduled (cron = " 0 * * * * MON-SAT" )
2080
+ public void hello () {
2081
+ SimpleDateFormat sdf = new SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" );
2082
+ String date = sdf. format(new Date ());
2083
+ System . out. println(date + " hello..." );
2084
+ }
2085
+ }
2086
+ ```
2087
+
2088
+ 开启定时任务注解@EnableScheduling
2089
+
2090
+ ```java
2091
+ @EnableAsync
2092
+ @EnableScheduling
2093
+ @SpringBootApplication
2094
+ public class Springboot12TaskApplication {
2095
+
2096
+ public static void main (String [] args ) {
2097
+ SpringApplication . run(Springboot12TaskApplication . class, args);
2098
+ }
2099
+ }
2100
+ ```
2101
+
2102
+ 测试
2103
+
2104
+ ```
2105
+ 2018 - 09- 29 10 : 48 : 00 hello. ..
2106
+ 2018 - 09- 29 10 : 49 : 00 hello. ..
2107
+ ```
2108
+
2109
+
2110
+
2023
2111
### 3 、邮件任务
2024
2112
2113
+ #### 1 、邮件发送需要引入spring- boot- starter- mail
2114
+
2115
+ ```xml
2116
+ < dependency>
2117
+ < groupId> org. springframework. boot< / groupId>
2118
+ < artifactId> spring- boot- starter- mail< / artifactId>
2119
+ < / dependency>
2120
+ ```
2121
+
2122
+ #### 2 、Spring Boot 自动配置MailSenderAutoConfiguration
2123
+
2124
+ #### 3 、定义MailProperties 内容,配置在application. properties中
2125
+
2126
+ ```properties
2127
+ spring. mail. username= 214769277 @qq . com
2128
+ spring. mail. password= xxxxxxxxxxx
2129
+ spring. mail. host= smtp. qq. com
2130
+ spring. mail. properties. mail. stmp. ssl. enable= true
2131
+ ```
2132
+
2133
+ #### 4 、自动装配JavaMailSender
2134
+
2135
+ ```java
2136
+ @Autowired
2137
+ JavaMailSenderImpl mailSender;
2138
+
2139
+ @Test
2140
+ public void contextLoads() {
2141
+ SimpleMailMessage message = new SimpleMailMessage ();
2142
+
2143
+ message. setSubject(" Hello World" );
2144
+ message. setText(" text" );
2145
+
2146
+ message
. setTo(
" [email protected] " );
2147
+ message
. setFrom(
" [email protected] " );
2148
+
2149
+ mailSender. send(message);
2150
+
2151
+ }
2152
+ ```
2153
+
2154
+
2155
+
2156
+ #### 5 、测试邮件发送
2157
+
2025
2158
## 十三、SpringBoot 的安全
2026
2159
2027
2160
## 十四、SpringBoot 的分布式
0 commit comments