在主线程执行完成后

在主线程执行完成后

本文介绍了在主线程执行完成后,弹簧批处理线程不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是弹簧批的新手.我已经通过使用多个线程从spring创建并成功执行了作业,并且工作正常,只是在程序执行完成时,程序流不会结束/停止.即,即使main方法的最后一条语句执行完毕,程序也不会退出.我不确定它是否继续等待线程完成或执行什么操作.有人可以对此提出建议吗?下面是我的作业配置文件

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>

下面是启动器代码

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();
        }
    }

如上所述,代码在任务"hello"和任务"world"的5个不同线程中运行,尽管即使在主方法"End of执行".任何链接白皮书将不胜枚举.提前致谢萨米尔

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 advanceSamir

推荐答案

您的线程池未关闭.可能最好的方法是在应用程序上下文中调用close()(为了安全起见,在finally块中).

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).

这篇关于在主线程执行完成后,弹簧批处理线程不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:14