moving dependencies around. Get non-persisted batch working

This commit is contained in:
Tadgh 2020-05-28 12:10:52 -07:00
parent 81d5ff8f5a
commit a306825625
6 changed files with 119 additions and 30 deletions

View File

@ -146,6 +146,11 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-jpaserver-batch</artifactId>
</dependency>
<dependency> <dependency>
<groupId>net.ttddyy</groupId> <groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId> <artifactId>datasource-proxy</artifactId>

View File

@ -43,17 +43,17 @@
<version>4.2.2.RELEASE</version> <version>4.2.2.RELEASE</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>${project.version}</version>
</dependency>
<!-- test -->
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId> <artifactId>java-hamcrest</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-jpaserver-base</artifactId>
<version>${project.version}</version>
</dependency>
<!-- test -->
<dependency> <dependency>
<groupId>ca.uhn.hapi.fhir</groupId> <groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-jpaserver-test-utilities</artifactId> <artifactId>hapi-fhir-jpaserver-test-utilities</artifactId>

View File

@ -2,7 +2,8 @@ package ca.uhn.fhir.jpa.batch;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.batch.config.BaseTestBatchConfig; import ca.uhn.fhir.jpa.batch.config.BatchJobConfig;
import ca.uhn.fhir.jpa.batch.config.InMemoryJobRepositoryBatchConfig;
import ca.uhn.fhir.jpa.batch.svc.DummyService; import ca.uhn.fhir.jpa.batch.svc.DummyService;
import ca.uhn.fhir.jpa.config.TestJpaR4Config; import ca.uhn.fhir.jpa.config.TestJpaR4Config;
import ca.uhn.fhir.jpa.dao.index.IdHelperService; import ca.uhn.fhir.jpa.dao.index.IdHelperService;
@ -14,18 +15,15 @@ import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = {BaseTestBatchConfig.class, TestJpaR4Config.class}) @ContextConfiguration(classes = {BatchJobConfig.class, InMemoryJobRepositoryBatchConfig.class, TestJpaR4Config.class})
abstract public class BaseBatchR4Test extends BaseJpaR4Test { abstract public class BaseBatchR4Test extends BaseJpaR4Test {
private static final Logger ourLog = getLogger(BaseBatchR4Test.class); private static final Logger ourLog = getLogger(BaseBatchR4Test.class);

View File

@ -1,27 +1,22 @@
package ca.uhn.fhir.jpa.batch.config; package ca.uhn.fhir.jpa.batch.config;
import ca.uhn.fhir.interceptor.api.HookParams;
import ca.uhn.fhir.jpa.batch.svc.DummyService; import ca.uhn.fhir.jpa.batch.svc.DummyService;
import ca.uhn.fhir.jpa.config.TestJpaConfig; import ca.uhn.test.concurrency.IPointcutLatch;
import ca.uhn.test.concurrency.PointcutLatch;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step; import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus; import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import java.util.List;
@Configuration @Configuration
@EnableBatchProcessing public class BatchJobConfig implements IPointcutLatch {
public class BaseTestBatchConfig extends DefaultBatchConfigurer {
@Autowired
private JpaTransactionManager myPlatformTransactionManager;
@Autowired @Autowired
private JobBuilderFactory myJobBuilderFactory; private JobBuilderFactory myJobBuilderFactory;
@ -29,11 +24,7 @@ public class BaseTestBatchConfig extends DefaultBatchConfigurer {
@Autowired @Autowired
private StepBuilderFactory myStepBuilderFactory; private StepBuilderFactory myStepBuilderFactory;
private final PointcutLatch myPointcutLatch = new PointcutLatch("batchJobLatch");
@Override
public PlatformTransactionManager getTransactionManager() {
return myPlatformTransactionManager;
}
@Bean @Bean
public DummyService myDummyService() { public DummyService myDummyService() {
@ -41,7 +32,7 @@ public class BaseTestBatchConfig extends DefaultBatchConfigurer {
} }
@Bean @Bean
public Job testJob() { public Job datJob() {
return myJobBuilderFactory.get("testJob") return myJobBuilderFactory.get("testJob")
.start(testStep()) .start(testStep())
.build(); .build();
@ -51,7 +42,23 @@ public class BaseTestBatchConfig extends DefaultBatchConfigurer {
public Step testStep() { public Step testStep() {
return myStepBuilderFactory.get("testStep").tasklet((theStepContribution, theChunkContext) -> { return myStepBuilderFactory.get("testStep").tasklet((theStepContribution, theChunkContext) -> {
System.out.println("woo!"); System.out.println("woo!");
myPointcutLatch.call(theChunkContext);
return RepeatStatus.FINISHED; return RepeatStatus.FINISHED;
}).build(); }).build();
} }
@Override
public void clear() {
myPointcutLatch.clear();
}
@Override
public void setExpectedCount(int count) {
myPointcutLatch.setExpectedCount(count);
}
@Override
public List<HookParams> awaitExpected() throws InterruptedException {
return myPointcutLatch.awaitExpected();
}
} }

View File

@ -0,0 +1,74 @@
package ca.uhn.fhir.jpa.batch.config;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
@Configuration
@EnableBatchProcessing
public class InMemoryJobRepositoryBatchConfig implements BatchConfigurer {
private PlatformTransactionManager myPlatformTransactionManager;
private JobLauncher myJobLauncher;
private JobRepository myJobRepository;
private JobExplorer myJobExplorer;
@Autowired
private JobBuilderFactory myJobBuilderFactory;
@Autowired
private StepBuilderFactory myStepBuilderFactory;
@Override
public PlatformTransactionManager getTransactionManager() {
return myPlatformTransactionManager;
}
@Override
public JobRepository getJobRepository() {
return myJobRepository;
}
@Override
public JobLauncher getJobLauncher() {
return myJobLauncher;
}
@Override
public JobExplorer getJobExplorer() {
return myJobExplorer;
}
@PostConstruct
public void setup() throws Exception{
if (myPlatformTransactionManager == null) {
myPlatformTransactionManager = new ResourcelessTransactionManager();
}
MapJobRepositoryFactoryBean jobRepositoryFactoryBean = new MapJobRepositoryFactoryBean(myPlatformTransactionManager);
jobRepositoryFactoryBean.afterPropertiesSet();
myJobRepository = jobRepositoryFactoryBean.getObject();
MapJobExplorerFactoryBean jobExplorerFactoryBean = new MapJobExplorerFactoryBean(jobRepositoryFactoryBean);
jobExplorerFactoryBean.afterPropertiesSet();
myJobExplorer = jobExplorerFactoryBean.getObject();
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(myJobRepository);
jobLauncher.afterPropertiesSet();
myJobLauncher = jobLauncher;
}
}

View File

@ -1,18 +1,23 @@
package ca.uhn.fhir.jpa.batch.svc; package ca.uhn.fhir.jpa.batch.svc;
import ca.uhn.fhir.jpa.batch.BaseBatchR4Test; import ca.uhn.fhir.jpa.batch.BaseBatchR4Test;
import ca.uhn.fhir.jpa.batch.config.BatchJobConfig;
import org.junit.Test; import org.junit.Test;
import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException; import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
public class BatchSvcTest extends BaseBatchR4Test { public class BatchSvcTest extends BaseBatchR4Test {
@Autowired
private BatchJobConfig myBatchJobConfig;
@Test @Test
public void testApplicationContextLoads() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { public void testApplicationContextLoads() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, InterruptedException {
myDummyService.test(); myBatchJobConfig.setExpectedCount(1);
myJobLauncher.run(myJob, new JobParameters()); myJobLauncher.run(myJob, new JobParameters());
myBatchJobConfig.awaitExpected();
} }
} }