Announcement Announcement Module
Collapse
No announcement yet.
ItemRead pass parameter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment


    • #3
      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.

      Comment


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

        Comment


        • #5
          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?

          Comment


          • #6
            http://static.springsource.org/sprin...l#late-binding

            Comment


            • #7
              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.

              Comment


              • #8
                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.

                Comment


                • #9
                  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.

                  Comment


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

                    Comment


                    • #11
                      Sorry, I'm newbie in SpringBatch and I hadn't watched in the documentation, but thanks for your help, I couldn't finish my job without your help.

                      Comment

                      Working...
                      X