Java 如何在设置并发限制后立即获取作业id

Java 如何在设置并发限制后立即获取作业id,java,spring,spring-batch,Java,Spring,Spring Batch,问题是,在我设置并发限制(例如限制为2)后,当第三个请求传入时,作业启动程序将不会返回作业执行。作业启动程序正在阻止新作业执行。换句话说,我希望通过运行runFun方法在收到请求后立即获取作业id 我尝试了SimpleAsynctaskeExecutor和SimpleThreadPoolTaskExecutor,它们都不工作。由于挂起jobLauncher.run方法,before作业侦听器也没有作业id信息。我浏览了源代码SimpleAsyncTaskExecutor.execute,Conc

问题是,在我设置并发限制(例如限制为2)后,当第三个请求传入时,作业启动程序将不会返回作业执行。作业启动程序正在阻止新作业执行。换句话说,我希望通过运行runFun方法在收到请求后立即获取作业id

我尝试了SimpleAsynctaskeExecutor和SimpleThreadPoolTaskExecutor,它们都不工作。由于挂起jobLauncher.run方法,before作业侦听器也没有作业id信息。我浏览了源代码SimpleAsyncTaskExecutor.execute,ConcurrencyThrottleSupport.beforeAccess,没有运气解决这个问题

在这种情况下,当job launcher运行执行时,如何立即获取作业id

我发布的主要代码如下:

**//Config:**

<bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository"/>
    <property name="taskExecutor">
        <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor">
            <property name="concurrencyLimit" value="2"/>
        </bean>
    </property>
</bean>

<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
    <property name="dataSource" ref="sampleservice.persist.datasource"/>
</bean>

<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
    <property name="jobExplorer" ref="jobExplorer"/>
    <property name="jobRepository" ref="jobRepository"/>
    <property name="jobLauncher" ref="jobLauncher"/>
    <property name="jobRegistry" ref="jobRegistry"/>
</bean>

<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"></bean>


**//Code:**

    Private Long runFun() {
    new JobParametersBuilder()
                    .addLong("pId", parameterDO.getPId())
                    .toJobParameters();

                try {
                    JobExecution jobExecution = jobLauncher.run(bulkDownloadJob, params);

    //the job execution id will be blocked with status "STARTING" in the batch metadata table until the job is actually started with status "STARTED"
                    return jobExecution.getId();  
    } catch ...

您正在尝试的操作与SimpleZoblancher不兼容。问题是,在使用节流时,TaskExecutor将阻塞,直到有线程可执行任务为止。问题是,在线程可以执行作业执行之前,必须有一种返回作业执行的方法


你必须做你正在尝试的信息传递。当前基于消息的组件不会执行此操作,但您可以创建一个JobLauncher,该JobLauncher创建了JobExecution并将其发送给MessageHandler执行。这将允许调用方返回JobExecution,但仍有作业等待执行。

嘿,Michael,非常感谢您提供的信息。我现在正在研究spring批处理集成。您的意思是创建一个自定义jobLauncher,它可以立即发回作业执行,然后将其发送给messageHandler执行?如果你能给我更详细的信息,我将不胜感激。关于如何实现此伪代码的详细信息就足够了。嘿,Michael,谢谢你提供的非常有用的信息。。基于这个想法,我按照上面的代码来解决这个问题。基本思想是我创建了一个新线程,用于在新的作业启动器中执行代码。如果您有任何意见,我们将不胜感激。您正在做与我建议的相同的事情,您只是手动管理线程。
//NewLauncherClass
public JobExecution run(final Job job, final JobParameters jobParameters) {

    ... 

    jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);

        try {
            return jobExecution;
        } finally {
            if (jobExecution != null) {
                new Thread(new Runnable() {
                    public void run() {
                        executeJob(jobExecution, job, jobParameters);
                    }
                }).start();
            } else {
                logger.error("*****");
            }
        }

    }

    //added this method for executing the job
    private void executeJob(final JobExecution jobExecution, final Job job, final JobParameters jobParameters) {
        try {
            logger.info("***** test bulk *****  Job " + jobExecution.getId() + " is going to be executed");

            taskExecutor.execute(new Runnable() {

                ...