feat: optimize HttpVertxCSInitializer

This commit is contained in:
tjq 2023-01-02 00:22:48 +08:00
parent d3bd22302f
commit 2606440f44
5 changed files with 71 additions and 39 deletions

View File

@ -21,6 +21,26 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.0</junit.version>
<logback.version>1.2.9</logback.version>
</properties>
<dependencies>
<!-- Junit tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- log for test stage -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -20,8 +20,7 @@
<powerjob-common.version>4.2.0</powerjob-common.version>
<reflections.version>0.10.2</reflections.version>
<junit.version>5.9.0</junit.version>
<logback.version>1.2.9</logback.version>
</properties>
<dependencies>
@ -37,20 +36,6 @@
<version>${reflections.version}</version>
</dependency>
<!-- Junit tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- log for test stage -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -19,11 +19,6 @@ import tech.powerjob.remote.framework.actor.Handler;
@Actor(path = "benchmark")
public class BenchmarkActor {
@Handler(path = "simple")
public String simpleRequest(String k) {
return k;
}
@Handler(path = "standard")
public BenchmarkResponse processStandardRequest(BenchmarkRequest request) {
log.info("[BenchmarkActor] receive request: {}", request);

View File

@ -12,8 +12,6 @@ import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import tech.powerjob.common.PowerSerializable;
import tech.powerjob.common.exception.PowerJobException;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.HandlerInfo;
@ -70,12 +68,12 @@ public class HttpVertxCSInitializer implements CSInitializer {
// 处理请求响应
router.route().handler(BodyHandler.create());
actorInfos.forEach(actorInfo -> {
log.info("[HttpVertxCSInitializer] start to bind Actor[{}]'s handler!", actorInfo.getAnno().path());
log.info("[PowerJob-Vertx] start to bind Actor[{}]'s handler!", actorInfo.getAnno().path());
Optional.ofNullable(actorInfo.getHandlerInfos()).orElse(Collections.emptyList()).forEach(handlerInfo -> {
Method method = handlerInfo.getMethod();
String handlerHttpPath = handlerInfo.getLocation().toPath();
ProcessType processType = handlerInfo.getAnno().processType();
log.info("[HttpVertxCSInitializer] register Handler with[path={},methodName={},processType={}]", handlerHttpPath, method.getName(), processType);
log.info("[PowerJob-Vertx] start to register Handler with[path={},methodName={},processType={}]", handlerHttpPath, method.getName(), processType);
Handler<RoutingContext> routingContextHandler = buildRequestHandler(actorInfo, handlerInfo);
Route route = router.post(handlerHttpPath);
@ -84,6 +82,7 @@ public class HttpVertxCSInitializer implements CSInitializer {
} else {
route.handler(routingContextHandler);
}
log.info("[PowerJob-Vertx] register Handler[path={},methodName={}] successfully!", handlerHttpPath, method.getName());
});
});
@ -104,11 +103,19 @@ public class HttpVertxCSInitializer implements CSInitializer {
}
private Handler<RoutingContext> buildRequestHandler(ActorInfo actorInfo, HandlerInfo handlerInfo) {
Method method = handlerInfo.getMethod();
Optional<Class<?>> powerSerializeClz = RemoteUtils.findPowerSerialize(method.getParameterTypes());
// 内部框架严格模式绑定失败直接报错
if (!powerSerializeClz.isPresent()) {
throw new PowerJobException("can't find any 'PowerSerialize' object in handler args: " + handlerInfo.getLocation());
}
return ctx -> {
final RequestBody body = ctx.body();
final Object convertResult = convertResult(body, handlerInfo);
final Object convertResult = body.asPojo(powerSerializeClz.get());
try {
Object response = handlerInfo.getMethod().invoke(actorInfo.getActor(), convertResult);
Object response = method.invoke(actorInfo.getActor(), convertResult);
if (response != null) {
if (response instanceof String) {
ctx.end((String) response);
@ -126,18 +133,6 @@ public class HttpVertxCSInitializer implements CSInitializer {
};
}
private static Object convertResult(RequestBody body, HandlerInfo handlerInfo) {
final Method method = handlerInfo.getMethod();
Optional<Class<?>> powerSerializeClz = RemoteUtils.findPowerSerialize(method.getParameterTypes());
// 内部框架严格模式绑定失败直接报错
if (!powerSerializeClz.isPresent()) {
throw new PowerJobException("can't find any 'PowerSerialize' object in handler args: " + handlerInfo.getLocation());
}
return body.asPojo(powerSerializeClz.get());
}
@Override

View File

@ -0,0 +1,37 @@
package tech.powerjob.remote.http;
import com.google.common.collect.Sets;
import org.junit.jupiter.api.Test;
import tech.powerjob.common.enums.Protocol;
import tech.powerjob.remote.framework.base.Address;
import tech.powerjob.remote.framework.cs.CSInitializer;
import tech.powerjob.remote.framework.cs.CSInitializerConfig;
import tech.powerjob.remote.framework.engine.EngineConfig;
import tech.powerjob.remote.framework.engine.EngineOutput;
import tech.powerjob.remote.framework.engine.RemoteEngine;
import tech.powerjob.remote.framework.engine.impl.PowerJobRemoteEngine;
import static org.junit.jupiter.api.Assertions.*;
/**
* HttpVertxCSInitializerTest
*
* @author tjq
* @since 2023/1/2
*/
class HttpVertxCSInitializerTest {
@Test
void testHttpVertxCSInitializerTest() {
final Address address = new Address().setPort(7890).setHost("127.0.0.1");
EngineConfig engineConfig = new EngineConfig()
.setTypes(Sets.newHashSet(Protocol.HTTP.name()))
.setBindAddress(address);
RemoteEngine engine = new PowerJobRemoteEngine();
final EngineOutput engineOutput = engine.start(engineConfig);
}
}