2015-01-09 5 views
5

작업 매개 변수를 사용자 정의 ItemReader에 주입하려고합니다. 제목 (예 : How to get access to job parameters from ItemReader, in Spring Batch?)에 대한 StackOverflow 노트를 모두 검토했으며, 이것이 대부분 해결되지 않은 공통된 문제점이라고 생각합니다. 나는 봄의 구루 (@Michael Minella 누구)가 이것을보고 약간의 통찰력을 가지기를 바라고있다.스프링 배치의 JobParameters

필자는 코드 나 구성 변경 없이도 작업 실행 수가 10 회 중 1 회 정도 사용할 수 있다고 판단했습니다. 이것은 임의의 실패가 아닌 임의의 성공의 경우이므로 추적하기가 어렵습니다.

디버거를 사용하여 스프링 코드를 파고 이것이 실패 할 때 주입이 발생하는 시점에 jobParameters라는 이름의 빈이 Spring에 등록되지 않았 음을 확인했습니다.

나는 자바에서 실행, 스프링 배치 3.0.2 및 스프링 데이터 JPA 1.7.1 및 스프링 데이터 공유지 1.9.1로 8

자바 클래스

을 봄 4.1.4를 사용하고 있습니다
@Component("sourceSelectionReader") 
@Scope("step") 
public class SourceSelectionReaderImpl 
implements ItemReader<MyThing> { 
    private Map<String,Object> jobParameters; 

// ... snip ... 


    @Autowired 
    @Lazy 
    @Qualifier(value="#{jobParameters}") 
    public void setJobParameters(Map<String, Object> jobParameters) { 
     this.jobParameters = jobParameters; 
    } 
} 

작업 시작 매개 변수 :

launch-context.xml job1 jobid(long)=1 

출시-context.xml에 (마이너스 보풀) :

<context:property-placeholder location="classpath:batch.properties" /> 

<context:component-scan base-package="com.maxis.maximo.ilm" /> 

<jdbc:initialize-database data-source="myDataSource" enabled="false"> 
    <jdbc:script location="${batch.schema.script}" /> 
</jdbc:initialize-database> 

<batch:job-repository id="jobRepository" 
    data-source="myDataSource" 
    transaction-manager="transactionManager" 
    isolation-level-for-create="DEFAULT" 
    max-varchar-length="1000"/> 

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

모듈-context.xml에 (마이너스 보풀) :

<description>Example job to get you started. It provides a skeleton for a typical batch application.</description> 

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

<context:component-scan base-package="com.me" /> 
<bean class="org.springframework.batch.core.scope.StepScope" /> 

<batch:job id="job1"> 
    <batch:step id="step0002" >    
     <batch:tasklet transaction-manager="transactionManager" start-limit="100" > 
      <batch:chunk reader="sourceSelectionReader" writer="selectedDataWriter" commit-interval="1" /> 
     </batch:tasklet> 
    </batch:step> 
</batch:job> 
+0

어떻게 작업을 시작합니까? – wassgren

+0

그리고 왜 주사에'@ Lazy'를 사용하고 있습니까? 그것은 당신의 작업에 필요한 것입니까? – wassgren

+0

일식 IDE 내에서 작업을 시작합니다. 실행시 명령 줄에서 시작됩니다. @Lazy를 사용하면이 문제를 회피하고 실제 프로젝트를 진행하면서 해결할 수 있기 때문에 사용할 수 있습니다. 빈에 매개 변수를 가져 오는 다른 방법이 있지만 장기적으로 지원 문제가 발생합니다. –

답변

2

중요한 단계를 @DependsOn ("jobParameters")를 추가보십시오 작업은 StepScope 빈을 정의하고 독자가 @StepScope 구성 요소인지 확인하는 것입니다.

나는 다음을 시도합니다 :

먼저 정의 된 단계 콩이 있는지 확인하십시오. 이것은 자바 구성을 사용하여 설정에 좋은 :

@Configuration 
public class JobFrameworkConfig { 
    @Bean 
    public static StepScope scope() { 
     return new StepScope(); 
    } 
    // jobRegistry, transactionManager etc... 
} 

그런 다음, 당신의 콩이 (거의 당신의 예에서와 같이)를 @StepScope -annotation의 사용에 의해 스텝 범위가 있는지 확인하십시오. @Value@Lazy이 아닙니다.

@Component("sourceSelectionReader") 
@StepScope // required, also works with @Scope("step") 
public class SourceSelectionReaderImpl implements ItemReader<MyThing> { 
    private final long myParam; 

    // Not lazy, specified param name for the jobParameters 
    @Autowired 
    public SourceSelectionReaderImpl(@Value("#{jobParameters['myParam']}") final long myParam) { 
     this.myParam = myParam; 
    } 

    // the rest of the reader... 
} 
+0

- Bean이 초기화되지만 단계 범위의 작업 매개 변수를 사용할 수 없습니다. @StepScope를 사용 는, 빈 초기화 실패 최종 클래스 클래스 com.sun.proxy를 서브 클래 싱 할 수 없습니다 $ Proxy42 \t org.springframework.cglib.proxy.Enhancer.generateClass (Enhancer.java:446)에서 ....etc. –

+0

@ pojo-guy, 이상한 실패. 'StepScope'라는 클래스와'@ StepScope'이라는 주석이 있음을 관찰하십시오. 불편 함;) – wassgren

+0

다른 스레드에서 StepScope를 사용한 우발적 인 프록 싱이 논의되었습니다. Per @Mike Manella (Spring Batch 기술 선도) StepScope는 Scope ("step", ...)의 바로 가기입니다. 그 해결 방법은 스코프 주석을 올바른 매개 변수로 이해하고 적용하는 것입니다. 기회가 생겼을 때이를 적용하고 결과를보고 할 것입니다. –

-1

은 @Component 후 ("sourceSelectionReader") 작업 매개 변수를 얻을 수

+0

이것은 jobParameters라는 bean이 없음을 확인합니다. @Scope ("step")을 사용하여 감사합니다 –

관련 문제