如何模拟springReact堆栈线程中的延迟

nhjlsmyf  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(368)

我正在构建一个简单应用程序的两个spring版本。其中一个是servlet堆栈,另一个是React式堆栈。我的目标是证明当线程在等待其他请求时,React堆栈不会阻止线程处理其他请求。所以,我在两个版本中模拟了代码的延迟。但是,当延迟生效时,React堆栈似乎不会处理其他请求。换句话说,这不是React,我做错什么了吗?我是否误解了spring的工作方式?我是在错误地模拟延迟吗?
React堆栈处理程序类

@Component @RequiredArgsConstructor
public class GradeHandler {
    private final GradeRepository gradeRepository;

    public Mono<ServerResponse> gradeHandler(ServerRequest serverRequest){
        String id = serverRequest.pathVariable("id");
        return ServerResponse.
                ok().contentType(MediaType.APPLICATION_JSON).
                body(getGradeByIdDelayed(id, Duration.ofSeconds(1)), Grade.class);
    }

    public Mono<ServerResponse> gradeHandler_blocking(ServerRequest serverRequest){
        String id = serverRequest.pathVariable("id");
        return ServerResponse.
                ok().contentType(MediaType.APPLICATION_JSON).
                body(getGradeByIdDelayed(id, Duration.ofSeconds(10000)), Grade.class);
    }

    private Mono<Grade> getGradeByIdDelayed(String id, Duration duration) {
        return gradeRepository.
                findById(id).
                delayElement(duration);
    }

}

React堆栈路由器配置文件

@Configuration
public class GradeRouterConfig {
    @Bean
    public RouterFunction<ServerResponse> gradeRouter(GradeHandler gradeHandler){
        return RouterFunctions.
                route(GET("grade/{id}").
                        and(accept(MediaType.APPLICATION_JSON)), gradeHandler::gradeHandler).
                andRoute(GET("blocking/grade/{id}").
                        and(accept(MediaType.APPLICATION_JSON)), gradeHandler::gradeHandler_blocking);
    }
}

存储库

@Repository
public interface GradeRepository extends ReactiveCrudRepository<Grade, String> {
}

实体

@Document @Data @RequiredArgsConstructor @Builder
public class Grade {
    @Id
    private final String id;
    private final double grade;
    private final String studentName;
}

这是调用端点的客户端javascript代码

gradesToBeQuried = [1, 2, 3, 4, 5]

var t0 = performance.now()

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onload = () => callback(xmlHttp.responseText);
    xmlHttp.open("GET", theUrl, true); // true for asynchronous
    xmlHttp.send(null);
}

function getRandomGradeId(grades) {
    return grades[Math.floor(Math.random() * grades.length)];
}

function getURLWithRandomGradeID(grades){
    randomGradeId = getRandomGradeId(grades);
    return "http://localhost:8080/grade/" + randomGradeId;
}

function getURLWithRandomGradeID_blocking(grades){
    randomGradeId = getRandomGradeId(grades);
    return "http://localhost:8080/blocking/grade/" + randomGradeId;
}

function checkFinishConditionAndLog(currentIndex, totalNumberOfQueries) {
    if (currentIndex === totalNumberOfQueries - 1) {
        var t1 = performance.now()
        console.log("it took " + (t1 - t0) / 1000 + " seconds.")
    }
}

function queryGrade(currentIndex, totalNumberOfQueries){
    let urlWithRandomGradeID = getURLWithRandomGradeID(gradesToBeQuried);
    httpGetAsync(urlWithRandomGradeID, (result) => {
        console.log(currentIndex + result);
        checkFinishConditionAndLog(currentIndex, totalNumberOfQueries);
    })
}

function queryGrade_blocking(){
    let urlWithRandomGradeID = getURLWithRandomGradeID_blocking(gradesToBeQuried);
    httpGetAsync(urlWithRandomGradeID, (result) => console.log("blocking thread is done"))
}

function runQueries(){
    const totalNumberOfQueries = 1000;
    const totalNumberOfBlockingQueries = 15;

    Array(totalNumberOfBlockingQueries).fill().map((_, i) => queryGrade_blocking());
    Array(totalNumberOfQueries).fill().map((_, i) => queryGrade(i, totalNumberOfQueries));
}

runQueries();
des4xlb0

des4xlb01#

经过大量研究,我找到了这个答案,结果发现 delayElement 函数阻塞线程。

相关问题