Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we write a Spring Batch Job Without ItemReader and ItemWriter

In my project, I have written a Quartz scheduler with Spring Batch 2.2.

As per my requirement, I want to run a scheduler to fetch application config property to refresh the configuration cache on all the GlassFish Clusters.

So I dont need ItemWriter and ItemReader which are used to for File Read/Write operations. So can I remove ItemReader and ItemWriter from ?

The configuration of my job is mentioned below :

 <batch:job id="reportJob">
                 <batch:step id="step1">
                     <batch:tasklet>    
    <!--I want to remove ItemReader and ItemWriter as its not used -->          
                         <batch:chunk reader="ItemReader" writer="ItemWriter" 
                            commit-interval="10"> 
                        </batch:chunk> 
                    </batch:tasklet> 
                </batch:step> 

                <batch:listeners>
                    <batch:listener ref="simpleListener"/>
                </batch:listeners>
            </batch:job>

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
            <!-- Cache Refresh code is written here : JobLauncherDetails.java file -->
        <property name="jobClass" value="com.mkyong.quartz.JobLauncherDetails" />
        <property name="group" value="quartz-batch" />
        <property name="jobDataAsMap">
            <map>
                <entry key="jobName" value="reportJob" />
                <entry key="jobLocator" value-ref="jobRegistry" />
                <entry key="jobLauncher" value-ref="jobLauncher" />
                <entry key="param1" value="mkyong1" />
                <entry key="param2" value="mkyong2" />
            </map>
        </property>
    </bean>

I writing my business logic to refresh cache on JobClass JobLauncherDetails.java. So is it possible to remove ItemReader and ItemWriter ? Do we have any possible alternative way ?

like image 269
Gunjan Shah Avatar asked Mar 27 '14 07:03

Gunjan Shah


1 Answers

Use a Tasklet

<job id="reportJob">
  <step id="step1">
    <tasklet ref="MyTaskletBean" />
  </step>
  <!-- Other config... -->
</job> 



class MyTasklet implements Tasklet {
  @Override
  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
  }
}

You can read more on Tasklet at chapter 5.2 from official doc

like image 191
Luca Basso Ricci Avatar answered Oct 02 '22 15:10

Luca Basso Ricci