AdjustingForInflation
AdjustingForInflation

Reputation: 1611

Java concurrency & cyclic task execution

I have a task that consists of 20 different steps:

public class MyTask implements Runnable {
    @Override
    public void run() {
        // Step #1. Fetch data
        Data data = fetchData();

        // Step #2. Do something with data
        SomeResult result = dataProcessor.process(data);

        // ...

        // Step #19. Generate report
        Report report = reportFactory.newReport(result);

        // Step #20. Clean up
        cleanUp();
    }
}

I want to:

For example, let's say the app starts up, and STARTING_POINT is 2/4/2014 11:15:36 EST. Let's say the 1st cycle takes exactly 1 minute to finish executing all 20 steps inside run(). It would then execute between 11:15:36 and 11:16:36, and then the 2nd cycle would start over again and call run(). Let's say this cycle was slow and by the time we get to 11:17:36 it's only on Step #11. It would shut down and stop executing, and then a 3rd cycle would start over again and call run(). If this cycle was fast and was all done with run() by 11:17:53, then STARTING_POINT would become 2/4/2014 11:17:53, and a 4th cycle would start executing and call run() again. Etc.

I have absolutely no idea where to even begin digging in with this. Any ideas? I'm open to the use of anything that ships with the JDK, and any open source libraries besides Apache Camel.

Upvotes: 0

Views: 803

Answers (2)

user3276097
user3276097

Reputation: 26

cancel() would cancel the current thread. can you check for time after each of your 20 methods and call cancel.

time2.scheduleAtFixedRate(new TimerTask() {
        long t0 = System.currentTimeMillis();
        @Override
        public void run() {
          if (System.currentTimeMillis() - t0 ) {
              cancel();
          } else {
              sendSamples();
          }
        }

Upvotes: 0

aNish
aNish

Reputation: 1077

Looks like Spring Batch would be a good one to consider. I would recommend it.

You can schedule the jobs with spring integrated quartz scheduler. A job will be split into multiple steps (and parallelism can also be achieved). Jobs could be re-started and best of all everything is right there that you have minimal work.

Most of the stuff goes in wiring it properly. I provided a bean configuration as example.

    <bean id="threadPoolExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="20" />
    <property name="maxPoolSize" value="25" />
    <property name="queueCapacity" value="500" />
</bean>
<bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="asyncTaskExecutor" />
</bean>

<batch:job id="batchjob" job-repository="jobRepository">
    <batch:step id="step1" next="step2">
        <batch:tasklet ref="start" />
    </batch:step>
    <batch:step id="step2" next="step3.master">
        <batch:tasklet ref="fetchData" />
    </batch:step>
    <batch:step id="step3.master">
        <batch:partition step="step3.slave"
            partitioner="processingPartitioner">
            <batch:handler grid-size="20" task-executor="threadPoolExecutor" />
        </batch:partition>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="MyListener" />
    </batch:listeners>
</batch:job>

<batch:step id="step3.slave">
    <batch:tasklet ref="processFile" />
</batch:step>

<bean id="myLauncher" class="com.<>.batch.MyLauncher">
    <property name="job" ref="batchJob" />
    <property name="jobLauncher" ref="jobLauncher" />
</bean>

<task:scheduled-tasks>
    <task:scheduled ref="myLauncher" method="launch"
        cron="0 0 0 0 0 1" />
</task:scheduled-tasks>

Each step could be defined and should implement Tasklet. There are examples provided by spring itself.

Upvotes: 1

Related Questions