SUPPORT-8507: moving retryable methode to the separated class
This commit is contained in:
parent
22b8665ca5
commit
d9470861c1
2 changed files with 62 additions and 38 deletions
|
|
@ -11,7 +11,6 @@ import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpDelete;
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
|
@ -34,16 +33,13 @@ import org.springframework.kafka.core.KafkaTemplate;
|
||||||
import org.springframework.kafka.support.Acknowledgment;
|
import org.springframework.kafka.support.Acknowledgment;
|
||||||
import org.springframework.kafka.support.SendResult;
|
import org.springframework.kafka.support.SendResult;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.retry.annotation.Backoff;
|
|
||||||
import org.springframework.retry.annotation.Retryable;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.micord.ervu.av.exception.FileUploadException;
|
import ru.micord.ervu.av.exception.FileUploadException;
|
||||||
import ru.micord.ervu.av.exception.InvalidHttpFileUrlException;
|
import ru.micord.ervu.av.exception.InvalidHttpFileUrlException;
|
||||||
import ru.micord.ervu.av.exception.RetryableException;
|
|
||||||
import ru.micord.ervu.av.kafka.dto.InMessage;
|
import ru.micord.ervu.av.kafka.dto.InMessage;
|
||||||
import ru.micord.ervu.av.kafka.dto.OutErrorMessage;
|
import ru.micord.ervu.av.kafka.dto.OutErrorMessage;
|
||||||
import ru.micord.ervu.av.response.AvResponse;
|
|
||||||
import ru.micord.ervu.av.response.AvFileSendResponse;
|
import ru.micord.ervu.av.response.AvFileSendResponse;
|
||||||
|
import ru.micord.ervu.av.response.AvResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author r.latypov
|
* @author r.latypov
|
||||||
|
|
@ -60,13 +56,15 @@ public class FileUploadService {
|
||||||
private final KafkaTemplate<String, String> kafkaTemplate;
|
private final KafkaTemplate<String, String> kafkaTemplate;
|
||||||
private final NewTopic outErrorTopic;
|
private final NewTopic outErrorTopic;
|
||||||
private final NewTopic outSuccessTopic;
|
private final NewTopic outSuccessTopic;
|
||||||
|
private final ReceiveScanReportRetryable receiveScanReportRetryable;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public FileUploadService(KafkaTemplate<String, String> kafkaTemplate, NewTopic outErrorTopic,
|
public FileUploadService(KafkaTemplate<String, String> kafkaTemplate, NewTopic outErrorTopic,
|
||||||
NewTopic outSuccessTopic) {
|
NewTopic outSuccessTopic, ReceiveScanReportRetryable receiveScanReportRetryable) {
|
||||||
this.kafkaTemplate = kafkaTemplate;
|
this.kafkaTemplate = kafkaTemplate;
|
||||||
this.outErrorTopic = outErrorTopic;
|
this.outErrorTopic = outErrorTopic;
|
||||||
this.outSuccessTopic = outSuccessTopic;
|
this.outSuccessTopic = outSuccessTopic;
|
||||||
|
this.receiveScanReportRetryable = receiveScanReportRetryable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@KafkaListener(id = "${spring.kafka.consumer.group-id}", topics = "${kafka-in.topic.name}")
|
@KafkaListener(id = "${spring.kafka.consumer.group-id}", topics = "${kafka-in.topic.name}")
|
||||||
|
|
@ -201,8 +199,11 @@ public class FileUploadService {
|
||||||
stringBuilder.append(" Status: ").append(status).append(".");
|
stringBuilder.append(" Status: ").append(status).append(".");
|
||||||
}
|
}
|
||||||
if (avFileSendResponse.error() != null) {
|
if (avFileSendResponse.error() != null) {
|
||||||
stringBuilder.append(" Error code: ").append(avFileSendResponse.error().code())
|
stringBuilder.append(" Error code: ")
|
||||||
.append(". Error message: ").append(avFileSendResponse.error().message()).append(". ");
|
.append(avFileSendResponse.error().code())
|
||||||
|
.append(". Error message: ")
|
||||||
|
.append(avFileSendResponse.error().message())
|
||||||
|
.append(". ");
|
||||||
}
|
}
|
||||||
throw new FileUploadException(stringBuilder.toString());
|
throw new FileUploadException(stringBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
@ -220,7 +221,7 @@ public class FileUploadService {
|
||||||
throw new FileUploadException(e);
|
throw new FileUploadException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return receiveScanReport(client, get);
|
return receiveScanReportRetryable.receiveScanReport(client, get);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
|
|
@ -229,35 +230,6 @@ public class FileUploadService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retryable(retryFor = {RetryableException.class}, maxAttempts = 10,
|
|
||||||
backoff = @Backoff(delay = 1000))
|
|
||||||
public AvResponse receiveScanReport(CloseableHttpClient client, HttpGet get)
|
|
||||||
throws FileUploadException {
|
|
||||||
|
|
||||||
try (CloseableHttpResponse getResponse = client.execute(get)) {
|
|
||||||
int getStatusCode = getResponse.getStatusLine().getStatusCode();
|
|
||||||
if (getStatusCode == HttpStatus.OK.value()) {
|
|
||||||
String getResponseJson = EntityUtils.toString(getResponse.getEntity());
|
|
||||||
AvResponse avResponse = new Gson().fromJson(getResponseJson, AvResponse.class);
|
|
||||||
if (avResponse.completed() == null) {
|
|
||||||
throw new RetryableException();
|
|
||||||
}
|
|
||||||
return avResponse;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new FileUploadException("http status code " + getStatusCode + " for " + get.getURI()
|
|
||||||
+ " get request.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (ClientProtocolException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
// непредусмотренная ошибка доступа через http-клиент
|
|
||||||
throw new FileUploadException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void uploadFile(String filePath, String uploadUrl) throws FileUploadException {
|
private void uploadFile(String filePath, String uploadUrl) throws FileUploadException {
|
||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
package ru.micord.ervu.av.service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.http.client.ClientProtocolException;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.retry.annotation.Backoff;
|
||||||
|
import org.springframework.retry.annotation.Retryable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.micord.ervu.av.exception.FileUploadException;
|
||||||
|
import ru.micord.ervu.av.exception.RetryableException;
|
||||||
|
import ru.micord.ervu.av.response.AvResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author r.latypov
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ReceiveScanReportRetryable {
|
||||||
|
@Retryable(retryFor = {RetryableException.class}, maxAttempts = 10,
|
||||||
|
backoff = @Backoff(delay = 1000))
|
||||||
|
public AvResponse receiveScanReport(CloseableHttpClient client, HttpGet get)
|
||||||
|
throws FileUploadException {
|
||||||
|
|
||||||
|
try (CloseableHttpResponse getResponse = client.execute(get)) {
|
||||||
|
int getStatusCode = getResponse.getStatusLine().getStatusCode();
|
||||||
|
if (getStatusCode == HttpStatus.OK.value()) {
|
||||||
|
String getResponseJson = EntityUtils.toString(getResponse.getEntity());
|
||||||
|
AvResponse avResponse = new Gson().fromJson(getResponseJson, AvResponse.class);
|
||||||
|
if (avResponse.completed() == null) {
|
||||||
|
throw new RetryableException();
|
||||||
|
}
|
||||||
|
return avResponse;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new FileUploadException("http status code " + getStatusCode + " for " + get.getURI()
|
||||||
|
+ " get request.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (ClientProtocolException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
// непредусмотренная ошибка доступа через http-клиент
|
||||||
|
throw new FileUploadException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue