带有不同参数的春季批次运行相同的工作

发布于 2025-02-06 18:48:51 字数 533 浏览 4 评论 0原文

我的春季批处理作业在传递某些作业参数时运行。现在,我想实现的是一个超级作业,它构建了作业参数列表,并使用不同的作业参数执行此批处理作业。

我正在使用Joblauncher使用不同参数触发

作业

java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).

fun runJobs(jobParams: List<JobParameters>): List<JobExecution> =
      jobParams.map { jobParam ->
        jobLauncher.run(someJob, jobParam)
      }

批处理

I'm having a spring batch job which runs when passed certain job params. Now I wanted to achieve is a super job which constructs list of job params and executes this batch job with different job param.

I'm using jobLauncher to trigger the batch job using different params

The error am facing is

java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).

The way by which am triggering the job is

fun runJobs(jobParams: List<JobParameters>): List<JobExecution> =
      jobParams.map { jobParam ->
        jobLauncher.run(someJob, jobParam)
      }

Is there an example to trigger same job with different job params programmatically ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2025-02-13 18:48:51

此错误意味着您正在尝试在交易范围内启动作业,情况并非如此。似乎fun runjobs()@transactional或建议以另一种方式进行交易。

是否有一个示例可以通过编程方式触发不同的作业参数?

这是一个快速示例:

JobParameters jobParameters1 = new JobParametersBuilder()
        .addString("name", "foo")
        .toJobParameters();
JobParameters jobParameters2 = new JobParametersBuilder()
        .addString("name", "bar")
        .toJobParameters();
jobLauncher.run(job, jobParameters1);
jobLauncher.run(job, jobParameters2);

This error means you are trying to launch a job within the scope of a transaction, which should not be the case. It seems that fun runJobs() is annotated with @Transactional or is AOP advised to be transactional in another way.

Is there an example to trigger same job with different job params programmatically ?

Here is a quick example:

JobParameters jobParameters1 = new JobParametersBuilder()
        .addString("name", "foo")
        .toJobParameters();
JobParameters jobParameters2 = new JobParametersBuilder()
        .addString("name", "bar")
        .toJobParameters();
jobLauncher.run(job, jobParameters1);
jobLauncher.run(job, jobParameters2);
半葬歌 2025-02-13 18:48:51

我能够使用 JobStep

private fun jobSteps(jobParams: List<JobParams>): List<Step> =
      jobParams.map { jobParam ->
        jobStep(
            JobParametersBuilder()
                .addString("param1", jobParam.param1)
                .addString("param2", jobParam.param2))
      }

private fun jobStep(jobParameters: JobParameters): Step =
      stepBuilderFactory
          .get("step-{some-unique-name-to-identify-each-job}")
          .job(<Job which needs to be executed for different params>)
          .parametersExtractor { _: Job, _: StepExecution -> jobParameters } // Here we are passing parameters for each job.
          .build()

首先创建作业步骤。现在,我们将有一个步骤列表。它们是工作步骤的步骤,需要将它们连接到超级工作,并且它们独立于每个工作。

private fun superJob(steps: List<Step>): Job {
    val jobBuilder: JobFlowBuilder =
jobBuilderFactory["superJob"].flow(<put a dummy start step here>)

    return steps
        .fold(jobBuilder) { builder: FlowBuilder<FlowJobBuilder>, step: Step ->
          builder.on("*").to(step)
        }
        .end()
        .build()
  }

条件:

  1. 这将确保从作业启动器启动超级作业时,为不同的参数运行相同的作业。
  2. 这将依次启动工作步骤。如果您正在寻找并行运行它们,请关注( https://stackoverflow.com/a/a/375499696/7275579 )。
  3. 超级作业需要从唯一的作业参数开始,例如当前时间毫秒 /日期,因此在下一步运行中,它不应返回,因为abos已经存在或已完成< / code>。

I was able to achieve the same using jobStep.

private fun jobSteps(jobParams: List<JobParams>): List<Step> =
      jobParams.map { jobParam ->
        jobStep(
            JobParametersBuilder()
                .addString("param1", jobParam.param1)
                .addString("param2", jobParam.param2))
      }

private fun jobStep(jobParameters: JobParameters): Step =
      stepBuilderFactory
          .get("step-{some-unique-name-to-identify-each-job}")
          .job(<Job which needs to be executed for different params>)
          .parametersExtractor { _: Job, _: StepExecution -> jobParameters } // Here we are passing parameters for each job.
          .build()

First create job steps. Now we will have a list of steps. They are steps which are job steps, they need to be wired to a super job and also they are independent of each.

private fun superJob(steps: List<Step>): Job {
    val jobBuilder: JobFlowBuilder =
jobBuilderFactory["superJob"].flow(<put a dummy start step here>)

    return steps
        .fold(jobBuilder) { builder: FlowBuilder<FlowJobBuilder>, step: Step ->
          builder.on("*").to(step)
        }
        .end()
        .build()
  }

Conditions:

  1. This will make sure to run the same job for different parameters when launched the super job from job launcher.
  2. This launches the job steps sequentially. If you're looking for running them in parallel, follow (https://stackoverflow.com/a/37549969/7275579).
  3. The super job needs to be started with a unique job parameter like current time milliseconds / date, so that on its next run, it should not return as job already exists or completed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文