2016-07-01 3 views
0

나는 을 Spring Batch 3.0.6.RELEASE과 함께 사용하고 있으며, 스프링 배치/스프링 부트가 이 아니고이 자동 시작되도록하는 데 문제가 있습니다.스프링 배치/스프링 부트는 항상 내 작업 자동 시작

이 질문은 how to stop spring batch scheduled jobs from running at first time when executing the code?

과 유사 나는 다음과 같은 테스트 클래스가 : 나는 설정 파일 GetMarkerViaSpringBatchApplicationTest-context.xml이 테스트 클래스와 같은 디렉토리 (패키지)에서

@RunWith(SpringJUnit4ClassRunner.class) 
@Configuration 
@ComponentScan(useDefaultFilters = false, 
    includeFilters = @ComponentScan.Filter(value = GetMarkerViaSpringBatchApplicationTest.class, 
     type = FilterType.ASSIGNABLE_TYPE)) 
@EnableBatchProcessing 
//@IntegrationTest({"spring.batch.job.enabled=false"}) 
@EnableAutoConfiguration 
@TestPropertySource(
    locations = {"classpath:env.properties", "classpath:application.properties", "classpath:database.properties"}, 
    properties = {"spring.batch.job.enabled=false"}) 
@SpringApplicationConfiguration(classes = {GetMarkerViaSpringBatchApplicationTest.class}, 
    locations = {"classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml"}) 
public class GetMarkerViaSpringBatchApplicationTest { 
    @Autowired 
    private JobLauncherTestUtils jobLauncherTestUtils; 

    @Test 
    public void testLaunchJob() throws Exception { 
    JobExecution jobExecution = jobLauncherTestUtils.launchJob(); 
    assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED); 
    } 
} 

합니다.

GetMarkerViaSpringBatchApplicationTest-context.xml 내용 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <import resource="classpath:/META-INF/spring/get-marker-job-new.xml" /> 

    <bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils" /> 

    <bean id="batchConfigurer" class="org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer"> 
     <constructor-arg name="dataSource" ref="hsqlDataSource" /> 
    </bean> 
</beans> 

get-marker-job-new.xml 내용 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:file="http://www.springframework.org/schema/integration/file" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <import resource="classpath:META-INF/spring/application-context.xml" /> 
    <import resource="classpath:META-INF/spring/database.xml" /> 

    <!-- The @EnableBatchProcessing on the main class sets up: job-repository, jobLauncher, and jobRepository --> 
    <!-- Spring Boot with Spring Batch doesn't like multiple DataSources since it doesn't know which one --> 
    <!-- to pick for Spring Batch. This class will therefore coerce to pick the proper HSQL dataSource. --> 
    <!-- See https://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch --> 
    <bean id="batchConfigurer" class="org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer"> 
     <constructor-arg name="dataSource" ref="hsqlDataSource" /> 
    </bean> 

    <bean id="foo" class="myClass" /> 

    <bean id="myTasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter"> 
     <property name="targetObject" ref="myClass" /> 
     <property name="targetMethod" value="deliver" /> 
    </bean> 

    <batch:job id="batchJob" restartable="false"> 
     <batch:step id="step1"> 
      <batch:tasklet ref="myTasklet"/> 
     </batch:step> 
    </batch:job> 
</beans>  

application-context.xml 내용 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 
    xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd 
     http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <import resource="classpath:META-INF/spring/database.xml" /> 

    <!-- load properties from config files --> 
    <context:property-placeholder location="classpath:env.properties,classpath:application.properties,classpath:database.properties" /> 

    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> 
</beans> 

database.xml 내용 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> 

    <bean id="myDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> 
     <property name="driverClassName" value="${jdbc.myco.driverClassName}" /> 
     <property name="username" value="${jdbc.myco.username}" /> 
     <property name="password" value="${jdbc.myco.encpassword}" /> 
     <property name="jdbcUrl" value="${jdbc.myco.url}" /> 
     <property name="maximumPoolSize" value="${jdbc.myco.pool.maxactive}" /> 
    </bean> 
    <alias alias="dataSource" name="myDataSource" /> 

    <bean id="hsqlDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" primary="true"> 
     <property name="driverClassName" value="${jdbc.hsqldb.driverClassName}" /> 
     <property name="username" value="${jdbc.hsqldb.username}" /> 
     <property name="password" value="${jdbc.hsqldb.password}" /> 
     <property name="jdbcUrl" value="${jdbc.hsqldb.url}" /> 
     <property name="maximumPoolSize" value="${jdbc.hsqldb.pool.maxactive}" /> 
    </bean> 

</beans> 

application.properties (클래스 패스에 또한 루트 디렉토리) :

spring.datasource.platform=sqlserver 
spring.profiles.active=local 
spring.batch.job.enabled=true 
spring.batch.initializer.enabled=true 
spring.batch.schema=classpath:org/springframework/batch/core/schema-hsqldb.sql 

