v1.0.13 - Retry logic (3 intentos) + timeout 30s/60s
This commit is contained in:
@@ -149,45 +149,64 @@ public class BCVApiService {
|
||||
}
|
||||
|
||||
private BCVRateResponse getRateFromBCVWebsite() {
|
||||
try {
|
||||
log.info("Iniciando scraping de BCV website...");
|
||||
String html = httpGetHtml(BCV_WEBSITE);
|
||||
if (html == null) {
|
||||
log.warning("No se pudo obtener pagina BCV (httpGetHtml returned null)");
|
||||
return null;
|
||||
}
|
||||
log.info("Pagina BCV obtenida, longitud: " + html.length());
|
||||
|
||||
Matcher rateMatcher = Pattern.compile(
|
||||
"USD</span>.*?strong-tb\">\\s*([\\d.,]+)\\s*</strong>", Pattern.DOTALL).matcher(html);
|
||||
if (!rateMatcher.find()) {
|
||||
log.warning("No se encontro tasa USD en pagina BCV (regex no match)");
|
||||
return null;
|
||||
}
|
||||
|
||||
String rateStr = rateMatcher.group(1).replace(",", ".");
|
||||
log.info("Tasa encontrada en HTML: " + rateStr);
|
||||
BigDecimal dollar = new BigDecimal(rateStr).setScale(4, RoundingMode.HALF_UP);
|
||||
|
||||
Matcher dateMatcher = Pattern.compile(
|
||||
"content=\"(\\d{4}-\\d{2}-\\d{2})T").matcher(html);
|
||||
String effectiveDate = null;
|
||||
if (dateMatcher.find()) {
|
||||
effectiveDate = dateMatcher.group(1);
|
||||
}
|
||||
|
||||
if (dollar.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (effectiveDate == null) {
|
||||
effectiveDate = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
|
||||
int maxRetries = 3;
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
log.info("Scraping BCV - Intento " + attempt + "/" + maxRetries);
|
||||
String html = httpGetHtml(BCV_WEBSITE);
|
||||
if (html == null) {
|
||||
log.warning("Intento " + attempt + "/" + maxRetries + ": No se pudo obtener pagina BCV");
|
||||
if (attempt < maxRetries) {
|
||||
log.info("Reintentando en 5 segundos...");
|
||||
Thread.sleep(5000);
|
||||
continue;
|
||||
}
|
||||
log.severe("Scraping BCV fallo despues de " + maxRetries + " intentos (timeout o error de conexion)");
|
||||
return null;
|
||||
}
|
||||
log.info("Pagina BCV obtenida, longitud: " + html.length());
|
||||
|
||||
Matcher rateMatcher = Pattern.compile(
|
||||
"USD</span>.*?strong-tb\">\\s*([\\d.,]+)\\s*</strong>", Pattern.DOTALL).matcher(html);
|
||||
if (!rateMatcher.find()) {
|
||||
log.warning("No se encontro tasa USD en pagina BCV (regex no match)");
|
||||
return null;
|
||||
}
|
||||
|
||||
String rateStr = rateMatcher.group(1).replace(",", ".");
|
||||
log.info("Tasa encontrada en HTML: " + rateStr);
|
||||
BigDecimal dollar = new BigDecimal(rateStr).setScale(4, RoundingMode.HALF_UP);
|
||||
|
||||
Matcher dateMatcher = Pattern.compile(
|
||||
"content=\"(\\d{4}-\\d{2}-\\d{2})T").matcher(html);
|
||||
String effectiveDate = null;
|
||||
if (dateMatcher.find()) {
|
||||
effectiveDate = dateMatcher.group(1);
|
||||
}
|
||||
|
||||
if (dollar.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (effectiveDate == null) {
|
||||
effectiveDate = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
|
||||
}
|
||||
log.info("Scraping BCV exitoso (intento " + attempt + "): USD=" + dollar + " effective=" + effectiveDate);
|
||||
return new BCVRateResponse(dollar, effectiveDate);
|
||||
}
|
||||
return null;
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.warning("Scraping BCV interrumpido");
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.warning("Intento " + attempt + "/" + maxRetries + ": Error scraping BCV: " + e.getMessage());
|
||||
if (attempt < maxRetries) {
|
||||
log.info("Reintentando en 5 segundos...");
|
||||
try { Thread.sleep(5000); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
|
||||
} else {
|
||||
log.severe("Scraping BCV fallo despues de " + maxRetries + " intentos: " + e.getMessage());
|
||||
}
|
||||
log.info("Scraping BCV exitoso: USD=" + dollar + " effective=" + effectiveDate);
|
||||
return new BCVRateResponse(dollar, effectiveDate);
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.log(Level.WARNING, "Error scraping BCV website: " + e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private BigDecimal extractUSD(String json) {
|
||||
|
||||
Reference in New Issue
Block a user