diff --git a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java b/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java index b76d8a3..a16e4a9 100644 --- a/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java +++ b/src/main/java/com/loafle/overflow/container/general/server/GeneralContainerServer.java @@ -1,12 +1,111 @@ package com.loafle.overflow.container.general.server; -import com.loafle.overflow.container.server.ContainerServer; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.loafle.overflow.container.general.service.Service; +import com.loafle.overflow.container.server.ContainerServer; +import com.loafle.overflow.core.annotation.RPCService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class GeneralContainerServer extends ContainerServer { + @Autowired + private ApplicationContext applicationContext; + + private Map, Object> services; public GeneralContainerServer() { } + + @Override + protected void init() throws Exception { + super.init(); + + Map services = this.applicationContext.getBeansWithAnnotation(RPCService.class); + if (null == services) { + return; + } + + this.services = new HashMap<>(); + services.forEach((name, bean)->{ + this.services.put(bean.getClass(), bean); + }); + + this.execServices(this.services, "initService", Service.OrderedServices, false); + } + + @Override + protected void onStart() throws Exception { + super.onStart(); + + this.execServices(this.services, "startService", Service.OrderedServices, false); + } + + @Override + protected void onStop() throws Exception { + this.execServices(this.services, "stopService", Service.OrderedServices, true); + + + super.onStop(); + } + + @Override + protected void destroy() throws Exception { + this.execServices(this.services, "destroyService", Service.OrderedServices, true); + + + super.destroy(); + } + + private void execServices(Map, Object> services, String methodName, Class[] orderedTypes, boolean reverse) throws Exception { + if (null == services || 0 == services.size()) { + return; + } + if (null == orderedTypes || 0 == orderedTypes.length) { + return; + } + + List orderedServices = new ArrayList<>(orderedTypes.length); + if (reverse) { + for (int i = orderedTypes.length - 1; i >= 0; i--) { + Class clazz = orderedTypes[i]; + Object service = services.get(clazz); + if (null == service) { + continue; + } + orderedServices.add(service); + } + } else { + for (int i = 0; i < orderedTypes.length; i++) { + Class clazz = orderedTypes[i]; + Object service = services.get(clazz); + if (null == service) { + continue; + } + orderedServices.add(service); + } + } + + if (0 == orderedServices.size()) { + return; + } + + Class[] parameterTypes = new Class[]{}; + for (int i = 0; i < orderedServices.size(); i++) { + Object service = orderedServices.get(i); + Class clazz = service.getClass(); + Method method = clazz.getMethod(methodName, parameterTypes); + if (null == method) { + continue; + } + method.invoke(service); + } + } } diff --git a/src/main/java/com/loafle/overflow/container/general/service/Service.java b/src/main/java/com/loafle/overflow/container/general/service/Service.java new file mode 100644 index 0000000..d95c7e3 --- /dev/null +++ b/src/main/java/com/loafle/overflow/container/general/service/Service.java @@ -0,0 +1,12 @@ +package com.loafle.overflow.container.general.service; + +import java.util.stream.Stream; + +/** + * Service + */ +public class Service { + public static final Class[] OrderedServices = + Stream.of(com.loafle.overflow.container.service.Service.OrderedServices).flatMap(Stream::of).toArray(Class[]::new); + +} \ No newline at end of file