From 3aa428309d92cf4997f57b69fe33d528367fc42b Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sat, 9 Jul 2022 19:24:22 +0800 Subject: [PATCH] =?UTF-8?q?bean=E5=AE=B9=E5=99=A8=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?web=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/bean/AbstractBeanFactory.java | 40 +++++++++++++ .../asgc/neutrino/core/bean/BeanWrapper.java | 15 +++-- .../core/context/ApplicationConfig.java | 10 ++++ .../core/context/ApplicationContext.java | 13 ++--- .../core/context/ExtensionServiceLoader.java | 58 +++++++++++++++++++ .../core/web/WebApplicationServer.java | 41 +++++++++++++ .../src/main/resources/application.yml | 3 + 7 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java index 2448cbf8..2d9f6743 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java @@ -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 + ); + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java index 04fc7276..640f3a41 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java @@ -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() { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationConfig.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationConfig.java index 113a43d7..271ed222 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationConfig.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationConfig.java @@ -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; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java index cd4be2bc..86246b5b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java @@ -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 applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class); - if (CollectionUtil.notEmpty(applicationRunnerList)) { - applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs())); - } +// List applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class); +// if (CollectionUtil.notEmpty(applicationRunnerList)) { +// applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs())); +// } } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java new file mode 100644 index 00000000..a18f4e5d --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java @@ -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); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java new file mode 100644 index 00000000..b3de768e --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java @@ -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服务启动..."); + } +} diff --git a/neutrino-proxy-server/src/main/resources/application.yml b/neutrino-proxy-server/src/main/resources/application.yml index ed037ea2..2a92b328 100644 --- a/neutrino-proxy-server/src/main/resources/application.yml +++ b/neutrino-proxy-server/src/main/resources/application.yml @@ -1,6 +1,9 @@ neutrino: application: name: neutrino-proxy-server + http: + port: 8080 + context-path: / proxy: protocol: