refactor: change remote framework api

This commit is contained in:
tjq 2022-12-31 16:56:00 +08:00
parent 31d2283f99
commit 68a9cc52e2
7 changed files with 58 additions and 62 deletions

View File

@ -1,28 +1,27 @@
package tech.powerjob.remote.framework.actor; package tech.powerjob.remote.framework.actor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.List;
/** /**
* ActorInfo * ActorInfo
* *
* @author tjq * @author tjq
* @since 2022/12/31 * @since 2022/12/31
*/ */
@Getter
@Setter
@Accessors(chain = true)
public class ActorInfo { public class ActorInfo {
private final Object actor; private Object actor;
private final Actor anno; private Actor anno;
public ActorInfo(Object actor, Actor anno) { private List<HandlerInfo> handlerInfos;
this.actor = actor;
this.anno = anno;
}
public Object getActor() {
return actor;
}
public Actor getAnno() {
return anno;
}
} }

View File

@ -1,5 +1,6 @@
package tech.powerjob.remote.framework.cs; package tech.powerjob.remote.framework.cs;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo; import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.transporter.Transporter; import tech.powerjob.remote.framework.transporter.Transporter;
@ -34,7 +35,7 @@ public interface CSInitializer extends Closeable {
/** /**
* bind Actor, publish handler's service * bind Actor, publish handler's service
* @param handlerInfos handler infos * @param actorInfos actor infos
*/ */
void bindHandlers(List<HandlerInfo> handlerInfos); void bindHandlers(List<ActorInfo> actorInfos);
} }

View File

@ -23,36 +23,9 @@ import java.util.Set;
* @since 2022/12/31 * @since 2022/12/31
*/ */
@Slf4j @Slf4j
class HandlerFactory { class ActorFactory {
public static List<HandlerInfo> load() { static List<ActorInfo> load() {
List<ActorInfo> actorInfos = loadActorInfos();
List<HandlerInfo> ret = Lists.newArrayList();
actorInfos.forEach(actorInfo -> {
Actor anno = actorInfo.getAnno();
String rootPath = anno.path();
Object actor = actorInfo.getActor();
Set<Method> allHandlerMethods = ReflectionUtils.getAllMethods(actor.getClass(), (input -> input != null && input.isAnnotationPresent(Handler.class)));
allHandlerMethods.forEach(handlerMethod -> {
Handler handlerMethodAnnotation = handlerMethod.getAnnotation(Handler.class);
HandlerLocation handlerLocation = new HandlerLocation()
.setRootPath(rootPath)
.setMethodPath(handlerMethodAnnotation.path());
HandlerInfo handlerInfo = new HandlerInfo()
.setActorInfo(actorInfo)
.setMethod(handlerMethod)
.setLocation(handlerLocation);
ret.add(handlerInfo);
});
});
return ret;
}
static List<ActorInfo> loadActorInfos() {
Reflections reflections = new Reflections(OmsConstant.PACKAGE); Reflections reflections = new Reflections(OmsConstant.PACKAGE);
final Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Actor.class); final Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Actor.class);
@ -64,7 +37,10 @@ class HandlerFactory {
log.info("[ActorFactory] load Actor[clz={},path={}] successfully!", clz, anno.path()); log.info("[ActorFactory] load Actor[clz={},path={}] successfully!", clz, anno.path());
actorInfos.add(new ActorInfo(object, anno)); ActorInfo actorInfo = new ActorInfo().setActor(object).setAnno(anno);
actorInfo.setHandlerInfos(loadHandlerInfos4Actor(actorInfo));
actorInfos.add(actorInfo);
} catch (Throwable t) { } catch (Throwable t) {
log.error("[ActorFactory] process Actor[{}] failed!", clz); log.error("[ActorFactory] process Actor[{}] failed!", clz);
ExceptionUtils.rethrow(t); ExceptionUtils.rethrow(t);
@ -73,4 +49,28 @@ class HandlerFactory {
return actorInfos; return actorInfos;
} }
private static List<HandlerInfo> loadHandlerInfos4Actor(ActorInfo actorInfo) {
List<HandlerInfo> ret = Lists.newArrayList();
Actor anno = actorInfo.getAnno();
String rootPath = anno.path();
Object actor = actorInfo.getActor();
Set<Method> allHandlerMethods = ReflectionUtils.getAllMethods(actor.getClass(), (input -> input != null && input.isAnnotationPresent(Handler.class)));
allHandlerMethods.forEach(handlerMethod -> {
Handler handlerMethodAnnotation = handlerMethod.getAnnotation(Handler.class);
HandlerLocation handlerLocation = new HandlerLocation()
.setRootPath(rootPath)
.setMethodPath(handlerMethodAnnotation.path());
HandlerInfo handlerInfo = new HandlerInfo()
.setActorInfo(actorInfo)
.setMethod(handlerMethod)
.setLocation(handlerLocation);
ret.add(handlerInfo);
});
return ret;
}
} }

