自动启动项目

This commit is contained in:
zhangjun
2022-09-27 16:16:22 +08:00
parent 1e60718574
commit 950c78575b
@@ -18,16 +18,12 @@ import org.springframework.stereotype.Component;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.SocketException; import java.util.*;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
/** /**
* 动态定时器 * 动态定时器
*/ */
@Slf4j @Slf4j
@Component @Component
@@ -52,7 +48,7 @@ public class SchedulerConf {
* 初始化线程池任务调度 * 初始化线程池任务调度
*/ */
@Autowired @Autowired
public SchedulerConf(){ public SchedulerConf() {
this.threadPoolTaskScheduler.setPoolSize(50); this.threadPoolTaskScheduler.setPoolSize(50);
this.threadPoolTaskScheduler.setThreadNamePrefix("task-thread-"); this.threadPoolTaskScheduler.setThreadNamePrefix("task-thread-");
this.threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true); this.threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
@@ -62,17 +58,17 @@ public class SchedulerConf {
/** /**
* 获取所有数据库里的定时任务 * 获取所有数据库里的定时任务
*/ */
private void getAllTbTask(){ private void getAllTbTask() {
//查询所有,并put到tasks //查询所有,并put到tasks
SchedulerConf.tasks.clear(); SchedulerConf.tasks.clear();
List<TbTask> list = iTbTaskService.list(); List<TbTask> list = iTbTaskService.list();
list.forEach((task)-> SchedulerConf.tasks.put(task.getId(),task)); list.forEach((task) -> SchedulerConf.tasks.put(task.getId(), task));
} }
/** /**
* 根据定时任务id,启动定时任务 * 根据定时任务id,启动定时任务
*/ */
public void start(String taskId){ public void start(String taskId) {
try { try {
//重新获取最新执行任务周期 //重新获取最新执行任务周期
this.getAllTbTask(); this.getAllTbTask();
@@ -81,7 +77,7 @@ public class SchedulerConf {
//获取并实例化Runnable任务类 //获取并实例化Runnable任务类
Class<?> clazz = Class.forName(tbTask.getTaskClass()); Class<?> clazz = Class.forName(tbTask.getTaskClass());
Runnable runnable = (Runnable)clazz.newInstance(); Runnable runnable = (Runnable) clazz.newInstance();
//Cron表达式 //Cron表达式
CronTrigger cron = new CronTrigger(tbTask.getTaskExp()); CronTrigger cron = new CronTrigger(tbTask.getTaskExp());
@@ -89,11 +85,11 @@ public class SchedulerConf {
//执行,并put到runTasks //执行,并put到runTasks
SchedulerConf.runTasks.put(taskId, Objects.requireNonNull(this.threadPoolTaskScheduler.schedule(runnable, cron))); SchedulerConf.runTasks.put(taskId, Objects.requireNonNull(this.threadPoolTaskScheduler.schedule(runnable, cron)));
this.updateTaskStatus(taskId,1); this.updateTaskStatus(taskId, 1);
log.info("{},任务启动!",taskId); log.info("{},任务启动!", taskId);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
log.error("{},任务启动失败...",taskId); log.error("{},任务启动失败...", taskId);
e.printStackTrace(); e.printStackTrace();
} }
@@ -102,95 +98,65 @@ public class SchedulerConf {
/** /**
* 根据定时任务id,停止定时任务 * 根据定时任务id,停止定时任务
*/ */
public void stop(String taskId){ public void stop(String taskId) {
SchedulerConf.runTasks.get(taskId).cancel(true); SchedulerConf.runTasks.get(taskId).cancel(true);
SchedulerConf.runTasks.remove(taskId); SchedulerConf.runTasks.remove(taskId);
this.updateTaskStatus(taskId,0); this.updateTaskStatus(taskId, 0);
log.info("{},任务停止...",taskId); log.info("{},任务停止...", taskId);
} }
/** /**
* 根据ip开启定时任务 * 根据ip开启定时任务
*/ */
public void startByIp() throws Exception{ public void startByIp() throws Exception {
// InetAddress addr = InetAddress.getLocalHost(); // InetAddress addr = InetAddress.getLocalHost();
// log.info("Local HostAddress: "+addr.getHostAddress()); // log.info("Local HostAddress: "+addr.getHostAddress());
// String hostname = addr.getHostName(); // String hostname = addr.getHostName();
// log.info("Local host name: "+hostname); // log.info("Local host name: "+hostname);
String addr = getIpAddress(); List<String> addr = getIpAddress();
log.info("本机ip地址:" + addr);
//查询 //查询
List<TbTask> tbTaskList = tbTaskDao.selectList(null); List<TbTask> tbTaskList = tbTaskDao.selectList(null);
for (TbTask tbTask : tbTaskList) { for (TbTask tbTask : tbTaskList) {
if (addr.equals(tbTask.getIp())) { if (addr == null){
this.start(tbTask.getId()); log.error("本机地址未获取到!!");
log.info("执行了:"+tbTask.getTaskDesc()); break;
}
String ips = tbTask.getIp();
String[] ipArray = ips.split(",");
for (String realIp : ipArray) {
if (addr.contains(realIp)) {
log.info("本机ip地址:" + addr);
this.start(tbTask.getId());
log.info("执行了:" + tbTask.getTaskDesc());
}
} }
} }
} }
private static String getIpAddress(){ private static List<String> getIpAddress() {
try{ try {
List<String> ips = new ArrayList<>();
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()){ while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()){ while (addresses.hasMoreElements()) {
InetAddress ip = (InetAddress) addresses.nextElement(); InetAddress ip = (InetAddress) addresses.nextElement();
if (ip != null if (ip != null
&& ip instanceof Inet4Address && ip instanceof Inet4Address
&& !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255 && !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
&& ip.getHostAddress().indexOf(":")==-1){ && ip.getHostAddress().indexOf(":") == -1) {
System.out.println("本机的IP = " + ip.getHostAddress()); log.info("本机的IP = " + ip.getHostAddress());
return ip.getHostAddress(); ips.add(ip.getHostAddress());
} }
} }
} }
}catch(Exception e){ return ips;
e.printStackTrace();
}
return null;
}
/** 获取主机地址 */
public static String getHostIp(){
String realIp = null;
try {
InetAddress address = InetAddress.getLocalHost();
// 如果是回环网卡地址, 则获取ipv4 地址
if (address.isLoopbackAddress()) {
address = getInet4Address();
}
realIp = address.getHostAddress();
log.info("获取主机ip地址成功, 主机ip地址:{}", address);
return address.getHostAddress();
} catch (Exception e) { } catch (Exception e) {
log.error("获取主机ip地址异常", e); e.printStackTrace();
}
return realIp;
}
/** 获取IPV4网络配置 */
private static InetAddress getInet4Address() throws SocketException {
// 获取所有网卡信息
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) networkInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = (InetAddress) addresses.nextElement();
if (ip instanceof Inet4Address) {
return ip;
}
}
} }
return null; return null;
} }
@@ -198,7 +164,7 @@ public class SchedulerConf {
/** /**
* 更新数据库动态定时任务状态 * 更新数据库动态定时任务状态
*/ */
private void updateTaskStatus(String taskId,int status){ private void updateTaskStatus(String taskId, int status) {
TbTask task = iTbTaskService.getById(taskId); TbTask task = iTbTaskService.getById(taskId);
task.setTaskStatus(status); task.setTaskStatus(status);
task.setUpdateTime(new Date()); task.setUpdateTime(new Date());