[代码完善](master): 功能完善
完善了README.md的说明 修改完善非签验证和签验证2种短链模式 增加了启动脚本 增加定时定时检查删除过期短链的逻辑master
parent
d86a19cb21
commit
adcdb02589
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
source stop-short-server.sh
|
||||
source start-short-server.sh
|
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Start run short-server-service-1.0 Application..."
|
||||
|
||||
nohup java -jar short-server-service-1.0-SNAPSHOT.jar 1> short-Log.log 2>log/short-errorLog.log &
|
@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
pid=`ps -ef | grep short-server-service-1.0-SNAPSHOT | grep -v grep| awk '{print $2}'`
|
||||
|
||||
if [ -z "$pid" ];
|
||||
then
|
||||
echo "Application is already stopped" $pid
|
||||
else
|
||||
echo $pid is killed
|
||||
kill -9 $pid
|
||||
fi
|
@ -0,0 +1,125 @@
|
||||
package com.by.utils;
|
||||
|
||||
import org.quartz.CronExpression;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
|
||||
/**
|
||||
* Cron表达式解析
|
||||
* <p>
|
||||
* 父类是关于操作定时任务的
|
||||
*
|
||||
* @author q 直接哪里使用的,并不是本人写的,有点不符合代码规范
|
||||
*/
|
||||
public class CronUtil {
|
||||
|
||||
/**
|
||||
* 装换成中文表示形式
|
||||
* @param cronExp
|
||||
* @return
|
||||
*/
|
||||
public static String translateToChinese(String cronExp) {
|
||||
if (cronExp == null || cronExp.length() < 1) {
|
||||
return "cron表达式为空";
|
||||
}
|
||||
CronExpression exp = null;
|
||||
// 初始化cron表达式解析器
|
||||
try {
|
||||
exp = new CronExpression(cronExp);
|
||||
} catch (ParseException e) {
|
||||
return "corn表达式不正确";
|
||||
}
|
||||
String[] tmpCorns = cronExp.split(" ");
|
||||
StringBuffer sBuffer = new StringBuffer();
|
||||
if (tmpCorns.length == 6) {
|
||||
//解析月
|
||||
if (!tmpCorns[4].equals("*")) {
|
||||
sBuffer.append(tmpCorns[4]).append("月");
|
||||
} else {
|
||||
sBuffer.append("每月");
|
||||
}
|
||||
//解析周
|
||||
if (!tmpCorns[5].equals("*") && !tmpCorns[5].equals("?")) {
|
||||
char[] tmpArray = tmpCorns[5].toCharArray();
|
||||
for (char tmp : tmpArray) {
|
||||
switch (tmp) {
|
||||
case '1':
|
||||
sBuffer.append("星期天");
|
||||
break;
|
||||
case '2':
|
||||
sBuffer.append("星期一");
|
||||
break;
|
||||
case '3':
|
||||
sBuffer.append("星期二");
|
||||
break;
|
||||
case '4':
|
||||
sBuffer.append("星期三");
|
||||
break;
|
||||
case '5':
|
||||
sBuffer.append("星期四");
|
||||
break;
|
||||
case '6':
|
||||
sBuffer.append("星期五");
|
||||
break;
|
||||
case '7':
|
||||
sBuffer.append("星期六");
|
||||
break;
|
||||
case '-':
|
||||
sBuffer.append("至");
|
||||
break;
|
||||
default:
|
||||
sBuffer.append(tmp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//解析日
|
||||
if (!tmpCorns[3].equals("?")) {
|
||||
if (!"*".equals(tmpCorns[3]) && !(tmpCorns[3]).contains("/")) {
|
||||
sBuffer.append(tmpCorns[3]).append("日");
|
||||
}else if(!tmpCorns[3].equals("*") && (tmpCorns[3]).contains("/")){
|
||||
|
||||
String[] split = tmpCorns[3].split("/");
|
||||
if(split.length == 2 ){
|
||||
sBuffer.append("从第").append(split[0]).append("天开始,每").append(split[1]).append("天,");
|
||||
}
|
||||
}
|
||||
else {
|
||||
sBuffer.append("每日");
|
||||
}
|
||||
}
|
||||
|
||||
//解析时
|
||||
if (!tmpCorns[2].equals("*")) {
|
||||
sBuffer.append(tmpCorns[2]).append("时");
|
||||
} else {
|
||||
sBuffer.append("每时");
|
||||
}
|
||||
|
||||
//解析分
|
||||
if (!tmpCorns[1].equals("*")) {
|
||||
sBuffer.append(tmpCorns[1]).append("分");
|
||||
} else {
|
||||
sBuffer.append("每分");
|
||||
}
|
||||
|
||||
//解析秒
|
||||
if (!tmpCorns[0].equals("*")) {
|
||||
sBuffer.append(tmpCorns[0]).append("秒");
|
||||
} else {
|
||||
sBuffer.append("每秒");
|
||||
}
|
||||
}
|
||||
|
||||
return sBuffer.append("执行").toString();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String CRON_EXPRESSION = "0 0 0 1/1 * ?";
|
||||
System.out.println(CRON_EXPRESSION +" - "+CronUtil.translateToChinese(CRON_EXPRESSION));
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.by.task;
|
||||
|
||||
import com.by.service.ShortServerService;
|
||||
import com.by.utils.CronUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 检查并更新定时任务记录的Task
|
||||
*
|
||||
* @author q
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CheckShortUrlScheduleTask {
|
||||
|
||||
public static final String EXECUTE_CRON = "0 0 0 1/1 * ? ";
|
||||
|
||||
private final ShortServerService shortServerService;
|
||||
|
||||
public CheckShortUrlScheduleTask(ShortServerService shortServerService) {
|
||||
this.shortServerService = shortServerService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 每天执行保存短链时间减一的操作 Task
|
||||
*/
|
||||
@Scheduled(cron = EXECUTE_CRON)
|
||||
public void execute() {
|
||||
log.info("=== [CheckShortUrlScheduleTask|execute is running. Cron rule is {}, Chinese translated to {} Thread is {} ] ===",
|
||||
EXECUTE_CRON,
|
||||
CronUtil.translateToChinese(EXECUTE_CRON),
|
||||
Thread.currentThread().getName()
|
||||
);
|
||||
|
||||
Integer updateRecCount = shortServerService.updateShortUrlRecOnDay();
|
||||
|
||||
log.info("=== [CheckShortUrlScheduleTask|execute update rec is {} . Thread is {} ] ===", updateRecCount, Thread.currentThread().getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.by;
|
||||
|
||||
import com.by.service.ShortServerService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@RunWith(SpringRunner.class)
|
||||
public class DaoTest {
|
||||
|
||||
@Autowired
|
||||
private ShortServerService shortServerService;
|
||||
|
||||
@Test
|
||||
public void test1(){
|
||||
System.out.println(shortServerService.updateShortUrlRecOnDay());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue