Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: ItemRead pass parameter

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    Default ItemRead pass parameter

    Hi everybody.
    Anybody here know how can I pass the parameters to ItemRead?
    For example, I have a class that process the parameters and return a object that I use on the read() method to search on the database.

    The ItemRead class.

    Method that use JdbcTemplate to search on the database.
    Code:
    public List<CategoriaTarifaria> obterDadosCategoriaTarifa(final Integer tarifa){
    		
    		return jdbcTemplate.query("select tabnum,ctgtrfcod,ctgtrfdes from agekcateg where tabnum = ? and ramcod = 531",
    				new Object[]{tarifa}, 
    				new RowMapper() {
    					public Object mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
    						final CategoriaTarifaria categoriaTarifaria = new CategoriaTarifaria();
    						categoriaTarifaria.setCodigoCategoriaTarifaria(resultSet.getInt("ctgtrfcod"));
    				    	categoriaTarifaria.setDescricaoCategoriaTarifaria(resultSet.getString("ctgtrfdes"));
    				    	return categoriaTarifaria;
    					}
    				});
    	}
    Method that receives the parameters and calls the JdbcTemplate:
    Code:
    public CategoriaTarifaria read() throws Exception,
    			UnexpectedInputException, ParseException {
    		CategoriaTarifaria categoriaTarifaria = null;
    		
    		VigenciaTarifaria vigenciaTarifaria = gerenciaTarifaria.gerenciarVigenciaTarifaria(nomeTabelaTarifaria, nomeTabela, dataVigencia);
    		
    		if(categoriaTarifas == null){
    			categoriaTarifas = obterDadosCategoriaTarifa(vigenciaTarifaria.getNumeroTarifa());
    		}
    		
    		if(!categoriaTarifas.isEmpty()){
    			categoriaTarifaria = categoriaTarifas.remove(0);
    		}
    		
    		return categoriaTarifaria;
    	}

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    I don't understand the problem. Don't you just need to inject those three parameters (nomeTabelaTarifaria, nomeTabela, dataVigencia) into the reader when you configure it?

  3. #3
    Join Date
    Nov 2009
    Posts
    14

    Default

    Yeap, but I need to pass the parameters in runtime, I can't configure this in .xml, I saw many examples that you configure the parameters in .xml but I need to pass the parameters when I call it.

  4. #4
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    What do you mean by "when I call it"? Doesn't the normal late binding work? You could pass them in as JobParameters.

  5. #5
    Join Date
    Nov 2009
    Posts
    14

    Default

    I meant when I call the method read() of the ItemRead class.

    My main class:
    Code:
    public static void main(String[] args) {
    		// Leitura do arquivo de configuração do Spring
    		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:/config/applicationService.xml");
    		// Obtendo o Bean que contêm o job
    		Job appJob = (Job) appContext.getBean("categoriaTarifariaJob");		
    		// Obtendo o Bean executor do job
    		JobLauncher jobLauncher = (JobLauncher) appContext.getBean("jobLauncher");
    		
    		try {
    			long tempoInicial = System.currentTimeMillis(); 
    			logger.info("Inicio do processamento batch - CategoriaTarifaria");			
    			// Executando o job
    			jobLauncher.run(appJob, new JobParameters());			
    			
    			logger.info( "Fim do processamento batch - CategoriaTarifaria. " +
    						 "Tempo de execução (segundos): " + (System.currentTimeMillis() - tempoInicial) / 1000 );			
    			// Saída do batch com sucesso.
    			System.exit(0);
    		} catch (JobExecutionAlreadyRunningException e) {.......
    Application.xml
    Code:
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    		<property name="transactionManager" ref="atomikosTransactionManager" />
    		<property name="userTransaction" ref="atomikosUserTransaction" />
            <property name="allowCustomIsolationLevels" value="true" />
    	</bean>
    	
    	<!-- ############################## Configuracao do Batch ########################-->
    	<bean id="jobRepositoryTransactionManager"
    		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    		
    	<bean id="jobRepository"
    		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    		<property name="transactionManager" ref="jobRepositoryTransactionManager" />
    	</bean>
    
    	<bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<property name="jobRepository" ref="jobRepository" />
    	</bean>
    In JobParameters Can I pass the parameters to read() method?

  6. #6
    Join Date
    Jun 2005
    Posts
    4,230

  7. #7
    Join Date
    Nov 2009
    Posts
    14

    Default

    Thanks for your reply, but I think you don't understand my question because I 'm not using flatfile, I tried to find examples of use the jobParameters for methods but I found nothing. I need to pass the parameters to a method into the read method.

  8. #8
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    It's quite possible I don't understand your question, but late binding is not restricted to use with flat files (wouldn't be much use if it was). Maybe you don't understand the documentation (which I have to say I thought was fine, but if it's not we can fix it). Can you explain how you would solve your problem, if you were just loading your data manually from code you wrote yourself? Maybe that would explain the gap that we haven't really understood yet? Just use pseudo code.

  9. #9
    Join Date
    Nov 2009
    Posts
    14

    Default

    I think I'm on the right way, my test class now is:
    Code:
    // Get the Bean with job
    	Job appJob = (Job) appContext.getBean("categoriaTarifariaJob");
             // Get the Bean that execute the job
    	JobLauncher jobLauncher = (JobLauncher) appContext.getBean("jobLauncher");
    		
    	Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
    	parameters.put("key1", new JobParameter("apokvclvrs"));
    	parameters.put("key2", new JobParameter("anatidade"));
    	parameters.put("key3", new JobParameter(DateUtils.stringToDate("07/12/2009", "dd/MM/yyyy")));
    		
    	JobParameters jobParameters = new JobParameters(parameters);
    try {
    	long tempoInicial = System.currentTimeMillis(); 
    	logger.info("Inicio do processamento batch - CategoriaTarifaria");			
    	// Executando o job
    	jobLauncher.run(appJob, jobParameters);			
    			
    	logger.info( "Fim do processamento batch - CategoriaTarifaria. " +
    	 "Tempo de execução (segundos): " + (System.currentTimeMillis() - tempoInicial) / 1000 );			
    	// Saída do batch com sucesso.
    	System.exit(0);
    	} catch (JobExecutionAlreadyRunningException e) ......{
    But I don't know how I can get the parameters in the class ItemReader. I tried to set the parameters on the application.xml.

    Code:
    <bean id="categoriaTarifariaReader" class="com.porto.portoprint.automovel.batch.steps.CategoriaTarifariaItemReader">
    	<property name="jdbcTemplate" ref="jdbcTemplate" />
    	<property name="nameTable" value="#{jobParameters[key1]}" />
    </bean>
    But the container make the dependency injection before calling the jobLauncher.run() method.

  10. #10
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Your reader has to be scope="step" (like it says in the documentation link I posted earlier).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •