Springboot关于脚本脚本启动的项目:

#!/bin/bash

if [ -f ~/.bash_profile ];
then
  . ~/.bash_profile
fi

JAVA_HOME=/usr/local/usr_software/jdk_1.8.0.121
JRE_HOME=/usr/local/usr_software/jdk_1.8.0.121/jre
MAVEN_HOME=/usr/local/usr_software/maven_3.3.9
REDIS_HOME=/usr/local/usr_software/redis
MYSQL_HOME=/usr/local/usr_software/mysql_5.7.18
NODE_HOME=/usr/local/usr_software/node-v8.2.1-linux-x64
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$MAVEN_HOME/bin:$REDIS_HOME/bin:$MYSQL_HOME/bin:$NODE_HOME/bin:$PATH
export PATH

java -jar /usr/local/usr_application/script/script-1.0.0.jar taskName:MobileAuthTask prop:test >> /dev/null

 

package com.vcredit.script;

import com.vcredit.script.exception.InvalidParamException;
import com.vcredit.script.model.frame.TaskInfo;
import com.vcredit.script.task.Task;
import com.vcredit.script.utils.ApplicationContextUtils;
import com.vcredit.script.utils.DateUtils;
import com.vcredit.script.utils.DdqStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
* Description:基础应用配置类
*
* @author : dfz
* Created_Date : 2017-06-05 11:52
*/
@Slf4j
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(value = {"com.vcredit.*"})
@ImportResource(value = {"classpath*:spring/**/*-config*.xml"})
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@EnableTransactionManagement(proxyTargetClass = true)
public class Application {

public static void main(String[] args) throws IOException {
String propName = getPropertiesName(args);
startWithProjectProperties(args, propName);

log.info("↓");
log.info("╭(●`∀´●)╯ Spring-context loaded ╰(●’◡’●)╮");
log.info("↓");

List<TaskInfo> taskInfoList = getTaskInfoList(args);
if (taskInfoList != null && taskInfoList.size() > 0) {
log.info("任务信息概览 ↓");
log.info("==============================================================================");
log.info("|");
log.info("| 当前时间 : [ {} ] ", DateUtils.dateToString(new Date()));
log.info("| 激活配置 : [ {} ] ", propName);
log.info("| 任务列表 : [ {} ] ", DdqStringUtils.toString(taskInfoList));
log.info("|");
log.info("==============================================================================");
for (TaskInfo taskInfo : taskInfoList) {
new Thread(() -> {
try {
MDC.put("TRACE_ID", DdqStringUtils.generateDateBasedUUID());
Object task = ApplicationContextUtils.getBean(DdqStringUtils.firstLtrLower(taskInfo.getTaskName()));
if (task != null && task instanceof Task) {
log.info("任务 : [ {} ] 开始执行..", taskInfo.getTaskName());
((Task) task).run(taskInfo.getTaskParams());
} else {
log.info("任务 : [ {} ] 不存在! 将被跳过..", taskInfo.getTaskName());
}
} catch (Exception e) {
log.error("任务 : [ {} ] 执行失败.. Exception:", taskInfo.getTaskName(), e);
}
}).start();
}
} else {
log.info("未检测到任何任务! 请检查请求参数. 程序即将退出..");
}
}

// ---------------------------------------------------------------------------------------------------
// -------------------------------------↓ Private Methods ↓-------------------------------------------
// ---------------------------------------------------------------------------------------------------
private static void startWithProjectProperties(String[] args, String propertiesName) throws IOException { Properties properties = new Properties(); InputStream in = Application.class.getClassLoader().getResourceAsStream(propertiesName); properties.load(in); SpringApplication application = new SpringApplication(Application.class); application.setDefaultProperties(properties); application.run(args); } // 获取任务名 private static List<TaskInfo> getTaskInfoList(String[] args) { if (ObjectUtils.isEmpty(args)) { return null; } List<TaskInfo> resultList = new ArrayList<>(); for (String str : args) { if (str.contains("taskName")) { String[] taskInfoArr = str.split(":"); if (ObjectUtils.isEmpty(taskInfoArr) || taskInfoArr.length != 2) { continue; } List<TaskInfo> tmp = parseTaskInfo(taskInfoArr[1]); if (tmp != null) { resultList.addAll(tmp); } } } return resultList; } // 解析任务参数 private static List<TaskInfo> parseTaskInfo(String arg) { String[] taskInfoArr = arg.split(","); List<TaskInfo> resultList = new ArrayList<>(); for (String taskInfo : taskInfoArr) { String[] tmpArr = taskInfo.split("\\?"); if (ObjectUtils.isEmpty(tmpArr) || DdqStringUtils.isBlank(tmpArr[0])) { throw new InvalidParamException(); } String taskName = tmpArr[0]; String taskParams = tmpArr.length > 1 ? tmpArr[1] : null; resultList.add(new TaskInfo(taskName, taskParams)); } return resultList; } private static String getPropertiesName(String[] args) { String propertiesName = "dev";// 默认dev if (args == null || args.length == 0) { return propertiesName; } for (String str : args) { if (str.contains("prop")) { propertiesName = str.split(":")[1]; } } return "config/" + propertiesName + ".properties"; }}

 

posted @ 2018-08-17 09:53  ding9587  阅读(1170)  评论(0编辑  收藏  举报