feat: define PowerJobActor

This commit is contained in:
tjq 2023-01-06 22:53:11 +08:00
parent 4b2d9d4d74
commit 79cde85256
6 changed files with 47 additions and 25 deletions

View File

@ -0,0 +1,10 @@
package tech.powerjob.remote.framework.actor;
/**
* 方便后续扩展比如启动回调等
*
* @author tjq
* @since 2023/1/6
*/
public interface PowerJobActor {
}

View File

@ -4,10 +4,12 @@ import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import tech.powerjob.remote.framework.actor.PowerJobActor;
import tech.powerjob.remote.framework.base.Address;
import tech.powerjob.remote.framework.base.ServerType;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
/**
@ -29,4 +31,8 @@ public class EngineConfig implements Serializable {
* 绑定的本地地址
*/
private Address bindAddress;
/**
* actor实例交由使用侧自己实例化以便自行注入各种 bean
*/
private List<PowerJobActor> actorList;
}

View File

@ -2,14 +2,13 @@ package tech.powerjob.remote.framework.engine.impl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.reflections.ReflectionUtils;
import org.reflections.Reflections;
import tech.powerjob.common.OmsConstant;
import tech.powerjob.remote.framework.actor.Actor;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.Handler;
import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.common.exception.PowerJobException;
import tech.powerjob.remote.framework.actor.*;
import tech.powerjob.remote.framework.base.HandlerLocation;
import java.lang.reflect.Method;
@ -25,19 +24,16 @@ import java.util.Set;
@Slf4j
class ActorFactory {
static List<ActorInfo> load() {
Reflections reflections = new Reflections(OmsConstant.PACKAGE);
final Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Actor.class);
static List<ActorInfo> load(List<PowerJobActor> actorList) {
List<ActorInfo> actorInfos = Lists.newArrayList();
typesAnnotatedWith.forEach(clz -> {
actorList.forEach(actor -> {
final Class<? extends PowerJobActor> clz = actor.getClass();
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 actorInfo = new ActorInfo().setActor(actor).setAnno(anno);
actorInfo.setHandlerInfos(loadHandlerInfos4Actor(actorInfo));
actorInfos.add(actorInfo);
@ -56,21 +52,24 @@ class ActorFactory {
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 -> {
Method[] declaredMethods = actor.getClass().getDeclaredMethods();
for (Method handlerMethod: declaredMethods) {
Handler handlerMethodAnnotation = handlerMethod.getAnnotation(Handler.class);
if (handlerMethodAnnotation == null) {
continue;
}
HandlerLocation handlerLocation = new HandlerLocation()
.setRootPath(rootPath)
.setMethodPath(handlerMethodAnnotation.path());
HandlerInfo handlerInfo = new HandlerInfo()
.setAnno(handlerMethodAnnotation)
.setMethod(handlerMethod)
.setLocation(handlerLocation);
ret.add(handlerInfo);
});
}
return ret;
}
}

View File

@ -27,7 +27,7 @@ public class PowerJobRemoteEngine implements RemoteEngine {
EngineOutput engineOutput = new EngineOutput();
log.info("[PowerJobRemoteEngine] start remote engine with config: {}", engineConfig);
List<ActorInfo> actorInfos = ActorFactory.load();
List<ActorInfo> actorInfos = ActorFactory.load(engineConfig.getActorList());
List<CSInitializer> csInitializerList = CSInitializerFactory.build(engineConfig.getTypes());
csInitializerList.forEach(csInitializer -> {

View File

@ -1,5 +1,6 @@
package tech.powerjob.remote.framework.engine.impl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import tech.powerjob.remote.framework.actor.ActorInfo;
@ -20,13 +21,7 @@ class ActorFactoryTest {
@Test
void load() {
final List<ActorInfo> actorInfos = ActorFactory.load();
log.info("[ActorFactoryTest] actorInfos: {}", actorInfos);
final Set<String> clzNames = actorInfos.stream().map(x -> x.getActor().getClass().getName()).collect(Collectors.toSet());
log.info("[ActorFactoryTest] all load clzNames: {}", clzNames);
assert clzNames.contains(TestActor.class.getName());
ActorFactory.load(Lists.newArrayList(new TestActor()));
}
}

View File

@ -1,7 +1,11 @@
package tech.powerjob.remote.framework.test;
import lombok.extern.slf4j.Slf4j;
import tech.powerjob.remote.framework.actor.Actor;
import tech.powerjob.remote.framework.actor.Handler;
import tech.powerjob.remote.framework.actor.PowerJobActor;
import java.util.Map;
/**
* TestActor
@ -9,8 +13,9 @@ import tech.powerjob.remote.framework.actor.Handler;
* @author tjq
* @since 2022/12/31
*/
@Slf4j
@Actor(path = "/test")
public class TestActor {
public class TestActor implements PowerJobActor {
public static void simpleStaticMethod() {
}
@ -20,12 +25,19 @@ public class TestActor {
@Handler(path = "/method1")
public String handlerMethod1() {
log.info("[TestActor] handlerMethod1");
return "1";
}
@Handler(path = "/method2")
public String handlerMethod2(String name) {
log.info("[TestActor] handlerMethod2 req: {}", name);
return name;
}
@Handler(path = "/returnEmpty")
public void handlerEmpty(Map<String, Object> req) {
log.info("[TestActor] handlerEmpty req: {}", req);
}
}