spring batch threads not returning after execution of main thread is complete

╄→尐↘猪︶ㄣ 提交于 2020-01-13 06:19:08

问题


I am novice to spring batch. I have created and successfully executed job from spring by using multiple threads and it works perfetly fine except that when program execution is complete, program flow does not comes to end/halt. i.e. even when the last statement of main method gets executed program does not exit. I am not sure whether it keeps on waiting for threads to complete or what. Can someone please advice on this ? "Below is my config file for job

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
            <property name="corePoolSize" value="5" />
            <property name="maxPoolSize" value="5" />
</bean>

<batch:job id="helloWorldJob" job-repository="jobRepository">
       <batch:step id="step0" next="step1">
                <batch:tasklet ref="hello" transaction-manager="transactionManager" task-executor="taskExecutor" />
       </batch:step>        
       <batch:step id="step1">
                <batch:tasklet ref="world" transaction-manager="transactionManager" />
       </batch:step>
</batch:job>  

And below is the launcher code

public static void main(String args[]) {
        try {
            new ClassPathXmlApplicationContext("simpleJob.xml");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addString("Date", "12/02/2011");
            jobLauncher.run(job, builder.toJobParameters());

            JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());

            System.out.println("\n\n"+jobExecution.toString());
            Thread.sleep(5000);
            System.out.println("End of execution ");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

As stated above, the code runs in 5 different thread for the task "hello" and once for task "world" though it does not bring the execution of main program to halt even after the last line of main method "End of execution" gets executed. Any link white paper will greatly be appriciated. Thanks in advance Samir


回答1:


Your thread pool is not being shutdown. Probably the best way to do that is to call close() on your application context (in a finally block to be safe).



来源:https://stackoverflow.com/questions/13699516/spring-batch-threads-not-returning-after-execution-of-main-thread-is-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!