Skip to content

Commit 5315896

Browse files
committed
task
1 parent a7d39b9 commit 5315896

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

SpringBoot高级教程.md

+133
Original file line numberDiff line numberDiff line change
@@ -2020,8 +2020,141 @@ public class AsynController {
20202020

20212021
### 2、定时任务
20222022

2023+
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutorTaskScheduler 接口。
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-7SUN-SAT 0,7SUN | , - * ? / 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+
20232111
### 3、邮件任务
20242112

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+
#### 2Spring 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+
20252158
## 十三、SpringBoot的安全
20262159

20272160
## 十四、SpringBoot的分布式

springboot-12-task/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<groupId>org.springframework.boot</groupId>
3030
<artifactId>spring-boot-starter-web</artifactId>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-mail</artifactId>
35+
</dependency>
3236

3337
<dependency>
3438
<groupId>org.springframework.boot</groupId>

springboot-12-task/src/main/java/com/cuzz/task/Springboot12TaskApplication.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.scheduling.annotation.Async;
66
import org.springframework.scheduling.annotation.EnableAsync;
7+
import org.springframework.scheduling.annotation.EnableScheduling;
78

89

910
@EnableAsync
11+
@EnableScheduling
1012
@SpringBootApplication
1113
public class Springboot12TaskApplication {
1214

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cuzz.task.service;
2+
3+
import com.sun.media.jfxmedia.logging.Logger;
4+
import org.springframework.scheduling.annotation.Scheduled;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
10+
/**
11+
* @Author: cuzz
12+
* @Date: 2018/9/29 10:25
13+
* @Description:
14+
*/
15+
@Service
16+
public class ScheduledService {
17+
18+
// 表示周一到周六当秒为0时执行一次
19+
@Scheduled(cron = "0 * * * * MON-SAT")
20+
public void hello() {
21+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22+
String date = sdf.format(new Date());
23+
System.out.println(date + " hello...");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.mail.username[email protected]
2+
spring.mail.password=xxxxxxxxxxx
3+
spring.mail.host=smtp.qq.com
4+
spring.mail.properties.mail.stmp.ssl.enable=true

springboot-12-task/src/test/java/com/cuzz/task/Springboot12TaskApplicationTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,31 @@
22

33
import org.junit.Test;
44
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.mail.SimpleMailMessage;
8+
import org.springframework.mail.javamail.JavaMailSenderImpl;
69
import org.springframework.test.context.junit4.SpringRunner;
710

811
@RunWith(SpringRunner.class)
912
@SpringBootTest
1013
public class Springboot12TaskApplicationTests {
1114

15+
@Autowired
16+
JavaMailSenderImpl mailSender;
17+
1218
@Test
1319
public void contextLoads() {
20+
SimpleMailMessage message = new SimpleMailMessage();
21+
22+
message.setSubject("Hello World");
23+
message.setText("text");
24+
25+
message.setTo("[email protected]");
26+
message.setFrom("[email protected]");
27+
28+
mailSender.send(message);
29+
1430
}
1531

1632
}

0 commit comments

Comments
 (0)