From 465ad8d2c40b981207a2076821ce654a52a060f3 Mon Sep 17 00:00:00 2001 From: yidao620 Date: Tue, 27 Feb 2018 19:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E7=A4=BA?= =?UTF-8?q?=E4=BE=8Bspringboot-schedule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 +- springboot-schedule/.gitignore | 14 ++++ springboot-schedule/LICENSE | 20 +++++ springboot-schedule/README.md | 17 +++++ springboot-schedule/pom.xml | 75 +++++++++++++++++++ springboot-schedule/run.sh | 72 ++++++++++++++++++ .../java/com/xncoding/pos/Application.java | 12 +++ .../xncoding/pos/config/ScheduleConfig.java | 31 ++++++++ .../com/xncoding/pos/jobs/HeartbeatJob.java | 46 ++++++++++++ .../src/main/resources/application.yml | 39 ++++++++++ .../src/main/resources/banner.txt | 23 ++++++ 11 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 springboot-schedule/.gitignore create mode 100644 springboot-schedule/LICENSE create mode 100644 springboot-schedule/README.md create mode 100644 springboot-schedule/pom.xml create mode 100644 springboot-schedule/run.sh create mode 100644 springboot-schedule/src/main/java/com/xncoding/pos/Application.java create mode 100644 springboot-schedule/src/main/java/com/xncoding/pos/config/ScheduleConfig.java create mode 100644 springboot-schedule/src/main/java/com/xncoding/pos/jobs/HeartbeatJob.java create mode 100644 springboot-schedule/src/main/resources/application.yml create mode 100644 springboot-schedule/src/main/resources/banner.txt diff --git a/app-manage-api/src/main/resources/application.yml b/app-manage-api/src/main/resources/application.yml index 1cc07fe..9f72be7 100644 --- a/app-manage-api/src/main/resources/application.yml +++ b/app-manage-api/src/main/resources/application.yml @@ -68,7 +68,7 @@ logging: ROOT: INFO com: xncoding: DEBUG - file: E:/logs/pos-api.log + file: E:/logs/app-manage-api.log --- diff --git a/springboot-schedule/.gitignore b/springboot-schedule/.gitignore new file mode 100644 index 0000000..1e3c5bb --- /dev/null +++ b/springboot-schedule/.gitignore @@ -0,0 +1,14 @@ +# 此为注释– 将被Git 忽略 +# /结尾表示是目录,忽略目录和目录下的所有件 +# /开头表示根目录,否则是.gitignore的相对目录 +# !开头表示反选 +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* diff --git a/springboot-schedule/LICENSE b/springboot-schedule/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/springboot-schedule/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Xiong Neng + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/springboot-schedule/README.md b/springboot-schedule/README.md new file mode 100644 index 0000000..16e45ec --- /dev/null +++ b/springboot-schedule/README.md @@ -0,0 +1,17 @@ +## 定时任务 + +定时任务非常简单,只需要写个配置类,然后定义定时任务类,使用注解定义某个方法定期执行 + +``` java +@Scheduled(cron = "0 26 19 * * ?") +public void checkState1() { + logger.info(">>>>> xxx检查开始...."); + logger.info(">>>>> xxx传检查完成...."); +} +``` + +## 许可证 + +Copyright (c) 2018 Xiong Neng + +基于 MIT 协议发布: diff --git a/springboot-schedule/pom.xml b/springboot-schedule/pom.xml new file mode 100644 index 0000000..fff7ecd --- /dev/null +++ b/springboot-schedule/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + com.xncoding + springboot-schedule + 1.0.0-SNAPSHOT + jar + + springboot-schedule + 定时任务 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + src/main/resources + + + src/main/java + + **/*.xml + + + + + + \ No newline at end of file diff --git a/springboot-schedule/run.sh b/springboot-schedule/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/springboot-schedule/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# 项目自动更新脚本 +# 先clone相应的分支下来: +# git clone ssh://git@120.24.173.142:7999/xxx.git +# 远程调试启动: +# 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 & + +function start { + profile="$1" + echo "启动环境profile=${profile}" + jarfile=$(ls target/*.jar) + if [[ "$?" == "0" ]]; then + stop $profile $jarfile + fi + branch=$(git branch |awk '{print $2}') + git pull origin ${branch} + echo "更新完代码开始重新打包" + mvn clean && mvn clean && mvn package -DskipTests=true + if [[ "$?" != "0" ]]; then + echo "编译出错,退出!" + exit 1 + fi + echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &" + nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + echo "启动应用中,请查看日志文件..." +} + +function stop { + profile="$1" + jarfile="$2" + ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null + if [[ "$?" == "0" ]]; then + echo "该应用还在跑,我先停了它" + pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}') + if [[ "$pid" != "" ]]; then + kill -9 $pid + fi + echo "停止应用成功..." + fi +} + +if [[ "$1" == "start" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh start {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + exit 1 + fi + start "${profile}" +elif [[ "$1" == "stop" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh stop {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 + fi + jarfile=$(ls target/*.jar) + stop $profile $jarfile +else + echo "参数错误,使用方法:{}参数是必填的,[]参数可选" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 +fi diff --git a/springboot-schedule/src/main/java/com/xncoding/pos/Application.java b/springboot-schedule/src/main/java/com/xncoding/pos/Application.java new file mode 100644 index 0000000..adc4c48 --- /dev/null +++ b/springboot-schedule/src/main/java/com/xncoding/pos/Application.java @@ -0,0 +1,12 @@ +package com.xncoding.pos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/springboot-schedule/src/main/java/com/xncoding/pos/config/ScheduleConfig.java b/springboot-schedule/src/main/java/com/xncoding/pos/config/ScheduleConfig.java new file mode 100644 index 0000000..8c4316e --- /dev/null +++ b/springboot-schedule/src/main/java/com/xncoding/pos/config/ScheduleConfig.java @@ -0,0 +1,31 @@ +package com.xncoding.pos.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * ScheduleConfig + * + * @author XiongNeng + * @version 1.0 + * @since 2017/9/13 + */ +@Configuration +@EnableScheduling +public class ScheduleConfig implements SchedulingConfigurer { + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + @Bean(destroyMethod="shutdown") + public ExecutorService taskExecutor() { + return Executors.newScheduledThreadPool(5); + } +} diff --git a/springboot-schedule/src/main/java/com/xncoding/pos/jobs/HeartbeatJob.java b/springboot-schedule/src/main/java/com/xncoding/pos/jobs/HeartbeatJob.java new file mode 100644 index 0000000..0b3b555 --- /dev/null +++ b/springboot-schedule/src/main/java/com/xncoding/pos/jobs/HeartbeatJob.java @@ -0,0 +1,46 @@ +package com.xncoding.pos.jobs; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * 定时任务 + * + * 第一位,表示秒,取值0-59 + * 第二位,表示分,取值0-59 + * 第三位,表示小时,取值0-23 + * 第四位,日期天/日,取值1-31 + * 第五位,日期月份,取值1-12 + * 第六位,星期,取值1-7,1表示星期天,2表示星期一 + * 第七位,年份,可以留空,取值1970-2099 + * + * @author XiongNeng + * @version 1.0 + * @since 2017/9/27 + */ +@Component +public class HeartbeatJob { + private static final Logger logger = LoggerFactory.getLogger(HeartbeatJob.class); + + /** + * 检查状态1 + */ + @Scheduled(cron = "0 26 19 * * ?") + public void checkState1() { + logger.info(">>>>> cron中午12:30上传检查开始...."); + logger.info(">>>>> cron中午12:30上传检查完成...."); + } + + /** + * 检查状态2 + */ + @Scheduled(cron = "0 05 18 * * ?") + public void checkState2() { + logger.info(">>>>> cron晚上18:00上传检查开始...."); + logger.info(">>>>> cron晚上18:00上传检查完成...."); + } + + +} diff --git a/springboot-schedule/src/main/resources/application.yml b/springboot-schedule/src/main/resources/application.yml new file mode 100644 index 0000000..90acc9c --- /dev/null +++ b/springboot-schedule/src/main/resources/application.yml @@ -0,0 +1,39 @@ +########################################################## +################## 所有profile共有的配置 ################# +########################################################## + +################### spring配置 ################### +spring: + profiles: + active: dev + +--- + +##################################################################### +######################## 开发环境profile ########################## +##################################################################### +spring: + profiles: dev + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: E:/logs/app.log + +--- + +##################################################################### +######################## 测试环境profile ########################## +##################################################################### + +spring: + profiles: test + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /var/logs/app.log diff --git a/springboot-schedule/src/main/resources/banner.txt b/springboot-schedule/src/main/resources/banner.txt new file mode 100644 index 0000000..859b78f --- /dev/null +++ b/springboot-schedule/src/main/resources/banner.txt @@ -0,0 +1,23 @@ + + _____ _______ _____ _____ + /\ \ /::\ \ /\ \ /\ \ + /::\____\ /::::\ \ /::\____\ /::\ \ + /:::/ / /::::::\ \ /:::/ / /::::\ \ + /:::/ / /::::::::\ \ /:::/ / /::::::\ \ + /:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \ + /:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \ + /:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \ + /:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \ + /:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \ +/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\ +\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ / + \:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/ + \:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \ + \:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\ + \:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ / + \:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/ + \:::\ \ \::::/ / \:::\ \ + \:::\____\ \::/____/ \:::\____\ + \::/ / ~~ \::/ / + \/____/ \/____/ +