diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java index f9a58d9afa7..1ae2d210434 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java @@ -8,7 +8,7 @@ import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.api.dao.IDao; import ca.uhn.fhir.jpa.batch.BatchJobsConfig; import ca.uhn.fhir.jpa.batch.api.IBatchJobSubmitter; -import ca.uhn.fhir.jpa.batch.config.InMemoryJobRepositoryBatchConfig; +import ca.uhn.fhir.jpa.batch.config.NonPersistedBatchConfigurer; import ca.uhn.fhir.jpa.batch.svc.BatchJobSubmitterImpl; import ca.uhn.fhir.jpa.binstore.BinaryAccessProvider; import ca.uhn.fhir.jpa.binstore.BinaryStorageInterceptor; @@ -28,8 +28,8 @@ import ca.uhn.fhir.jpa.model.sched.ISchedulerService; import ca.uhn.fhir.jpa.packages.IHapiPackageCacheManager; import ca.uhn.fhir.jpa.packages.IPackageInstallerSvc; import ca.uhn.fhir.jpa.packages.JpaPackageCache; -import ca.uhn.fhir.jpa.packages.PackageInstallerSvcImpl; import ca.uhn.fhir.jpa.packages.NpmJpaValidationSupport; +import ca.uhn.fhir.jpa.packages.PackageInstallerSvcImpl; import ca.uhn.fhir.jpa.partition.IPartitionLookupSvc; import ca.uhn.fhir.jpa.partition.IRequestPartitionHelperSvc; import ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl; @@ -63,6 +63,8 @@ import org.hibernate.jpa.HibernatePersistenceProvider; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.utilities.cache.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.graphql.IGraphQLStorageServices; +import org.springframework.batch.core.configuration.annotation.BatchConfigurer; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -119,8 +121,9 @@ import java.util.Date; @ComponentScan.Filter(type = FilterType.REGEX, pattern = "ca.uhn.fhir.jpa.batch.*") }) @Import({ - SearchParamConfig.class, BatchJobsConfig.class, InMemoryJobRepositoryBatchConfig.class + SearchParamConfig.class, BatchJobsConfig.class }) +@EnableBatchProcessing public abstract class BaseConfig { public static final String JPA_VALIDATION_SUPPORT_CHAIN = "myJpaValidationSupportChain"; @@ -138,6 +141,11 @@ public abstract class BaseConfig { @Autowired private DaoRegistry myDaoRegistry; + @Bean + public BatchConfigurer batchConfigurer() { + return new NonPersistedBatchConfigurer(); + } + @Bean("myDaoRegistry") public DaoRegistry daoRegistry() { return new DaoRegistry(); diff --git a/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/InMemoryJobRepositoryBatchConfig.java b/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/InMemoryJobRepositoryBatchConfig.java index 2c3d5847b66..ef8b7f2ee1a 100644 --- a/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/InMemoryJobRepositoryBatchConfig.java +++ b/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/InMemoryJobRepositoryBatchConfig.java @@ -21,7 +21,6 @@ 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.explore.JobExplorer; import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean; import org.springframework.batch.core.launch.JobLauncher; @@ -30,11 +29,12 @@ 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.context.annotation.Configuration; +import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; import javax.annotation.PostConstruct; -@EnableBatchProcessing +@Component @Configuration public class InMemoryJobRepositoryBatchConfig implements BatchConfigurer { @@ -43,6 +43,7 @@ public class InMemoryJobRepositoryBatchConfig implements BatchConfigurer { private JobRepository myJobRepository; private JobExplorer myJobExplorer; + @Override public PlatformTransactionManager getTransactionManager() { return myPlatformTransactionManager; diff --git a/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/NonPersistedBatchConfigurer.java b/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/NonPersistedBatchConfigurer.java new file mode 100644 index 00000000000..b57e0d804c9 --- /dev/null +++ b/hapi-fhir-jpaserver-batch/src/main/java/ca/uhn/fhir/jpa/batch/config/NonPersistedBatchConfigurer.java @@ -0,0 +1,60 @@ +package ca.uhn.fhir.jpa.batch.config; + +/*- + * #%L + * HAPI FHIR JPA Server - Batch Task Processor + * %% + * Copyright (C) 2014 - 2020 University Health Network + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; +import org.springframework.batch.core.explore.JobExplorer; +import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean; +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.transaction.PlatformTransactionManager; + + +public class NonPersistedBatchConfigurer extends DefaultBatchConfigurer { + + private PlatformTransactionManager myPlatformTransactionManager; + private MapJobRepositoryFactoryBean myJobRepositoryFactory; + + @Override + public PlatformTransactionManager getTransactionManager() { + if (myPlatformTransactionManager == null) { + myPlatformTransactionManager = new ResourcelessTransactionManager(); + } + return myPlatformTransactionManager; + } + + @Override + protected JobRepository createJobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + factory.setTransactionManager(this.getTransactionManager()); + factory.afterPropertiesSet(); + myJobRepositoryFactory = factory; + return factory.getObject(); + } + + @Override + public JobExplorer createJobExplorer() throws Exception { + MapJobExplorerFactoryBean jobExplorerFactoryBean = new MapJobExplorerFactoryBean(myJobRepositoryFactory); + jobExplorerFactoryBean.afterPropertiesSet(); + return jobExplorerFactoryBean.getObject(); + } +} diff --git a/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/BaseBatchR4Test.java b/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/BaseBatchR4Test.java index 6a526e69fd0..98b955592bc 100644 --- a/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/BaseBatchR4Test.java +++ b/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/BaseBatchR4Test.java @@ -1,7 +1,7 @@ package ca.uhn.fhir.jpa.batch; import ca.uhn.fhir.jpa.batch.config.BatchJobConfig; -import ca.uhn.fhir.jpa.batch.config.InMemoryJobRepositoryBatchConfig; +import ca.uhn.fhir.jpa.batch.config.TestBatchConfig; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.springframework.batch.core.Job; @@ -14,7 +14,7 @@ import org.springframework.transaction.PlatformTransactionManager; import static org.slf4j.LoggerFactory.getLogger; @RunWith(SpringRunner.class) -@ContextConfiguration(classes = {BatchJobConfig.class, InMemoryJobRepositoryBatchConfig.class}) +@ContextConfiguration(classes = {BatchJobConfig.class, TestBatchConfig.class}) abstract public class BaseBatchR4Test { private static final Logger ourLog = getLogger(BaseBatchR4Test.class); diff --git a/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/config/TestBatchConfig.java b/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/config/TestBatchConfig.java new file mode 100644 index 00000000000..045b1b1ec44 --- /dev/null +++ b/hapi-fhir-jpaserver-batch/src/test/java/ca/uhn/fhir/jpa/batch/config/TestBatchConfig.java @@ -0,0 +1,15 @@ +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.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableBatchProcessing +public class TestBatchConfig { + @Bean + public BatchConfigurer batchConfigurer() { + return new NonPersistedBatchConfigurer(); + } +}