Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Spring Batch batch failed retry

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to configure Spring Batch batch failure retry". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction

By default, any errors in the execution of Spring batch jobs will fail. Sometimes, however, in order to improve the resilience of the application, we need to deal with such intermittent failures. In this short article, let's explore how to configure retry logic in the Spring batch framework.

two。 A simple example

Suppose you have a batch job that reads a CSV file as input:

Username, userid, transaction_date, transaction_amount sammy, 1234, 31/10/2015, 10000 john, 9999, 3/12/2015, 12321

It then processes each record by accessing the REST endpoint, getting the user's age and postCode properties:

Public class RetryItemProcessor implements ItemProcessor {@ Override public Transaction process (Transaction transaction) throws IOException {log.info ("RetryItemProcessor, attempting to process: {}", transaction); HttpResponse response = fetchMoreUserDetails (transaction.getUserId ()); / / parse user's age and postCode from response and update transaction. Return transaction;}...}

Finally, it generates and outputs a merged XML:

10000.0 2015-10-31 00:00:00 1234 sammy 10 430222.

3. Add retry to ItemProcessor

Now suppose what if the connection to the REST endpoint times out due to the slow speed of some networks? If this happens, our batch work will fail.

In this case, we want the failed item processing to retry several times. So next I configure the batch job to perform up to three retries in the event of a failure:

@ Bean public Step retryStep (ItemProcessor processor, ItemWriter writer) throws ParseException {return stepBuilderFactory .get ("retryStep") .chunk (10) .reader (itemReader (inputCsv)) .processor (processor) .writer (writer) .faultTolerant () .retryLimit (3) .retry (ConnectTimeoutException.class) .retry (DeadlockLoserDataAccessException.class) .build ();}

FaultTolerant () is called here to enable the retry feature. In addition, we use retry and retryLimit to define the exceptions that meet the retry criteria and the maximum number of retries for item, respectively.

4. Number of test retries

Suppose we have a test scenario where the REST endpoint that returns age and postCode is shut down for a while. In this test scenario, we only get a ConnectTimeoutException for the first two API calls, and the third call will succeed:

@ Test public void whenEndpointFailsTwicePasses3rdTime_thenSuccess () throws Exception {FileSystemResource expectedResult = new FileSystemResource (EXPECTED_OUTPUT); FileSystemResource actualResult = new FileSystemResource (TEST_OUTPUT); when (httpResponse.getEntity ()) .thenReturn (new StringEntity ("{\" age\ ": 10,\" postCode\ ":\" 430222\ "}") / / fails for first two calls and passes third time onwards when (httpClient.execute (any () .thenThrow (new ConnectTimeoutException ("Timeout count 1")) .thenThrow (new ConnectTimeoutException ("Timeout count 2")) .thenReturn (httpResponse); JobExecution jobExecution = jobLauncherTestUtils .launchJob (defaultJobParameters ()); JobInstance actualJobInstance = jobExecution.getJobInstance (); ExitStatus actualJobExitStatus = jobExecution.getExitStatus (); assertThat (actualJobInstance.getJobName (), is ("retryBatchJob")) AssertThat (actualJobExitStatus.getExitCode (), is ("COMPLETED")); AssertFile.assertFileEquals (expectedResult, actualResult);}

Here, our work has been successfully completed. In addition, it is clear from the log that the first record id=1234 failed twice and finally succeeded on the third retry:

19Executing step 06retryStep 57.742 [main] INFO o.s.batch.core.job.SimpleStepHandler-Executing step: [retryStep] 19Vl06VOV 57.758 [main] INFO o.b.batch.service.RetryItemProcessor-Attempting to process user with id=1234 19VO6VOR 57.758 [main] INFO o.b.batch.service.RetryItemProcessor-Attempting to process user with id=1234 19VO6VOV 06VOV 57.758 [main] INFO o.b.batch.service.RetryItemProcessor-Attempting to process user with id=1234 19Vl06VOV 57.758 [main ] INFO o.b.batch.service.RetryItemProcessor-Attempting to process user with id=9999 1906VR 57.773 [main] INFO o.s.batch.core.step.AbstractStep-Step: [retryStep] executed in 31ms

Similarly, take a look at another test case and what happens when all the retries are used up:

@ Test public void whenEndpointAlwaysFail_thenJobFails () throws Exception {when (httpClient.execute (any () .thenThrow (new ConnectTimeoutException ("Endpoint is down")); JobExecution jobExecution = jobLauncherTestUtils .launchJob (defaultJobParameters ()); JobInstance actualJobInstance = jobExecution.getJobInstance (); ExitStatus actualJobExitStatus = jobExecution.getExitStatus (); assertThat (actualJobInstance.getJobName (), is ("retryBatchJob")); assertThat (actualJobExitStatus.getExitCode (), is ("FAILED")) AssertThat (actualJobExitStatus.getExitDescription (), containsString ("org.apache.http.conn.ConnectTimeoutException");}

In this test case, three attempts are made to retry the first record before the job fails because of ConnectTimeoutException.

5. Retry using XML configuration

Finally, let's take a look at the XML equivalent to the above configuration:

This is the end of "how to configure Spring Batch batch failed retry". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report