Announcement Announcement Module
Collapse
No announcement yet.
spring batch pulling out servicebean from stepId Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring batch pulling out servicebean from stepId

    New to Spring Batch/Spring in general
    Hi All!
    So before I run any job i want to start with clean state , so undo data from old job run. so i do this in @beforeStart in StepExecListener.
    In my stepexecListener I am pulling out serice bean associated with that step in @beforestart using component-metadata-config and call undo().
    component-metadata-config has just common java bean with each bean having serviceName property different. Now its redundant that I have all these serviceBean defined in my service-config.xml.. I want to use the same serviceBeanConfig and get rid of component-metadata-config.xml So I was wondering if there is any way to pull out all the beans associated with a particular step. I did some digging in stepExecListener, which just stepName.
    I tried 2 solution and both didnt work out. Before I get in those details let me show code and config. You could also see code @ http://pastebin.com/y4e16Akc

    Job Definition
    Code:
    <batch:job id="taskletComponentTestJob">
    		<batch:step id="taskletTestComponentService-0"
    			tasklet="taskletForTestComponent">
    			<batch:listeners>
    				<batch:listener ref="customStepExecutionListener" />
    			</batch:listeners>
    		</batch:step>
    		<batch:listeners>
    			<batch:listener ref="customJobExecutionListener" />
    		</batch:listeners>
    	</batch:job>
    The tasklet is defnied in the component-spring-config.xml
    [code]
    <bean id="taskletForTestComponent"
    class="com.mypackage.tasklet.CustomTasklet">
    <property name="service">
    <ref bean="taskletTestComponentService" />
    </property>
    </bean>
    [/code}

    Service bean definition
    Code:
    <bean id="taskletTestComponentService" class="org.springframework.aop.framework.ProxyFactoryBean" scope="singleton" lazy-init="true">
    	<property name="proxyInterfaces"><value>com.mypackage.service.CustomTaskletService</value></property>
            <property name="target">
                <bean class="com.fbfs.cp.pic.batch.comp.component.TaskletComponent" scope="singleton" lazy-init="true"/>
            </property>
    	</bean>
    Lets get to the component-meta-config for this service bean which we use pull out the serviceBeanName
    Code:
    <bean id="taskletTestComponent"
    		class="com.mypackage.helper.ComponentMetadata">
    		<property name="thresholdCheck" value="false" />
    		<property name="serviceClassName"
    			value="taskletTestComponentService" />
    </bean>
    The ComponentMeta is java bean with getter and setters:
    Code:
    public class ComponentMetadata {	
    	private boolean thresholdCheck;
    	private List<String> dependencies;
    	private String serviceClassName;
    StepExecListener
    Code:
    ublic class CustomStepListener implements StepExecutionListener {
    	private static final Logger LOGGER = Logger.getLogger(CustomStepListener.class);
    	/************** Part of 2nd solution
    	private CustomTaskletService componentService;
    	
    	public CustomTaskletService getComponentService() {
    		return componentService;
    	}
    
    	public void setComponentService(CustomTaskletService componentService) {
    		this.componentService = componentService;
    	}**********/
    
    	
    	
    	@Override
    	public void beforeStep(StepExecution stepExecution) {
    		try {
    			LOGGER.info("[ Executing Component : " + stepExecution.getStepName() + " ]");
    			LOGGER.info("[ Component Start Date/Time : " + DateUtils.getCurrentTimestamp() + " ]");
    			ComponentMetadata component = JobInitializer.instance().getComponentBeans().get(stepExecution.getStepName().split("-")[0]);
    			if (component == null) {
    				// Stop the job if component developer has not made an entry for
    				// component in component-metadata.xml
    				stepExecution.addFailureException(new InvalidJobConfigurationException("No entry found for '" + stepExecution.getStepName()
    						+ "' component in Component-metadata.xml, Abruptly stopping the job..."));
    				throw new InvalidJobConfigurationException("No entry found for '" + stepExecution.getStepName()
    						+ "' component in Component-metadata.xml, Abruptly stopping the job...");
    			}
    			/************Part of 1st solution
    			pulls out the stepName =	stepExecution.getStepName().split("-")[0]
    			
    
    			************/
    			// If dependency check is 'true', Check for dependency Components:
    			// Whether they have been executed
    			// or not
    			if (JobInitializer.instance().isCheckDependency()) {
    				checkDependencies(stepExecution);
    			}
    			
    			//Check if user wants to skip UNDO part of this step
    			if (!JobInitializer.instance().isSkipUndo()) {
    				// Call Undo Process of the Component: Mandatory Steps	
    ****Look AT ME**** StepService serviceClass = (StepService) CommonApplicationContext.getContext().getBean(component.getServiceClassName());
    				serviceClass.doUndoStep();
    			}
    		} catch (Exception e) {
    			LOGGER.error(ExceptionHelper.getExceptionStack(e));
    			LOGGER.debug(ExceptionHelper.getExceptionStack(e));
    			throw new RuntimeException(e);
    		}
    	}

    1st Solution :
    So if we look into the the link ***Look AT ME*** above in code we just pull out the serviceClassName from component-metadata-config and then pass it to applicatiioncontext to pull out the servicebean.
    I kept stepName-someNumber same as serviceBeanName as in my metadata-config we are just keeping serviceBeanName. So when i split the stepName and pull out the first Name, Its same name as serviceBeanName. I pass to the applicationContext hoping to get beanByName, but I get beanByType which is instance of TaskletStepImpl. And StepExecution doesnt really offer much in the , I guess till Listener completes. Will elaborate if needed.

    2nd Solution:
    I wanted to inject the serviceBeans into the listener defitinition but dropped as for 100 jobs/100 serviceBeans and 100 properties in beanDefition .

    So what could be soluion for my problem, I was thinking on foll up the first solution with somework around or customizing the listener further but what to use, So looking forward to hear from experts.

    Regards,
    Idea.
    Last edited by idea4gud; Jun 24th, 2010, 02:28 PM. Reason: to be specific on what is focus of my achievement
Working...
X