Skip to content

Commit

Permalink
Merge pull request #3 from federicomartinlara1976/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
federicomartinlara1976 authored Feb 3, 2023
2 parents 9b35a7a + 067f17e commit 9843a74
Show file tree
Hide file tree
Showing 21 changed files with 637 additions and 121 deletions.
46 changes: 46 additions & 0 deletions LOG_PATH_IS_UNDEFINED/chguadalquivir-batch.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
13:51:42.582 [main] INFO n.b.c.c.ChGuadalquivirApplication - Starting ChGuadalquivirApplication using Java 1.8.0_202 on discovery with PID 47281 (/home/federico/git/chguadalquivir-batch/target/classes started by federico in /home/federico/git/chguadalquivir-batch)
13:51:42.585 [main] INFO n.b.c.c.ChGuadalquivirApplication - No active profile set, falling back to 1 default profile: "default"
13:52:26.159 [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
at net.bounceme.chronos.chguadalquivir.ChGuadalquivirApplication.main(ChGuadalquivirApplication.java:18)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:229)
at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:43)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
... 14 common frames omitted
Caused by: java.lang.IllegalArgumentException: standardService.connector.startFailed
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:238)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:282)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:213)
... 16 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1077)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:234)
... 18 common frames omitted
Caused by: java.net.BindException: La dirección ya se está usando
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:275)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:230)
at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1227)
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1313)
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:617)
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1074)
... 20 common frames omitted
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package net.bounceme.chronos.chguadalquivir.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.bounceme.chronos.chguadalquivir.model.Status;
import net.bounceme.chronos.chguadalquivir.model.Task;