View File

@ -2,7 +2,7 @@ package tech.powerjob.remote.framework.engine.impl;
import com.google.common.base.Stopwatch; import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import tech.powerjob.remote.framework.actor.HandlerInfo; import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.cs.CSInitializer; import tech.powerjob.remote.framework.cs.CSInitializer;
import tech.powerjob.remote.framework.cs.CSInitializerConfig; import tech.powerjob.remote.framework.cs.CSInitializerConfig;
import tech.powerjob.remote.framework.engine.EngineConfig; import tech.powerjob.remote.framework.engine.EngineConfig;
@ -27,7 +27,7 @@ public class PowerJobRemoteEngine implements RemoteEngine {
EngineOutput engineOutput = new EngineOutput(); EngineOutput engineOutput = new EngineOutput();
log.info("[PowerJobRemoteEngine] start remote engine with config: {}", engineConfig); log.info("[PowerJobRemoteEngine] start remote engine with config: {}", engineConfig);
List<HandlerInfo> handlerInfos = HandlerFactory.load(); List<ActorInfo> actorInfos = ActorFactory.load();
List<CSInitializer> csInitializerList = CSInitializerFactory.build(engineConfig.getTypes()); List<CSInitializer> csInitializerList = CSInitializerFactory.build(engineConfig.getTypes());
csInitializerList.forEach(csInitializer -> { csInitializerList.forEach(csInitializer -> {
@ -44,7 +44,7 @@ public class PowerJobRemoteEngine implements RemoteEngine {
Transporter transporter = csInitializer.buildTransporter(); Transporter transporter = csInitializer.buildTransporter();
engineOutput.getType2Transport().put(type, transporter); engineOutput.getType2Transport().put(type, transporter);
csInitializer.bindHandlers(handlerInfos); csInitializer.bindHandlers(actorInfos);
log.info("[PowerJobRemoteEngine] startup CSInitializer[type={}] successfully, cost: {}", type, sw); log.info("[PowerJobRemoteEngine] startup CSInitializer[type={}] successfully, cost: {}", type, sw);
}); });

View File

@ -3,15 +3,12 @@ package tech.powerjob.remote.framework.engine.impl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tech.powerjob.remote.framework.actor.ActorInfo; import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.test.TestActor; import tech.powerjob.remote.framework.test.TestActor;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* HandlerFactoryTest * HandlerFactoryTest
* *
@ -19,20 +16,17 @@ import static org.junit.jupiter.api.Assertions.*;
* @since 2022/12/31 * @since 2022/12/31
*/ */
@Slf4j @Slf4j
class HandlerFactoryTest { class ActorFactoryTest {
@Test @Test
void load() { void load() {
final List<HandlerInfo> handlerInfos = HandlerFactory.load(); final List<ActorInfo> actorInfos = ActorFactory.load();
log.info("[HandlerFactoryTest] handlerInfos: {}", handlerInfos); log.info("[ActorFactoryTest] actorInfos: {}", actorInfos);
}
@Test
void loadActorInfos() {
final List<ActorInfo> actorInfos = HandlerFactory.loadActorInfos();
final Set<String> clzNames = actorInfos.stream().map(x -> x.getActor().getClass().getName()).collect(Collectors.toSet()); final Set<String> clzNames = actorInfos.stream().map(x -> x.getActor().getClass().getName()).collect(Collectors.toSet());
log.info("[HandlerFactoryTest] all load clzNames: {}", clzNames); log.info("[ActorFactoryTest] all load clzNames: {}", clzNames);
assert clzNames.contains(TestActor.class.getName()); assert clzNames.contains(TestActor.class.getName());
} }
} }

View File

@ -1,6 +1,7 @@
package tech.powerjob.remote.framework.test; package tech.powerjob.remote.framework.test;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo; import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.cs.CSInitializer; import tech.powerjob.remote.framework.cs.CSInitializer;
import tech.powerjob.remote.framework.cs.CSInitializerConfig; import tech.powerjob.remote.framework.cs.CSInitializerConfig;
@ -34,8 +35,8 @@ public class TestCSInitializer implements CSInitializer {
} }
@Override @Override
public void bindHandlers(List<HandlerInfo> handlerInfos) { public void bindHandlers(List<ActorInfo> actorInfos) {
log.info("TestCSInitializer#bindHandlers"); log.info("TestCSInitializer#actorInfos");
} }
@Override @Override

View File

@ -7,6 +7,7 @@ import akka.actor.Props;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.typesafe.config.Config; import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo; import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.base.Address; import tech.powerjob.remote.framework.base.Address;
import tech.powerjob.remote.framework.base.ServerType; import tech.powerjob.remote.framework.base.ServerType;
@ -64,7 +65,7 @@ public class AkkaCSInitializer implements CSInitializer {
} }
@Override @Override
public void bindHandlers(List<HandlerInfo> handlerInfos) { public void bindHandlers(List<ActorInfo> actorInfos) {
} }