참고 @IntegrationTest@TestPropertySource에서 application.properties에서 spring.batch.job.enabled=true하지만 거짓 그. 나는 관계없이 @IntegrationTest 내가 작업이 jobLauncherTestUtils.launchJob() 전에 쫓겨지고이 테스트 호출되는 실행 때마다 @TestPropertySourcespring.batch.job.enabled=false의 다양한 콤보 시도의 application.propertiesspring.batch.job.enabled=true을 설정할 때마다
. 실행에서
로그 : 나는 http://forum.spring.io/forum/spring-projects/batch/748038-i-do-not-want-enablebatchprocessing-to-launch-a-jobhow to stop spring batch scheduled jobs from running at first time when executing the code?을 읽은하지만 하지 자동 시작에 jobLauncher를 얻이 수없는 것

15:52:49,083 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - Before test class: context [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null]. 

15:52:49,099 (o.s.test.context.support.DependencyInjectionTestExecutionListener) - Performing dependency injection for test context [[[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [email protected]9360f, testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]]]. 

15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 

15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]] 
15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]] 
15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false' 
... 
    BatchAutoConfiguration#jobExplorer did not match 
     - @ConditionalOnMissingBean (types: o.s.batch.core.explore.JobExplorer; SearchStrategy: all) found the following [jobExplorer] (OnBeanCondition) 

    BatchAutoConfiguration#jobLauncherCommandLineRunner did not match 
     - @ConditionalOnMissingBean (types: o.s.boot.autoconfigure.batch.JobLauncherCommandLineRunner; SearchStrategy: all) found no beans (OnBeanCondition) 
     - @ConditionalOnProperty expected 'true' for properties spring.batch.job.enabled (OnPropertyCondition) 

    BatchAutoConfiguration.JpaBatchConfiguration did not match 
     - required @ConditionalOnClass classes not found: javax.persistence.EntityManagerFactory (OnClassCondition) 
... 
15:53:02,225 (o.s.test.context.cache.DefaultCacheAwareContextLoaderDelegate) - Storing ApplicationContext in cache under key [[[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]] 

15:53:02,256 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - Before test method: context [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [email protected]9360f, testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null]. 

15:53:04,669 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - After test method: context [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [email protected]9360f, testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null]. 

15:53:04,685 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - After test class: context [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null]. 

.
내 이해는 application.properties 속성이 테스트 내에서 직접 설정하려고하는 2 곳에서 재정의되어야하지만 BatchAutoConfiguration은 무시할 수 없습니다.
application.propertiesspring.batch.job.enabled=false (또는 해당 속성에 주석을다는)을 설정하고 @IntegrationTest 또는 @TestPropertySource에서 드라이브를 시도하면 예상대로 작동합니다.
재정의 값에 대한 나의 이해가 올바르지 않습니까?

도움을 주시면 감사하겠습니다.

+0

'@ ComponentScan'과'@ EnableAutoConfiguration'은 테스트 클래스에 속하지 않습니다. 그들이 아무 것도하지 않기 때문에 그것은 단지 소음 일뿐입니다. –

+0

@DaveSyer 이것은 작동합니다 ('@ EnableAutoConfiguration'에주의하십시오) : @RunWith (SpringJUnit4ClassRunner.클래스) @EnableBatchProcessing는 @IntegrationTest() { "= 거짓 spring.batch.job.enabled"} @EnableAutoConfiguration @TestPropertySource (위치 = { "클래스 경로 env.properties", "클래스 경로 database.properties"}) @SpringApplicationConfiguration (클래스 = {GetMarkerViaSpringBatchApplicationTest.class}, 위치 = { "클래스 경로 : **/GetMarkerViaSpringBatchApplicationTest-context.xml에"}) 공용 클래스가 GetMarkerViaSpringBatchApplicationTest가 { –

+0

@DaveSyer 이것은 (일에는'@ EnableAutoConfiguration'을 통지하지 않습니다) "nobody라는 이름의 bean이 없습니다."error : @RunWith (SpringJUnit4ClassRunner.class) @EnableBatchProces @SpringApplicationConfiguration (classes = { "classpath : env.properties", "classpath : database.properties"} @ GetMarkerViaSpringBatchApplicationTest.class}, 위치 = { "클래스 경로 : **/GetMarkerViaSpringBatchApplicationTest-context.xml에"}) 공용 클래스 GetMarkerViaSpringBatchApplicationTest { –

답변

1

application.properties@TestPropertySource을 추가하면 다른 항목보다 우선 적용됩니다. 스프링 부트를 사용하는 경우에는 필요하지 않습니다.

+0

예, 실제로 이것이 문제인 것처럼 보입니다. '@ EnableAutoConfiguration' 제안에 대한 답장을 볼 수 있다면 도움이 될 것입니다. 내 문제는 구성 파일 용 Java 클래스와 XML 파일을 혼합하는 것과 관련이 있다고 생각합니다. –

관련 문제