v1.0.13 - Retry logic (3 intentos) + timeout 30s/60s
This commit is contained in:
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
|||||||
Bundle-ManifestVersion: 2
|
Bundle-ManifestVersion: 2
|
||||||
Bundle-Name: BCV Exchange Rate Plugin
|
Bundle-Name: BCV Exchange Rate Plugin
|
||||||
Bundle-SymbolicName: com.venezuela.bcvrate;singleton:=true
|
Bundle-SymbolicName: com.venezuela.bcvrate;singleton:=true
|
||||||
Bundle-Version: 1.0.12
|
Bundle-Version: 1.0.13
|
||||||
Bundle-Vendor: Ezerpa
|
Bundle-Vendor: Ezerpa
|
||||||
Bundle-RequiredExecutionEnvironment: JavaSE-11
|
Bundle-RequiredExecutionEnvironment: JavaSE-11
|
||||||
Bundle-ActivationPolicy: lazy
|
Bundle-ActivationPolicy: lazy
|
||||||
|
|||||||
@@ -149,45 +149,64 @@ public class BCVApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BCVRateResponse getRateFromBCVWebsite() {
|
private BCVRateResponse getRateFromBCVWebsite() {
|
||||||
try {
|
int maxRetries = 3;
|
||||||
log.info("Iniciando scraping de BCV website...");
|
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||||
String html = httpGetHtml(BCV_WEBSITE);
|
try {
|
||||||
if (html == null) {
|
log.info("Scraping BCV - Intento " + attempt + "/" + maxRetries);
|
||||||
log.warning("No se pudo obtener pagina BCV (httpGetHtml returned null)");
|
String html = httpGetHtml(BCV_WEBSITE);
|
||||||
return null;
|
if (html == null) {
|
||||||
}
|
log.warning("Intento " + attempt + "/" + maxRetries + ": No se pudo obtener pagina BCV");
|
||||||
log.info("Pagina BCV obtenida, longitud: " + html.length());
|
if (attempt < maxRetries) {
|
||||||
|
log.info("Reintentando en 5 segundos...");
|
||||||
Matcher rateMatcher = Pattern.compile(
|
Thread.sleep(5000);
|
||||||
"USD</span>.*?strong-tb\">\\s*([\\d.,]+)\\s*</strong>", Pattern.DOTALL).matcher(html);
|
continue;
|
||||||
if (!rateMatcher.find()) {
|
}
|
||||||
log.warning("No se encontro tasa USD en pagina BCV (regex no match)");
|
log.severe("Scraping BCV fallo despues de " + maxRetries + " intentos (timeout o error de conexion)");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
log.info("Pagina BCV obtenida, longitud: " + html.length());
|
||||||
String rateStr = rateMatcher.group(1).replace(",", ".");
|
|
||||||
log.info("Tasa encontrada en HTML: " + rateStr);
|
Matcher rateMatcher = Pattern.compile(
|
||||||
BigDecimal dollar = new BigDecimal(rateStr).setScale(4, RoundingMode.HALF_UP);
|
"USD</span>.*?strong-tb\">\\s*([\\d.,]+)\\s*</strong>", Pattern.DOTALL).matcher(html);
|
||||||
|
if (!rateMatcher.find()) {
|
||||||
Matcher dateMatcher = Pattern.compile(
|
log.warning("No se encontro tasa USD en pagina BCV (regex no match)");
|
||||||
"content=\"(\\d{4}-\\d{2}-\\d{2})T").matcher(html);
|
return null;
|
||||||
String effectiveDate = null;
|
}
|
||||||
if (dateMatcher.find()) {
|
|
||||||
effectiveDate = dateMatcher.group(1);
|
String rateStr = rateMatcher.group(1).replace(",", ".");
|
||||||
}
|
log.info("Tasa encontrada en HTML: " + rateStr);
|
||||||
|
BigDecimal dollar = new BigDecimal(rateStr).setScale(4, RoundingMode.HALF_UP);
|
||||||
if (dollar.compareTo(BigDecimal.ZERO) > 0) {
|
|
||||||
if (effectiveDate == null) {
|
Matcher dateMatcher = Pattern.compile(
|
||||||
effectiveDate = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
|
"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) {
|
private BigDecimal extractUSD(String json) {
|
||||||
|
|||||||
Reference in New Issue
Block a user