使用应用生命周期事件优化license获取服务

This commit is contained in:
aoshiguchen
2022-10-10 21:34:39 +08:00
parent aa1c08fd54
commit d19b7147f2
3 changed files with 21 additions and 14 deletions
@@ -50,7 +50,7 @@ public class ApplicationEventReceiver<D> implements EventReceiver<ApplicationEve
@Override
public void receive(ApplicationEvent<D> msg) {
log.debug("ApplicationEventReceiver receive {}", JSONObject.toJSONString(msg));
log.debug("ApplicationEventReceiver receive {}", JSONObject.toJSONString(msg.data()));
}
public void setTopic(String topic) {
@@ -21,9 +21,14 @@
*/
package fun.asgc.neutrino.proxy.client.core;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.annotation.Subscribe;
import fun.asgc.neutrino.core.base.event.ApplicationEvent;
import fun.asgc.neutrino.core.base.event.ApplicationEventReceiver;
import fun.asgc.neutrino.core.constant.AppLifeCycleStatusEnum;
import fun.asgc.neutrino.core.constant.MetaDataConstant;
import lombok.extern.slf4j.Slf4j;
/**
@@ -31,11 +36,19 @@ import lombok.extern.slf4j.Slf4j;
* @date: 2022/10/10
*/
@Slf4j
@NonIntercept
@Component
@Subscribe(topic = MetaDataConstant.TOPIC_APP_LIFE_CYCLE)
public class ApplicationLifeCycleListener extends ApplicationEventReceiver<AppLifeCycleStatusEnum> {
@Autowired
private LicenseObtainService licenseObtainService;
@Override
public void receive(ApplicationEvent<AppLifeCycleStatusEnum> msg) {
log.info("ApplicationLifeCycleListener:{}", msg.data().getDesc());
log.debug("ApplicationLifeCycleListener:{}", msg.data().getDesc());
if (msg.data() == AppLifeCycleStatusEnum.APP_STARTUP) {
licenseObtainService.start();
}
}
}
@@ -25,11 +25,10 @@ import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.base.CustomThreadFactory;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import fun.asgc.neutrino.core.context.Environment;
import fun.asgc.neutrino.core.util.ArrayUtil;
import fun.asgc.neutrino.core.util.FileUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import fun.asgc.neutrino.core.util.SystemUtil;
import fun.asgc.neutrino.proxy.client.config.ProxyConfig;
import lombok.extern.slf4j.Slf4j;
@@ -47,7 +46,7 @@ import java.util.concurrent.locks.ReentrantLock;
@Slf4j
@NonIntercept
@Component
public class LicenseObtainService implements ApplicationRunner {
public class LicenseObtainService {
@Autowired
private ProxyConfig proxyConfig;
/**
@@ -59,16 +58,15 @@ public class LicenseObtainService implements ApplicationRunner {
private ProxyClientService proxyClientService;
private ReentrantLock runLock = new ReentrantLock();
private Scanner scanner = new Scanner(System.in);
private volatile boolean isFirst;
@Autowired
private Environment environment;
@Override
public void run(String[] args) throws Exception {
isFirst = true;
public void start() {
scheduledExecutor.scheduleWithFixedDelay(() -> {
boolean lock = runLock.tryLock();
try {
if (lock) {
this.process(args);
this.process(this.environment.getMainArgs());
}
} finally {
if (runLock.isHeldByCurrentThread()){
@@ -84,10 +82,6 @@ public class LicenseObtainService implements ApplicationRunner {
}
public void process(String[] args) {
if (isFirst) {
isFirst = false;
SystemUtil.trySleep(2000);
}
String licenseKey = getLicenseKey(args);
proxyClientService.start(licenseKey);
}