简体   繁体   中英

Configure spring boot retry for multiple API calls

I have a spring boot api application which has a POST endpoint,Lets call it /doSomething as method .when receive request for /doSomething endpoint i need to save that data on our application and then needs to make GET request for another api[A] with that request have to GET from API[B] and then again have to post to API[B].In that case what is the best way to handle spring retry.

Please find the below code

@RequestMapping(value = "/subpub", method = RequestMethod.POST, headers = {"content-type=application/x-www-form-urlencoded"})
    public String subPub(HttpServletRequest request, HttpServletResponse response, @RequestBody String rawBody) {
    //persists some data on this database

    //this method will invoke api[A] and api[B]
    integrationServiceBean.processCourseMetaData("_id");
    return "OK"
}; 

IntegrationServiceBean class

package com.org.reader.integration;

@Service
public class IntegrationServiceBean {


    /**
     * This method will process meta data submission for  
     * section details by section id and update meta data
     *
     * @param sectionId
     */
    @Retryable(RuntimeException.class)
    public void processCourseMetaData(final String sectionId) {

        System.out.println("Invoking processCourseMetaData");

        ResponseEntity<String> responseEntity = registrarService.findOneSection(sectionId);
        String responseBody = responseEntity.getBody();

        LinkedHashMap requestObj = (LinkedHashMap) JsonUtils.jsonToObject(responseBody);
        LinkedHashMap metaDataObj = (LinkedHashMap) requestObj.get(Constant.Response.META_DATA);
        if (!contextConfig.getMetaDataCopyable().isEmpty()) {

            metaDataObj.put(Constant.MetaData.COPYABLE, contextConfig.getMetaDataCopyable());
        }
        if (!contextConfig.getMetaDataPending().isEmpty()) {

            metaDataObj.put(Constant.MetaData.PENDING, contextConfig.getMetaDataPending());
        }
        metaDataObj.put(Constant.MetaData.LAUNCH_URL, getLaunchUrlByEnvironment(requestObj, sectionId));

        String updatedSectionPayload = JsonUtils.toJsonString(requestObj);

        registrarService.updateSection(sectionId, updatedSectionPayload);
    }

    @Recover
    public void recover(RuntimeException e){
        System.out.println("Recovering - returning safe value"+e.getMessage());

    }



}

My Problem is if retry is applied for integration service bean will it be a performance impact for the main part of the application ex.for the saving data on main end point.

And what would be the best practice

A method tagged @Retryable is called in separated thread by blocking the current executing thread.

In term of your code prospective,

Saving is happening in thread (say thread A) and Integration service is being processed in other thread (say thread B). Thus, Thread A is blocked until B finishes off. So next line of integrationServiceBean.processCourseMetaData("_id"); is blocked until it is finished by success or exhaustion of retrying limit .

Coming to your question.

Saving data is untouched. So I don't think, there is any performance hit.

Talking about best practice

using Retry, whenever there is a network partition among services, is good practice. It makes application robust.

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