Skip to content

Commit

Permalink
Merge pull request #1759 from lat-lon/fix/gmlLoaderNoQualifyingBean-1…
Browse files Browse the repository at this point in the history
…0115

Fix missing JobBuilderFactory bean in GmlLoaderConfiguration
  • Loading branch information
copierrj authored Nov 20, 2024
2 parents ebe27e0 + 5c32ee4 commit fdacadc
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
5 changes: 5 additions & 0 deletions deegree-tools/deegree-tools-gml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@
<version>${spring-batch.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.support.AbstractItemStreamItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.PlatformTransactionManager;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -59,6 +63,8 @@

import static org.slf4j.LoggerFactory.getLogger;

import javax.sql.DataSource;

/**
* Configuration of the GMLLoader.
*
Expand All @@ -71,12 +77,6 @@ public class GmlLoaderConfiguration {

private static final Logger LOG = getLogger(GmlLoaderConfiguration.class);

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@JobScope
@Bean
public Summary summary(@Value("#{jobParameters[reportWriteStatistics] ?: false}") boolean reportWriteStatistics) {
Expand Down Expand Up @@ -192,13 +192,14 @@ public StepExecutionListener referenceCheckListener(SQLFeatureStore sqlFeatureSt

@JobScope
@Bean
public Step step(StepExecutionListener referenceCheckListener, AbstractItemStreamItemReader<Feature> gmlReader,
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager,
StepExecutionListener referenceCheckListener, AbstractItemStreamItemReader<Feature> gmlReader,
FeatureReferencesParser featureReferencesParser, ItemWriter<Feature> featureStoreWriter,
@Value("#{jobParameters['chunkSize']}") Integer chunkSize,
@Value("#{jobParameters['skipReferenceCheck'] ?: false}") boolean skipReferenceCheck) {
int chunk = chunkSize != null && chunkSize.intValue() > 10 ? chunkSize.intValue() : 10;
SimpleStepBuilder<Feature, Feature> builder = stepBuilderFactory.get("gmlLoaderStep")
.<Feature, Feature>chunk(chunk);
SimpleStepBuilder<Feature, Feature> builder = new StepBuilder("gmlLoaderStep", jobRepository)
.<Feature, Feature>chunk(chunk, transactionManager);
builder.reader(gmlReader);
if (skipReferenceCheck) {
LOG.warn("The feature reference check will be skipped.");
Expand All @@ -210,14 +211,27 @@ public Step step(StepExecutionListener referenceCheckListener, AbstractItemStrea
}

@Bean
public Job job(Step step, ReportWriter reportWriter) {
return jobBuilderFactory.get("gmlLoaderJob")
.incrementer(new RunIdIncrementer())
public Job job(JobRepository jobRepository, Step step, ReportWriter reportWriter) {
return new JobBuilder("gmlLoaderJob", jobRepository).incrementer(new RunIdIncrementer())
.start(step)
.listener(reportWriter)
.build();
}

@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
.addScript("classpath:org/springframework/batch/core/schema-h2.sql")
.setType(EmbeddedDatabaseType.H2)
.build();
}

@Bean
public ResourcelessTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}

private List<String> parseDisabledResources(String disabledResources) {
List<String> patterns = new ArrayList<>();
if (disabledResources != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.io.PrintWriter;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Collection;

Expand All @@ -51,7 +53,7 @@ public class ReportWriter extends JobExecutionListenerSupport {

private static final Logger LOG = getLogger(ReportWriter.class);

private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

private final Summary summary;

Expand Down Expand Up @@ -134,13 +136,13 @@ private String getTimeNeeded(StepExecution stepExecution) {

private String getStartTime(StepExecution stepExecution) {
if (stepExecution != null && stepExecution.getStartTime() != null)
return DATE_FORMAT.format(stepExecution.getStartTime());
return stepExecution.getStartTime().format(DATE_FORMAT);
return "UNKNOWN";
}

private String getEndTime(StepExecution stepExecution) {
if (stepExecution != null && stepExecution.getEndTime() != null)
return DATE_FORMAT.format(stepExecution.getEndTime());
return stepExecution.getEndTime().format(DATE_FORMAT);
return "UNKNOWN";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.deegree.tools.featurestoresql.loader;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author <a href="mailto:[email protected]">Lyn Goltz </a>
*/
public class GmlLoaderConfigurationTest {

@Test
public void testLoadApplicationContextAndInitializeBeans() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(GmlLoaderConfiguration.class);
context.refresh();
context.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.deegree.tools.featurestoresql.loader;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collections;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;

/**
* @author <a href="mailto:[email protected]">Lyn Goltz </a>
*/
public class ReportWriterTest {

@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();

@Test
public void test() throws IOException {
Summary summary = new Summary();
ReportWriter reportWriter = new ReportWriter(summary, tmpFolder.newFile().toPath());

JobExecution jobExecution = mock(JobExecution.class);
StepExecution stepExecution = mock(StepExecution.class);
when(jobExecution.getStepExecutions()).thenReturn(Collections.singletonList(stepExecution));
when(stepExecution.getStartTime()).thenReturn(LocalDateTime.now().minusDays(1));
when(stepExecution.getEndTime()).thenReturn(LocalDateTime.now().minusDays(1));
when(stepExecution.getExitStatus()).thenReturn(ExitStatus.COMPLETED);

reportWriter.afterJob(jobExecution);
}

}

0 comments on commit fdacadc

Please sign in to comment.