Skip to content

Commit 465ad8d

Browse files
committed
添加代码示例springboot-schedule
1 parent e7f352a commit 465ad8d

File tree

11 files changed

+350
-1
lines changed

11 files changed

+350
-1
lines changed

app-manage-api/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ logging:
6868
ROOT: INFO
6969
com:
7070
xncoding: DEBUG
71-
file: E:/logs/pos-api.log
71+
file: E:/logs/app-manage-api.log
7272

7373
---
7474

springboot-schedule/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*

springboot-schedule/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-schedule/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 定时任务
2+
3+
定时任务非常简单,只需要写个配置类,然后定义定时任务类,使用注解定义某个方法定期执行
4+
5+
``` java
6+
@Scheduled(cron = "0 26 19 * * ?")
7+
public void checkState1() {
8+
logger.info(">>>>> xxx检查开始....");
9+
logger.info(">>>>> xxx传检查完成....");
10+
}
11+
```
12+
13+
## 许可证
14+
15+
Copyright (c) 2018 Xiong Neng
16+
17+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-schedule/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-schedule</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-schedule</name>
13+
<description>定时任务</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter</artifactId>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<version>3.6.1</version>
40+
<configuration>
41+
<!--<proc>none</proc>-->
42+
<source>1.8</source>
43+
<target>1.8</target>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.20</version>
50+
<configuration>
51+
<skip>true</skip>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
<executions>
58+
</executions>
59+
</plugin>
60+
</plugins>
61+
62+
<resources>
63+
<resource>
64+
<directory>src/main/resources</directory>
65+
</resource>
66+
<resource>
67+
<directory>src/main/java</directory>
68+
<includes>
69+
<include>**/*.xml</include>
70+
</includes>
71+
</resource>
72+
</resources>
73+
</build>
74+
75+
</project>

springboot-schedule/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.xncoding.pos.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
import org.springframework.scheduling.annotation.SchedulingConfigurer;
7+
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
8+
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
12+
/**
13+
* ScheduleConfig
14+
*
15+
* @author XiongNeng
16+
* @version 1.0
17+
* @since 2017/9/13
18+
*/
19+
@Configuration
20+
@EnableScheduling
21+
public class ScheduleConfig implements SchedulingConfigurer {
22+
@Override
23+
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
24+
taskRegistrar.setScheduler(taskExecutor());
25+
}
26+
27+
@Bean(destroyMethod="shutdown")
28+
public ExecutorService taskExecutor() {
29+
return Executors.newScheduledThreadPool(5);
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.xncoding.pos.jobs;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.scheduling.annotation.Scheduled;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* 定时任务
10+
*
11+
* 第一位,表示秒,取值0-59
12+
* 第二位,表示分,取值0-59
13+
* 第三位,表示小时,取值0-23
14+
* 第四位,日期天/日,取值1-31
15+
* 第五位,日期月份,取值1-12
16+
* 第六位,星期,取值1-7,1表示星期天,2表示星期一
17+
* 第七位,年份,可以留空,取值1970-2099
18+
*
19+
* @author XiongNeng
20+
* @version 1.0
21+
* @since 2017/9/27
22+
*/
23+
@Component
24+
public class HeartbeatJob {
25+
private static final Logger logger = LoggerFactory.getLogger(HeartbeatJob.class);
26+
27+
/**
28+
* 检查状态1
29+
*/
30+
@Scheduled(cron = "0 26 19 * * ?")
31+
public void checkState1() {
32+
logger.info(">>>>> cron中午12:30上传检查开始....");
33+
logger.info(">>>>> cron中午12:30上传检查完成....");
34+
}
35+
36+
/**
37+
* 检查状态2
38+
*/
39+
@Scheduled(cron = "0 05 18 * * ?")
40+
public void checkState2() {
41+
logger.info(">>>>> cron晚上18:00上传检查开始....");
42+
logger.info(">>>>> cron晚上18:00上传检查完成....");
43+
}
44+
45+
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##########################################################
2+
################## 所有profile共有的配置 #################
3+
##########################################################
4+
5+
################### spring配置 ###################
6+
spring:
7+
profiles:
8+
active: dev
9+
10+
---
11+
12+
#####################################################################
13+
######################## 开发环境profile ##########################
14+
#####################################################################
15+
spring:
16+
profiles: dev
17+
18+
logging:
19+
level:
20+
ROOT: INFO
21+
com:
22+
xncoding: DEBUG
23+
file: E:/logs/app.log
24+
25+
---
26+
27+
#####################################################################
28+
######################## 测试环境profile ##########################
29+
#####################################################################
30+
31+
spring:
32+
profiles: test
33+
34+
logging:
35+
level:
36+
ROOT: INFO
37+
com:
38+
xncoding: DEBUG
39+
file: /var/logs/app.log

0 commit comments

Comments
 (0)