bean容器启动逻辑优化,新增web服务启动入口
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.bean;
|
||||
|
||||
import fun.asgc.neutrino.core.context.Environment;
|
||||
import fun.asgc.neutrino.core.context.LifeCycle;
|
||||
import fun.asgc.neutrino.core.context.LifeCycleManager;
|
||||
import fun.asgc.neutrino.core.context.LifeCycleStatus;
|
||||
@@ -32,6 +33,9 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -61,6 +65,14 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
|
||||
* 生命周期管理
|
||||
*/
|
||||
private LifeCycleManager lifeCycleManager = LifeCycleManager.create();
|
||||
/**
|
||||
* 环境参数
|
||||
*/
|
||||
private static volatile Environment environment;
|
||||
/**
|
||||
* 调度器
|
||||
*/
|
||||
private static final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
public AbstractBeanFactory(String name) {
|
||||
this(null, name);
|
||||
@@ -362,6 +374,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
|
||||
beanCache.values().stream().filter(b -> BeanStatus.INJECT == b.getStatus()).forEach(bean -> bean.init());
|
||||
}
|
||||
log.info("bean工厂[{}]初始化.", getName());
|
||||
scheduledExecutor.scheduleWithFixedDelay(this::run, 0, 1, TimeUnit.SECONDS);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -526,4 +539,31 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
|
||||
* @throws BeanException
|
||||
*/
|
||||
protected abstract void inject(BeanWrapper bean) throws BeanException;
|
||||
|
||||
/**
|
||||
* 运行
|
||||
*/
|
||||
private void run() {
|
||||
beanCache.values().stream().filter(b -> b.getStatus().getStatus() < BeanStatus.RUNNING.getStatus()).forEach(b -> {
|
||||
try {
|
||||
getOrNew(b);
|
||||
b.run(getEnvironment().getMainArgs());
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取环境参数
|
||||
* @return
|
||||
*/
|
||||
private Environment getEnvironment() {
|
||||
return LockUtil.doubleCheckProcessForNoException(
|
||||
() -> null == environment,
|
||||
this,
|
||||
() -> environment = getBean(Environment.class),
|
||||
() -> environment
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
package fun.asgc.neutrino.core.bean;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.*;
|
||||
import fun.asgc.neutrino.core.context.ApplicationRunner;
|
||||
import fun.asgc.neutrino.core.context.LifeCycle;
|
||||
import fun.asgc.neutrino.core.util.*;
|
||||
import lombok.Data;
|
||||
@@ -77,7 +78,7 @@ public class BeanWrapper implements LifeCycle {
|
||||
try {
|
||||
method.invoke(instance);
|
||||
} catch (Exception e) {
|
||||
log.error(String.format("初始化方法执行异常 class:%s method:%s", type.getName(), method.getName()), e);
|
||||
log.error(String.format("Bean[type:%s name:%s]初始化方法执行异常 method:%s", type.getName(), name, method.getName()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,7 +97,7 @@ public class BeanWrapper implements LifeCycle {
|
||||
try {
|
||||
method.invoke(instance);
|
||||
} catch (Exception e) {
|
||||
log.error(String.format("销毁方法执行异常 class:%s method:%s", type.getName(), method.getName()), e);
|
||||
log.error(String.format("Bean[type:%s name:%s]销毁方法执行异常 method:%s", type.getName(), name, method.getName()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,8 +129,14 @@ public class BeanWrapper implements LifeCycle {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
public void run(String[] args) {
|
||||
if (BeanStatus.INIT != status) {
|
||||
return;
|
||||
}
|
||||
this.setStatus(BeanStatus.RUNNING);
|
||||
if (this.instance instanceof ApplicationRunner) {
|
||||
((ApplicationRunner)this.instance).run(args);
|
||||
}
|
||||
}
|
||||
|
||||
public BeanIdentity getIdentity() {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
package fun.asgc.neutrino.core.context;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Configuration;
|
||||
import fun.asgc.neutrino.core.annotation.Value;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -30,9 +31,11 @@ import lombok.Data;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/6/16
|
||||
*/
|
||||
@Data
|
||||
@Configuration(prefix = "neutrino")
|
||||
public class ApplicationConfig {
|
||||
private Application application;
|
||||
private Http http;
|
||||
|
||||
@Data
|
||||
public static class Application {
|
||||
@@ -41,4 +44,11 @@ public class ApplicationConfig {
|
||||
*/
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Http {
|
||||
private Integer port;
|
||||
@Value("context-path")
|
||||
private String contextPath;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,10 +49,6 @@ public class ApplicationContext implements LifeCycle {
|
||||
* 应用环境
|
||||
*/
|
||||
private Environment environment;
|
||||
/**
|
||||
* 应用配置
|
||||
*/
|
||||
private ApplicationConfig applicationConfig;
|
||||
/**
|
||||
* 根Bean工厂
|
||||
*/
|
||||
@@ -115,6 +111,7 @@ public class ApplicationContext implements LifeCycle {
|
||||
classes = new HashSet<>();
|
||||
}
|
||||
classes.add(BeanManager.class);
|
||||
classes.add(ExtensionServiceLoader.class);
|
||||
applicationBeanFactory.register(classes);
|
||||
}
|
||||
|
||||
@@ -123,9 +120,9 @@ public class ApplicationContext implements LifeCycle {
|
||||
*/
|
||||
public void run() {
|
||||
init();
|
||||
List<ApplicationRunner> applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class);
|
||||
if (CollectionUtil.notEmpty(applicationRunnerList)) {
|
||||
applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs()));
|
||||
}
|
||||
// List<ApplicationRunner> applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class);
|
||||
// if (CollectionUtil.notEmpty(applicationRunnerList)) {
|
||||
// applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs()));
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.context;
|
||||
|
||||
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.bean.SimpleBeanFactory;
|
||||
import fun.asgc.neutrino.core.web.WebApplicationServer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/7/9
|
||||
*/
|
||||
@NonIntercept
|
||||
@Component
|
||||
public class ExtensionServiceLoader implements ApplicationRunner {
|
||||
@Autowired
|
||||
private ApplicationConfig rootApplicationConfig;
|
||||
@Autowired
|
||||
private SimpleBeanFactory applicationBeanFactory;
|
||||
|
||||
@Override
|
||||
public void run(String[] args) {
|
||||
startHttpServer();
|
||||
}
|
||||
|
||||
private void startHttpServer() {
|
||||
if (null == rootApplicationConfig) {
|
||||
return;
|
||||
}
|
||||
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
|
||||
if (null == http || null == http.getPort()) {
|
||||
return;
|
||||
}
|
||||
applicationBeanFactory.registerBean(WebApplicationServer.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.web;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.context.ApplicationRunner;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* web服务
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/7/9
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WebApplicationServer implements ApplicationRunner {
|
||||
|
||||
@Override
|
||||
public void run(String[] args) {
|
||||
log.debug("web服务启动...");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
neutrino:
|
||||
application:
|
||||
name: neutrino-proxy-server
|
||||
http:
|
||||
port: 8080
|
||||
context-path: /
|
||||
|
||||
proxy:
|
||||
protocol:
|
||||
|
||||
Reference in New Issue
Block a user