BAEL-3298 Some more refactoring
This commit is contained in:
parent
02f140e1c5
commit
db549a4f8f
|
@ -1,6 +1,9 @@
|
|||
package org.baeldung.batch;
|
||||
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.baeldung.batch.model.Transaction;
|
||||
import org.baeldung.batch.service.RecordFieldSetMapper;
|
||||
import org.baeldung.batch.service.RetryItemProcessor;
|
||||
|
@ -12,7 +15,6 @@ import org.springframework.batch.core.configuration.annotation.StepBuilderFactor
|
|||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.UnexpectedInputException;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
||||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||
|
@ -35,6 +37,8 @@ public class SpringBatchRetryConfig {
|
|||
|
||||
private static final String[] tokens = { "username", "userid", "transactiondate", "amount" };
|
||||
|
||||
private static final int TWO_SECONDS = 2000;
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
|
@ -47,7 +51,7 @@ public class SpringBatchRetryConfig {
|
|||
@Value("file:xml/retryOutput.xml")
|
||||
private Resource outputXml;
|
||||
|
||||
public ItemReader<Transaction> itemReader(Resource inputData) throws Exception {
|
||||
public ItemReader<Transaction> itemReader(Resource inputData) throws ParseException {
|
||||
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
|
||||
tokenizer.setNames(tokens);
|
||||
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
|
||||
|
@ -60,6 +64,12 @@ public class SpringBatchRetryConfig {
|
|||
return reader;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloseableHttpClient closeableHttpClient() {
|
||||
final RequestConfig config = RequestConfig.custom().setConnectTimeout(TWO_SECONDS).build();
|
||||
return HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemProcessor<Transaction, Transaction> retryItemProcessor() {
|
||||
return new RetryItemProcessor();
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package org.baeldung.batch.service;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.baeldung.batch.model.Transaction;
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
|
@ -12,21 +10,16 @@ import org.codehaus.jettison.json.JSONObject;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RetryItemProcessor implements ItemProcessor<Transaction, Transaction> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RetryItemProcessor.class);
|
||||
|
||||
private static final int TWO_SECONDS = 2000;
|
||||
|
||||
private CloseableHttpClient client;
|
||||
|
||||
public RetryItemProcessor() {
|
||||
final RequestConfig config = RequestConfig.custom().setConnectTimeout(TWO_SECONDS).build();
|
||||
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||
}
|
||||
@Autowired
|
||||
private CloseableHttpClient closeableHttpClient;
|
||||
|
||||
@Override
|
||||
public Transaction process(Transaction transaction) throws IOException, JSONException {
|
||||
|
@ -44,6 +37,6 @@ public class RetryItemProcessor implements ItemProcessor<Transaction, Transactio
|
|||
|
||||
private HttpResponse fetchMoreUserDetails(int id) throws IOException {
|
||||
final HttpGet request = new HttpGet("http://www.baeldung.com:81/user/" + id);
|
||||
return client.execute(request);
|
||||
return closeableHttpClient.execute(request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,13 +52,12 @@ public class SpringBatchRetryIntegrationTest {
|
|||
private JobLauncherTestUtils jobLauncherTestUtils;
|
||||
|
||||
@Mock
|
||||
private CloseableHttpClient httpClient;
|
||||
private CloseableHttpClient closeableHttpClient;
|
||||
|
||||
@Mock
|
||||
private CloseableHttpResponse httpResponse;
|
||||
|
||||
@InjectMocks
|
||||
@Autowired
|
||||
private RetryItemProcessor retryItemProcessor;
|
||||
|
||||
@Before
|
||||
|
@ -68,7 +67,7 @@ public class SpringBatchRetryIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenEndpointAlwaysFailing_thenJobFails() throws Exception {
|
||||
when(httpClient.execute(any())).thenThrow(new ConnectTimeoutException("Endpoint is down"));
|
||||
when(closeableHttpClient.execute(any())).thenThrow(new ConnectTimeoutException("Endpoint is down"));
|
||||
|
||||
JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters());
|
||||
JobInstance actualJobInstance = jobExecution.getJobInstance();
|
||||
|
@ -86,7 +85,7 @@ public class SpringBatchRetryIntegrationTest {
|
|||
|
||||
//fails for first two calls and passes third time onwards
|
||||
when(httpResponse.getEntity()).thenReturn(new StringEntity("{ \"age\":10, \"postCode\":\"430222\" }"));
|
||||
when(httpClient.execute(any()))
|
||||
when(closeableHttpClient.execute(any()))
|
||||
.thenThrow(new ConnectTimeoutException("Timeout count 1"))
|
||||
.thenThrow(new ConnectTimeoutException("Timeout count 2"))
|
||||
.thenReturn(httpResponse);
|
||||
|
|
Loading…
Reference in New Issue