简体   繁体   中英

Spring multiple requests simultaneously

This is the code that sends a POST request with the link data of each post after receiving the list of posts in db.

After requesting POST with each link, extract the playerCount from the response and update it to each post.

I am using Resttemplate in this code, but there is an issue that takes too long.

So I want to change this code to sending a request at once and update each post when all the requests are finished.

How can i convert this code to what i want?

I'm going to use this code as a scheduled task.

    @Test
    @Transactional
    @Rollback(false)
    public void postToGraphql2() throws JsonProcessingException, JSONException {
        String URL = "https://gt-space-data.herokuapp.com/graphql";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-type", "application/json");

        long startTime = System.currentTimeMillis();

        List<Posts> list = postsRepository.findPostsByCategoryStringContaining("GATHERTOWN");
        String query = "query gameData($apikey:String!,$spaceid:String!,$spacename:String!){gameData(spaceData:{apiKey: $apikey, spaceIdNum: $spaceid, spaceName: $spacename}){playerCount,}}";
        String opertationName = "gameData";

        list.forEach(posts -> {
            String link = posts.getLink();
            if(link.contains("gather.town")){
                String spaceid = link.substring(link.indexOf("app/")+4,link.lastIndexOf("/"));
                String spacename = link.substring(link.lastIndexOf("/")+1, link.length());
                String variables = "{\"apikey\": \"QUNCVEQGILsqe5\",\"spaceid\": \""+spaceid+"\",\"spacename\" : \""+spacename+"\"}";

                try {
                    ResponseEntity<PlayerCountDto> response =
                            restTemplate.postForEntity(URL, new HttpEntity<>(createJsonQueries(query,opertationName,variables), headers), PlayerCountDto.class);
                    int playerCount = Objects.requireNonNull(response.getBody()).getData().getGameData().playerCount;
                    posts.setPlayerCount(playerCount);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        });

        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;

        System.out.println(elapsedTime);
    }

One way is to replace Resttemplate with 'Webclient'. Webclient is part of Spring Webflux introduced in Spring 5.0. Webclient is asynchronous and non-blocking.

You can start with following document

https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/html/boot-features-webclient.html

I changed my code Resttemplate to webclient but not working when using subscribe().

It works well when using.block() but it takes too long.

How do I get.subscribe() to work?

    @Test
    @Transactional
    @Rollback(false)
    public void postToGraphql3() throws JsonProcessingException, JSONException {
        String URL = "https://gt-space-data.herokuapp.com/graphql";

        WebClient webClient = webClientBuilder.baseUrl(URL).defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

        long startTime = System.currentTimeMillis();

        List<Posts> list = postsRepository.findPostsByCategoryStringContaining("GATHERTOWN");
        String query = "query gameData($apikey:String!,$spaceid:String!,$spacename:String!){gameData(spaceData:{apiKey: $apikey, spaceIdNum: $spaceid, spaceName: $spacename}){playerCount,}}";
        String opertationName = "gameData";

        list.forEach(posts -> {
            String link = posts.getLink();
            String graphqlQuery = null;
            if(link.contains("gather.town")){
                String spaceid = link.substring(link.indexOf("app/")+4,link.lastIndexOf("/"));
                String spacename = link.substring(link.lastIndexOf("/")+1, link.length());
                String variables = "{\"apikey\": \"QUNCVEQGILsqeXR5\",\"spaceid\": \""+spaceid+"\",\"spacename\" : \""+spacename+"\"}";

                try {
                    graphqlQuery = createJsonQueries(query,opertationName,variables);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }

            //not working when using .subscribe()
            Mono<PlayerCountDto> playerCountDtoMono = webClient.post().bodyValue(graphqlQuery)
                    .retrieve()
                    .bodyToMono(PlayerCountDto.class);

            playerCountDtoMono.doOnSuccess(
                    playerCountDto -> {
                        System.out.println(playerCountDto.getData().gameData.getPlayerCount());
                    }
            ).subscribe();
        });

        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;

        System.out.println(elapsedTime);

    }

log

2022-04-24 12:15:08.101  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.1              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.102  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.1              : | request(unbounded)
2022-04-24 12:15:08.856  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.2              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.856  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.2              : | request(unbounded)
2022-04-24 12:15:08.859  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.3              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.859  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.3              : | request(unbounded)
2022-04-24 12:15:08.861  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.4              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.862  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.4              : | request(unbounded)
2022-04-24 12:15:08.867  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.5              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.867  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.5              : | request(unbounded)
2022-04-24 12:15:08.869  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.6              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.870  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.6              : | request(unbounded)
2022-04-24 12:15:08.872  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.7              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.873  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.7              : | request(unbounded)
2022-04-24 12:15:08.876  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.8              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.877  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.8              : | request(unbounded)
2022-04-24 12:15:08.880  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.9              : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.880  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.9              : | request(unbounded)
2022-04-24 12:15:08.882  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.10             : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.882  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.10             : | request(unbounded)
2022-04-24 12:15:08.884  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.11             : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.884  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.11             : | request(unbounded)
2022-04-24 12:15:08.887  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.12             : | onSubscribe([Fuseable] MonoPeekTerminal.MonoTerminalPeekSubscriber)
2022-04-24 12:15:08.887  INFO 29112 --- [    Test worker] reactor.Mono.PeekTerminal.12             : | request(unbounded)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM