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;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.List;
/**
* ActorInfo
*
* @author tjq
* @since 2022/12/31
*/
@Getter
@Setter
@Accessors(chain = true)
public class ActorInfo {
private final Object actor;
private Object actor;
private final Actor anno;
private Actor anno;
public ActorInfo(Object actor, Actor anno) {
this.actor = actor;
this.anno = anno;
}
private List<HandlerInfo> handlerInfos;
public Object getActor() {
return actor;
}
public Actor getAnno() {
return anno;
}
}

View File

@ -1,5 +1,6 @@
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.transporter.Transporter;
@ -34,7 +35,7 @@ public interface CSInitializer extends Closeable {
/**
* 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,12 +23,36 @@ import java.util.Set;
* @since 2022/12/31
*/
@Slf4j
class HandlerFactory {
class ActorFactory {
public static List<HandlerInfo> load() {
List<ActorInfo> actorInfos = loadActorInfos();
static List<ActorInfo> load() {
Reflections reflections = new Reflections(OmsConstant.PACKAGE);
final Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Actor.class);
List<ActorInfo> actorInfos = Lists.newArrayList();
typesAnnotatedWith.forEach(clz -> {
try {
final Actor anno = clz.getAnnotation(Actor.class);
final Object object = clz.getDeclaredConstructor().newInstance();
log.info("[ActorFactory] load Actor[clz={},path={}] successfully!", clz, anno.path());
ActorInfo actorInfo = new ActorInfo().setActor(object).setAnno(anno);
actorInfo.setHandlerInfos(loadHandlerInfos4Actor(actorInfo));
actorInfos.add(actorInfo);
} catch (Throwable t) {
log.error("[ActorFactory] process Actor[{}] failed!", clz);
ExceptionUtils.rethrow(t);
}
});
return actorInfos;
}
private static List<HandlerInfo> loadHandlerInfos4Actor(ActorInfo actorInfo) {
List<HandlerInfo> ret = Lists.newArrayList();
actorInfos.forEach(actorInfo -> {
Actor anno = actorInfo.getAnno();
String rootPath = anno.path();
Object actor = actorInfo.getActor();
@ -47,30 +71,6 @@ class HandlerFactory {
.setLocation(handlerLocation);
ret.add(handlerInfo);
});
});
return ret;
}
static List<ActorInfo> loadActorInfos() {
Reflections reflections = new Reflections(OmsConstant.PACKAGE);
final Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Actor.class);
List<ActorInfo> actorInfos = Lists.newArrayList();
typesAnnotatedWith.forEach(clz -> {
try {
final Actor anno = clz.getAnnotation(Actor.class);
final Object object = clz.getDeclaredConstructor().newInstance();
log.info("[ActorFactory] load Actor[clz={},path={}] successfully!", clz, anno.path());
actorInfos.add(new ActorInfo(object, anno));
} catch (Throwable t) {
log.error("[ActorFactory] process Actor[{}] failed!", clz);
ExceptionUtils.rethrow(t);
}
});
return actorInfos;
}
}

View File

@ -2,7 +2,7 @@ package tech.powerjob.remote.framework.engine.impl;
import com.google.common.base.Stopwatch;
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.CSInitializerConfig;
import tech.powerjob.remote.framework.engine.EngineConfig;
@ -27,7 +27,7 @@ public class PowerJobRemoteEngine implements RemoteEngine {
EngineOutput engineOutput = new EngineOutput();
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());
csInitializerList.forEach(csInitializer -> {
@ -44,7 +44,7 @@ public class PowerJobRemoteEngine implements RemoteEngine {
Transporter transporter = csInitializer.buildTransporter();
engineOutput.getType2Transport().put(type, transporter);
csInitializer.bindHandlers(handlerInfos);
csInitializer.bindHandlers(actorInfos);
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 org.junit.jupiter.api.Test;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.test.TestActor;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/**
* HandlerFactoryTest
*
@ -19,20 +16,17 @@ import static org.junit.jupiter.api.Assertions.*;
* @since 2022/12/31
*/
@Slf4j
class HandlerFactoryTest {
class ActorFactoryTest {
@Test
void load() {
final List<HandlerInfo> handlerInfos = HandlerFactory.load();
log.info("[HandlerFactoryTest] handlerInfos: {}", handlerInfos);
}
final List<ActorInfo> actorInfos = ActorFactory.load();
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());
log.info("[HandlerFactoryTest] all load clzNames: {}", clzNames);
log.info("[ActorFactoryTest] all load clzNames: {}", clzNames);
assert clzNames.contains(TestActor.class.getName());
}
}

View File

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

View File

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