Results 1 to 6 of 6

Thread: How to create a common recordset to be used while job running

  1. #1

    Default How to create a common recordset to be used while job running

    Hi all,

    What I need to do is that before running the batch I have to run multiple queries and store them in a Map so that while checking business logic in 'itemProcessor' I can validate input records with the help of Map values.
    So my config xml may look like below. But what I am stuck with is that how can I make the Map created in 'parentStep' available in 'process' class?

    Please suggest.


    Code:
    <job id="counterJob">
    <step id="parentStep">
    <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
    </tasklet>
    </step>
    		<step id="fetchRecord" parent="parentStep">
    			<tasklet>
    				<chunk reader="inputRecords" processor="process" writer="opWriter" commit-interval="5">					
    				</chunk>
    				
    			</tasklet>
    		</step>				
    	</job>

  2. #2
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @kousik.majumder,

    Take a look at "StepExecutionListener": http://static.springsource.org/sprin...cutionListener where you can populate your map in "@BeforeStep"

    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  3. #3

    Default Sample code

    Hi,

    How that Map can be available to the whole job? How can I use this map anywhere in batch processing.
    Also I need to populate the Map after reading from database. I am NOT getting idea how to do that.
    I am a newbie and there is not much sites for reference so please give some code. That wil be very helpful to me.
    Last edited by kousik.majumder; Oct 7th, 2010 at 01:49 AM.

  4. #4
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @kousik.majumder,

    You can use a "JobExecutionListener", and populate your map in its "beforeJob" method. After that this map will be available for the whole duration of the job, and can be simply injected into your beans as:

    Code:
    <bean id="itemProcessor" scope="step" class="org.springframework.batch.item.....ItemProcessor">
        <property name="magicMap" value="#{jobExecutionContext['magicMap']}" />
    </bean>
    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  5. #5

    Default

    @ litius,

    I am getting error.Seems can not bind that property holder value. How to fix this?
    How to make my code jobExecutionContext scoped. The problem will be solved if I use StepExecutionListener and set accordingly but I want to use JobExecutionListener to set the Map malue available in Job level.
    My code and error are given below:

    Error StackTrace:-

    n failed; nested exception is java.lang.IllegalStateException: Cannot bind to placeholder: jobExecutionContext['mapPr
    oductExclDto']
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'lazyBindingProxy.process#sysi
    nit' defined in class path resource [counterJob.xml]: Initialization of bean failed; nested exception is java.lang.Il
    legalStateException: Cannot bind to placeholder: jobExecutionContext['mapProductExclDto']
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towire
    CapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apable
    BeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCa
    pableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$2.getObject(AbstractBeanFactory.java:3 02)
    at org.springframework.batch.core.scope.StepScope.get (StepScope.java:120)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:298 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.batch.core.scope.util.Placehol derTargetSource.getTarget(PlaceholderTargetSource. java:2
    05)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:184)
    at $Proxy3.process(Unknown Source)



    Code:
    <job id="counterJob">
    		<step id="fetchRecord" >
    			<tasklet>
    				<chunk reader="inputMemberSource" processor="process" writer="opWriter" commit-interval="5">					
    				</chunk>
    				<listeners>
    	            	<listener ref="stepListener"/>
    	        	</listeners>
    				
    			</tasklet>
    		</step>				
    	</job>
    	
    	<beans:bean id="process" scope="step" class="com.test.MemberDetailsProcessor">
    		<beans:property name="mapProductExclDto" value="#{jobExecutionContext['mapProductExclDto']}" />
    	</beans:bean>
    	
    	<beans:bean id="stepListener" class="com.test.LoadMapListener">
    		<beans:property name="dataSource" ref="dataSource" />		
    	</beans:bean>
    LoadMapListener.java:-

    Code:
    public class LoadMapListener extends SimpleJdbcDaoSupport implements JobExecutionListener {
    
    	private static String GENERAl_EXCLUSION_LIST = "SELECT * FROM XX";
    	
    	Map<String,ProductExclDto> mapProductExclDto = new HashMap<String,ProductExclDto>();
    	
    	public void afterJob(JobExecution arg0) {
    		// TODO Auto-generated method stub
    	}
    
    	public void beforeJob(JobExecution jobExec) {
    		@SuppressWarnings("unchecked")
    		List<ProductExclDto> listOfProductExclDto = (List<ProductExclDto>) getJdbcTemplate().query(GENERAl_EXCLUSION_LIST, new RowMapper(){
    
    			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    				ProductExclDto prodExclDto = new ProductExclDto();
    				prodExclDto.setFamilyId(rs.getString("ID"));
    				prodExclDto.setIneligibilityCode(rs.getString("XX1"));
    				prodExclDto.setProdId(rs.getString("XX2"));
    				prodExclDto.setProdType(rs.getString("XX3"));
    				mapProductExclDto.put(prodExclDto.getProdId(), prodExclDto);
    				return prodExclDto;
    			}			
    		});
    		for(ProductExclDto productExcl : listOfProductExclDto)
    		{
    			mapProductExclDto.put(productExcl.getProdId(), productExcl);
    		}
    		jobExec.getExecutionContext().put("mapProductExclDto", mapProductExclDto);
    		
    	}
    
    }
    processor class (MemberDetailsProcessor.java) :-
    Code:
    public class MemberDetailsProcessor implements ItemProcessor<MembersDetails,MembersDetails>{
    
    	private Map<String,ProductExclDto> mapProductExclDto;
    	
    	public void setMapProductExclDto(Map<String, ProductExclDto> mapProductExclDto) {
    		this.mapProductExclDto = mapProductExclDto;
    	}
    
    	public MembersDetails process(MembersDetails arg0) throws Exception {
    		System.out.println("MemberDetailsProcessor Member ID:-"+arg0.getMemberId());
    		for(Map.Entry<String,ProductExclDto> mapOfExclList : mapProductExclDto.entrySet())
    		{
    			System.out.println("MAP Values ..."+mapOfExclList.getKey() + "---K/V---"+mapOfExclList.getValue());
    		}
    		return ...;
    	}
    
    	
    }
    Last edited by kousik.majumder; Oct 7th, 2010 at 05:10 AM.

  6. #6
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @kousik.majumder,

    This is a job listener, not a step listener, and you are defining it on a step level, whereas you should define it on a job level:

    Code:
    	<job id="yourJob">
    
                <step>...</step>
                <step>...</step>
                <step>...</step>
    
    	    <listeners>
    	        <listener ref="yourJobListener"/>
    	    </listeners>
    
    	</job>
    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

Posting Permissions

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