启动项目

This commit is contained in:
zhangjun
2022-09-27 15:53:27 +08:00
parent a26274ac9c
commit 1e60718574
5 changed files with 131 additions and 0 deletions
@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@@ -24,6 +25,7 @@ import java.util.Map;
*/
@Component
@Order(value = 1)
public class InitStandAttrSearchUtil implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(InitStandAttrSearchUtil.class);
@@ -0,0 +1,27 @@
package com.adc.da.scheduled;
import com.adc.da.search.conf.SchedulerConf;
import com.adc.da.search.util.InitStandAttrSearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value = 2)
public class ScheduledJobRunner implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(ScheduledJobRunner.class);
@Autowired
private SchedulerConf schedulerConf;
@Override
public void run(ApplicationArguments args) throws Exception {
schedulerConf.startByIp();
}
}
@@ -15,7 +15,12 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
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.concurrent.ConcurrentHashMap;
@@ -107,6 +112,89 @@ public class SchedulerConf {
log.info("{},任务停止...",taskId);
}
/**
* 根据ip开启定时任务
*/
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<TbTask> tbTaskList = tbTaskDao.selectList(null);
for (TbTask tbTask : tbTaskList) {
if (addr.equals(tbTask.getIp())) {
this.start(tbTask.getId());
log.info("执行了:"+tbTask.getTaskDesc());
}
}
}
private static String getIpAddress(){
try{
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()){
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
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();
}
}
}
}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();
} 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;
}
}
}
return null;
}
/**
* 更新数据库动态定时任务状态
*/
@@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
import com.adc.da.base.web.BaseController;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
@@ -54,6 +55,16 @@ public class TbTaskController extends BaseController<TbTask> {
return "操作成功";
}
/**
* 根据ip开启定时任务
* http://localhost:10085/tbTask
*/
@GetMapping("startByIp/{taskId}")
public String startByIp() throws Exception{
schedulerConf.startByIp();
return "操作成功";
}
/**
* 更新一个动态定时任务
* http://localhost:10085/tbTask/save?taskId=2&taskExp=0/2 * * * * ?&taskClass=cn.huanzi.qch.springboottimer.task.MyRunnable3
@@ -45,6 +45,9 @@ public class TbTask extends BaseEntity {
@ApiModelProperty(value = "定时任务状态,0停用 1启用")
private Integer taskStatus;
@ApiModelProperty(value = "ip地址")
private String ip;
@ApiModelProperty(value = "定时任务的Runnable任务类完整路径")
private String taskClass;