From 174a766972c4f03c71e25fea3551afaa6ee36776 Mon Sep 17 00:00:00 2001 From: ezerpa Date: Fri, 3 Jul 2026 15:42:19 -0400 Subject: [PATCH] v1.0.13 - Retry logic (3 intentos) + timeout 30s/60s --- META-INF/MANIFEST.MF | 2 +- .../bcvrate/service/BCVApiService.java | 91 +++++++++++-------- 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 2a8e13b..b693aec 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: BCV Exchange Rate Plugin Bundle-SymbolicName: com.venezuela.bcvrate;singleton:=true -Bundle-Version: 1.0.12 +Bundle-Version: 1.0.13 Bundle-Vendor: Ezerpa Bundle-RequiredExecutionEnvironment: JavaSE-11 Bundle-ActivationPolicy: lazy diff --git a/src/com/venezuela/bcvrate/service/BCVApiService.java b/src/com/venezuela/bcvrate/service/BCVApiService.java index a3e09ab..ac918c2 100644 --- a/src/com/venezuela/bcvrate/service/BCVApiService.java +++ b/src/com/venezuela/bcvrate/service/BCVApiService.java @@ -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.*?strong-tb\">\\s*([\\d.,]+)\\s*", 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.*?strong-tb\">\\s*([\\d.,]+)\\s*", 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) {