mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: optimize HttpVertxCSInitializer
This commit is contained in:
parent
d3bd22302f
commit
2606440f44
@ -21,6 +21,26 @@
|
|||||||
<maven.compiler.source>8</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
<maven.compiler.target>8</maven.compiler.target>
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
||||||
|
<junit.version>5.9.0</junit.version>
|
||||||
|
<logback.version>1.2.9</logback.version>
|
||||||
</properties>
|
</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>
|
</project>
|
@ -20,8 +20,7 @@
|
|||||||
<powerjob-common.version>4.2.0</powerjob-common.version>
|
<powerjob-common.version>4.2.0</powerjob-common.version>
|
||||||
<reflections.version>0.10.2</reflections.version>
|
<reflections.version>0.10.2</reflections.version>
|
||||||
|
|
||||||
<junit.version>5.9.0</junit.version>
|
|
||||||
<logback.version>1.2.9</logback.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -37,20 +36,6 @@
|
|||||||
<version>${reflections.version}</version>
|
<version>${reflections.version}</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -19,11 +19,6 @@ import tech.powerjob.remote.framework.actor.Handler;
|
|||||||
@Actor(path = "benchmark")
|
@Actor(path = "benchmark")
|
||||||
public class BenchmarkActor {
|
public class BenchmarkActor {
|
||||||
|
|
||||||
@Handler(path = "simple")
|
|
||||||
public String simpleRequest(String k) {
|
|
||||||
return k;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Handler(path = "standard")
|
@Handler(path = "standard")
|
||||||
public BenchmarkResponse processStandardRequest(BenchmarkRequest request) {
|
public BenchmarkResponse processStandardRequest(BenchmarkRequest request) {
|
||||||
log.info("[BenchmarkActor] receive request: {}", request);
|
log.info("[BenchmarkActor] receive request: {}", request);
|
||||||
|
@ -12,8 +12,6 @@ import io.vertx.ext.web.Router;
|
|||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.handler.BodyHandler;
|
import io.vertx.ext.web.handler.BodyHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.common.exception.PowerJobException;
|
||||||
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.actor.HandlerInfo;
|
||||||
@ -70,12 +68,12 @@ public class HttpVertxCSInitializer implements CSInitializer {
|
|||||||
// 处理请求响应
|
// 处理请求响应
|
||||||
router.route().handler(BodyHandler.create());
|
router.route().handler(BodyHandler.create());
|
||||||
actorInfos.forEach(actorInfo -> {
|
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 -> {
|
Optional.ofNullable(actorInfo.getHandlerInfos()).orElse(Collections.emptyList()).forEach(handlerInfo -> {
|
||||||
Method method = handlerInfo.getMethod();
|
Method method = handlerInfo.getMethod();
|
||||||
String handlerHttpPath = handlerInfo.getLocation().toPath();
|
String handlerHttpPath = handlerInfo.getLocation().toPath();
|
||||||
ProcessType processType = handlerInfo.getAnno().processType();
|
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);
|
Handler<RoutingContext> routingContextHandler = buildRequestHandler(actorInfo, handlerInfo);
|
||||||
Route route = router.post(handlerHttpPath);
|
Route route = router.post(handlerHttpPath);
|
||||||
@ -84,6 +82,7 @@ public class HttpVertxCSInitializer implements CSInitializer {
|
|||||||
} else {
|
} else {
|
||||||
route.handler(routingContextHandler);
|
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) {
|
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 -> {
|
return ctx -> {
|
||||||
final RequestBody body = ctx.body();
|
final RequestBody body = ctx.body();
|
||||||
final Object convertResult = convertResult(body, handlerInfo);
|
final Object convertResult = body.asPojo(powerSerializeClz.get());
|
||||||
try {
|
try {
|
||||||
Object response = handlerInfo.getMethod().invoke(actorInfo.getActor(), convertResult);
|
Object response = method.invoke(actorInfo.getActor(), convertResult);
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
if (response instanceof String) {
|
if (response instanceof String) {
|
||||||
ctx.end((String) response);
|
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
|
@Override
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user