ing
This commit is contained in:
parent
4d22b8f367
commit
5bee7bf4ea
|
@ -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<Class<?>, Object> services;
|
||||
|
||||
public GeneralContainerServer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() throws Exception {
|
||||
super.init();
|
||||
|
||||
Map<String, Object> 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<Class<?>, 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<Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user