@RestController
@RequestMapping("/api")
@Slf4j
public class JobController {

@Autowired
private ApplicationContext ctx;

@Autowired
private JobLauncher jobLauncher;

/**
* @return
*/
@GetMapping("/")
public String index() {
return "Hello world";
}

/**
* @return
*/
@GetMapping("/status")
public ResponseEntity<Status> status() {
Status status = Status.builder().version(System.getProperty("java.version"))
.platform(System.getProperty("os.name")).response("OK").build();

return new ResponseEntity<>(status, HttpStatus.OK);
}

@PostMapping("/execute")
public ResponseEntity<Map<String, Object>> executeTask(@Valid @RequestBody Task task, BindingResult result) {
Map<String, Object> response = new HashMap<>();

try {
if (result.hasErrors()) {
List<String> errors = result.getFieldErrors().stream()
.map(error -> String.format("El campo '%s' %s", error.getField(), error.getDefaultMessage()))
.collect(Collectors.toList());

response.put("errors", errors);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

log.info("Ejecutar: {}", task.getName());
run(task.getName());
response.put("mensaje", "Tarea ejecutada correctamente");
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (Exception e) {
response.put("error", e.getMessage());
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

/**
* @param name
* @throws Exception
*/
private void run(String name) throws Exception {
try {
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());

Job job = ctx.getBean(name, Job.class);
JobExecution result = jobLauncher.run(job, builder.toJobParameters());

// Exit on failure
if (ExitStatus.FAILED.equals(result.getExitStatus())) {
throw new Exception("La tarea ha fallado");
}
} catch (NoSuchBeanDefinitionException e) {
throw new Exception("Tarea no encontrada");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.bounceme.chronos.chguadalquivir.model;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class Status implements Serializable {

/**
*
*/
private static final long serialVersionUID = -6605836896150803431L;

@Getter
@Setter
private String version;

@Getter
@Setter
private String platform;

@Getter
@Setter
private String response;
}
27 changes: 27 additions & 0 deletions src/main/java/net/bounceme/chronos/chguadalquivir/model/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.bounceme.chronos.chguadalquivir.model;

import java.io.Serializable;

import javax.validation.constraints.NotEmpty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class Task implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6557119505034879817L;

@NotEmpty(message = "no puede estar vacío")
@Getter
@Setter
private String name;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.bounceme.chronos.chguadalquivir.model.Embalse;
import net.bounceme.chronos.chguadalquivir.model.Zona;
Expand All @@ -30,12 +31,9 @@ public class DailyRegisterItemReader implements ItemReader<Embalse>, Initializin
@Value("${application.importJob.url}")
private String url;

@Value("${application.importJob.excludeHeader}")
private Boolean excludeHeader;

@Autowired
private ObjectMapper mapper;

@Autowired
private CHGuadalquivirHelper helper;

Expand All @@ -50,7 +48,6 @@ public class DailyRegisterItemReader implements ItemReader<Embalse>, Initializin
@Override
public void afterPropertiesSet() {
Assert.notNull(url, "Must provide the url");
Assert.notNull(excludeHeader, "excludeHeader must be [true/false]");
Assert.notNull(elementMapper, "Must provide a elementMapper");

initialize();
Expand All @@ -59,38 +56,34 @@ public void afterPropertiesSet() {
/**
*
*/
@SneakyThrows
private void initialize() {
try {
List<Zona> zonas = helper.getZonesFromJsonResource(mapper, "zonas.json");
records = new ArrayList<>();

Document doc = helper.retrieveDocument(url);
Map<String, String> postData = helper.initFormData(doc);
String selectName = helper.getNameFrom(doc, "form2", "DDBzona");
List<Zona> zonas = helper.getZonesFromJsonResource(mapper, "zonas.json");
records = new ArrayList<>();

for (Zona zona : zonas) {
postData.put(selectName, zona.getCodigo());
log.debug("{}", postData.toString());
Document doc = helper.retrieveDocument(url);
Map<String, String> postData = helper.initFormData(doc);
String selectName = helper.getNameFrom(doc, "form2", "DDBzona");

doc = Jsoup.connect(url).data(postData).post();
for (Zona zona : zonas) {
postData.put(selectName, zona.getCodigo());
log.debug("{}", postData.toString());

Elements elements = doc.select("table#ContentPlaceHolder1_GridNivelesEmbalses > tbody > tr");
doc = Jsoup.connect(url).data(postData).post();

Integer inicio = (excludeHeader) ? 1 : 0;
Elements elements = doc.select("table#ContentPlaceHolder1_GridNivelesEmbalses > tbody > tr");

for (int i = inicio; i < elements.size(); i++) {
ZonaElement ze = new ZonaElement();
ze.setZona(zona);
ze.setElement(elements.get(i));

records.add(ze);
}
}
for (int i = 1; i < elements.size(); i++) {
ZonaElement ze = new ZonaElement();
ze.setZona(zona);
ze.setElement(elements.get(i));

index = 0;
} catch (Exception e) {
log.error(e.getMessage());
records.add(ze);
}
}

index = 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import net.bounceme.chronos.chguadalquivir.model.Execution;
import net.bounceme.chronos.chguadalquivir.repository.ExecutionsRepository;
import net.bounceme.chronos.chguadalquivir.support.CHGuadalquivirHelper;

@Component
@Slf4j
public class ExecutionsItemReader implements ItemReader<Execution>, InitializingBean {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import net.bounceme.chronos.chguadalquivir.model.Execution;
import net.bounceme.chronos.chguadalquivir.reader.mapping.StatExecutionsLineMapper;

@Component
@Slf4j
public class StatExecutionsFileItemReader extends FlatFileItemReader<Execution> implements InitializingBean {

@Autowired
private Environment environment;

@Autowired
private SimpleDateFormat dateFormat;

@Autowired
private StatExecutionsLineMapper lineMapper;

Expand All @@ -37,23 +35,18 @@ public void afterPropertiesSet() {
*
*/
private void initialize() {
try {
String exportFilePath = environment.getRequiredProperty("application.lastExecutions.export.file.path");
String suffix = environment.getRequiredProperty("application.lastExecutions.export.file.suffix");

Date date = new Date();
String sDate = dateFormat.format(date);
Resource exportFileResource = new FileSystemResource(exportFilePath + "-" + sDate + "." + suffix);
setResource(exportFileResource);

//Set number of lines to skips. Use it if file has header rows.
setLinesToSkip(1);

//Configure how each line will be parsed and mapped to different values
setLineMapper(lineMapper);

} catch (Exception e) {
log.error("ERROR: ", e);
}
String exportFilePath = environment.getRequiredProperty("application.lastExecutions.export.file.path");
String suffix = environment.getRequiredProperty("application.lastExecutions.export.file.suffix");

Date date = new Date();
String sDate = dateFormat.format(date);
Resource exportFileResource = new FileSystemResource(exportFilePath + "-" + sDate + "." + suffix);
setResource(exportFileResource);

// Set number of lines to skips. Use it if file has header rows.
setLinesToSkip(1);

// Configure how each line will be parsed and mapped to different values
setLineMapper(lineMapper);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.bounceme.chronos.chguadalquivir.reader.mapping;

import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.stereotype.Component;

import net.bounceme.chronos.chguadalquivir.model.Execution;

@Component
public class StatExecutionsFieldMapper extends BeanWrapperFieldSetMapper<Execution> {

public StatExecutionsFieldMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class StatExecutionsLineMapper extends DefaultLineMapper<Execution> imple
@Autowired
private StatExecutionsLineTokenizer lineTokenizer;

@Autowired
private StatExecutionsFieldMapper fieldMapper;

@Override
Expand Down
Loading

0 comments on commit 9843a74

Please sign in to comment.