自动启动项目
This commit is contained in:
@@ -18,16 +18,12 @@ import org.springframework.stereotype.Component;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* 动态定时器
|
||||
* 动态定时器
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -52,7 +48,7 @@ public class SchedulerConf {
|
||||
* 初始化线程池任务调度
|
||||
*/
|
||||
@Autowired
|
||||
public SchedulerConf(){
|
||||
public SchedulerConf() {
|
||||
this.threadPoolTaskScheduler.setPoolSize(50);
|
||||
this.threadPoolTaskScheduler.setThreadNamePrefix("task-thread-");
|
||||
this.threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||
@@ -62,17 +58,17 @@ public class SchedulerConf {
|
||||
/**
|
||||
* 获取所有数据库里的定时任务
|
||||
*/
|
||||
private void getAllTbTask(){
|
||||
private void getAllTbTask() {
|
||||
//查询所有,并put到tasks
|
||||
SchedulerConf.tasks.clear();
|
||||
List<TbTask> list = iTbTaskService.list();
|
||||
list.forEach((task)-> SchedulerConf.tasks.put(task.getId(),task));
|
||||
list.forEach((task) -> SchedulerConf.tasks.put(task.getId(), task));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据定时任务id,启动定时任务
|
||||
*/
|
||||
public void start(String taskId){
|
||||
public void start(String taskId) {
|
||||
try {
|
||||
//重新获取最新执行任务周期
|
||||
this.getAllTbTask();
|
||||
@@ -81,7 +77,7 @@ public class SchedulerConf {
|
||||
|
||||
//获取并实例化Runnable任务类
|
||||
Class<?> clazz = Class.forName(tbTask.getTaskClass());
|
||||
Runnable runnable = (Runnable)clazz.newInstance();
|
||||
Runnable runnable = (Runnable) clazz.newInstance();
|
||||
|
||||
//Cron表达式
|
||||
CronTrigger cron = new CronTrigger(tbTask.getTaskExp());
|
||||
@@ -89,11 +85,11 @@ public class SchedulerConf {
|
||||
//执行,并put到runTasks
|
||||
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) {
|
||||
log.error("{},任务启动失败...",taskId);
|
||||
log.error("{},任务启动失败...", taskId);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -102,95 +98,65 @@ public class SchedulerConf {
|
||||
/**
|
||||
* 根据定时任务id,停止定时任务
|
||||
*/
|
||||
public void stop(String taskId){
|
||||
public void stop(String taskId) {
|
||||
SchedulerConf.runTasks.get(taskId).cancel(true);
|
||||
|
||||
SchedulerConf.runTasks.remove(taskId);
|
||||
|
||||
this.updateTaskStatus(taskId,0);
|
||||
this.updateTaskStatus(taskId, 0);
|
||||
|
||||
log.info("{},任务停止...",taskId);
|
||||
log.info("{},任务停止...", taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ip开启定时任务
|
||||
*/
|
||||
public void startByIp() throws Exception{
|
||||
public void startByIp() throws Exception {
|
||||
// InetAddress addr = InetAddress.getLocalHost();
|
||||
// log.info("Local HostAddress: "+addr.getHostAddress());
|
||||
// String hostname = addr.getHostName();
|
||||
// log.info("Local host name: "+hostname);
|
||||
String addr = getIpAddress();
|
||||
log.info("本机ip地址:" + addr);
|
||||
List<String> addr = getIpAddress();
|
||||
//查询
|
||||
List<TbTask> tbTaskList = tbTaskDao.selectList(null);
|
||||
for (TbTask tbTask : tbTaskList) {
|
||||
if (addr.equals(tbTask.getIp())) {
|
||||
this.start(tbTask.getId());
|
||||
log.info("执行了:"+tbTask.getTaskDesc());
|
||||
if (addr == null){
|
||||
log.error("本机地址未获取到!!");
|
||||
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(){
|
||||
try{
|
||||
private static List<String> getIpAddress() {
|
||||
try {
|
||||
List<String> ips = new ArrayList<>();
|
||||
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (allNetInterfaces.hasMoreElements()){
|
||||
while (allNetInterfaces.hasMoreElements()) {
|
||||
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
|
||||
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()){
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress ip = (InetAddress) addresses.nextElement();
|
||||
if (ip != null
|
||||
&& ip instanceof Inet4Address
|
||||
&& !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
|
||||
&& ip.getHostAddress().indexOf(":")==-1){
|
||||
System.out.println("本机的IP = " + ip.getHostAddress());
|
||||
return ip.getHostAddress();
|
||||
&& ip.getHostAddress().indexOf(":") == -1) {
|
||||
log.info("本机的IP = " + ip.getHostAddress());
|
||||
ips.add(ip.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
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();
|
||||
return ips;
|
||||
} catch (Exception e) {
|
||||
log.error("获取主机ip地址异常", e);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
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);
|
||||
task.setTaskStatus(status);
|
||||
task.setUpdateTime(new Date());
|
||||
|
||||
Reference in New Issue
Block a user