Can we make parallel Service calls using Spring fr

2019-09-13 01:52发布

问题:

I have a website with a home page and a search text and button and 5-6 tabs. Based on the search text, there would be independent service calls (API calls to backend) for the tabs. Each tab would have different data. After clicking on search , I am planning to fetch data for one tab and load it in the UI and let the data for other tabs be loaded in the background.

I am planning to use Spring MVC for this task, but I am not sure if spring supports this kind of parallel service calls.

Are there any other frameworks that can do this?

Any pointers would be helpful.

回答1:

Spring MVC can do this. Because it can handle parallel Requests (It will not take care that they come from the same client).

The "problem" is to have the client to send this parallel requests (one for each tab).



回答2:

independent service calls (API calls to backend)

I guess, you are talking about Ajax requests (HTTP).

All Java EE web framework (and of course SpringFramework MVC) is based on HttpServlet usage. You may also read JSR-315 specification.

What is important, that every servlet request is running in separate thread. Thread creation is carried out by servlet container (tomcat, GlassFish, Jetty, etc), so you are not required to do it manually.

So the answer definitely is yes.

Concerning other frameworks, please look to the list in wiki.

UPD. About client. Ajax requests are asynchronous by definition, so it's easy to implement parallel requests.

Small and quick example on how to make parallel calls on client (Jquery):

<script type="text/javascript">
$( document ).ready(function() {
    $("#searchButton").click(function(){
        var pattern = $("#searchText").val();
        var processingMap = [
            {tabid:"tab1", url:"./first-api-call"},
            {tabid:"tab2", url:"./second-api-call"},
            {tabid:"tab3", url:"./third-api-call"},
            {tabid:"tab4", url:"./fourth-api-call"},
            {tabid:"tab5", url:"./fifth-api-call"}
        ];

        $(processingMap).each(function(index, element){
            $.ajax(element.url,{
                'type':'get',
                'data':{'pattern':pattern},
                'dataType':'text',
                'success':function(data, textStatus, jqXHR){
                    setTabContent(element.tabid,data);
                }
            });
        });
    });

    function setTabContent(tabid, content){
        $('#'+tabid).text(content);
    }

}); 
</script>

<input type="text" value="" id="searchText">
<input type="button" value="Search" id="searchButton">

<div id="tab1">tab1</div>
<div id="tab2">tab2</div>
<div id="tab3">tab3</div>
<div id="tab4">tab4</div>
<div id="tab5">tab5</div>