从JSR 352中的分区步骤访问JobContext [英] Accesing JobContext from a partitioned step in JSR 352

查看:122
本文介绍了从JSR 352中的分区步骤访问JobContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在批处理之间传递对象,但是在尝试从分区步骤(批处理)访问jobContext时遇到了问题.

I'm trying to pass an object between batchlets, but I've encountered a problem when trying to access the jobContext from a partitioned step (batchlet).

根据JSR 352规范

According to the JSR 352 Specification

9.4.1.1批处理上下文生命周期和范围: 批处理上下文具有线程关联性,并且仅对批处理可见 在该特定线程上执行的工件.批处理上下文注入 超出范围时,该字段可以为null.每个上下文类型都有一个不同的 范围和生命周期如下: 1. JobContext 每个作业执行有一个JobContext.它存在一个生命 工作. 并行的每个子线程都有一个不同的JobContext 执行(例如,分区步骤). 2. StepContext 每步执行只有一个StepContext.它存在的生命 步骤.对于一个分区的步骤,有一个StepContext用于 父步骤/线程;每个子线程都有一个不同的StepContext.

9.4.1.1 Batch Context Lifecycle and Scope: A batch context has thread affinity and is visible only to the batch artifacts executing on that particular thread. A batch context injected field may be null when out of scope. Each context type has a distinct scope and lifecycle as follows: 1. JobContext There is one JobContext per job execution. It exists for the life of a job. There is a distinct JobContext for each sub-thread of a parallel execution (e.g. partitioned step). 2. StepContext There is one StepContext per step execution. It exists for the life of the step. For a partitioned step, there is one StepContext for the parent step/thread; there is a distinct StepContext for each sub-thread.

我(失败的)解决方案是使用JobContext.setTransientUserData,但是由于分区步骤使用了不同的JobContext,所以我无法获得TransientUserData.

My (failed) solution was to use the JobContext.setTransientUserData, but because the partitioned step uses a distinct JobContext I can't get the TransientUserData.

我要尝试的方法是否有替代方法?使用PartitionMapper属性是不可能的,因为我需要将一个对象而不是字符串传递给每个分区.

Is there an alternative to what I'm trying to do? Using PartitionMapper properties it's not possible because I need to pass an object, not a string to every partition.

为清楚起见,我需要这样做:

To be clear, I need to do this:

  1. NormalBatchlet->保存要在下一步中使用的对象.
  2. PartitionedBatchlet->在上一步中获取已保存的对象.该对象不是简单的String,因此使用PartitionMapper属性不是解决方案.

更新

我现在正在使用带有HashMap的简单Singleton EJB在步骤之间存储对象,当作业完成时,我会清除此映射以避免资源泄漏.

I'm now using a simple Singleton EJB with a HashMap to store objects between steps and when the job is finished I clear this map to avoid resources leak.

这是一种解决方法,因为我真的只想使用javax.batch包而不依赖于EJB,所以我不打算将其作为答案.

This is a workaround because I really want to use only the javax.batch package and not depend on EJB's, so I'm not putting it as an answer.

推荐答案

您可以尝试这样的方法,该方法应符合当前的规范编程模型.

You could try something like this which should conform with the current spec programming model.

使用NormalBatchlet中的持久性步骤数据将第一步中的对象存储起来:

Store the object from your first step using the persistent step data in your NormalBatchlet:

stepCtx.setPersistentUserData(mySerializableData);

通过查找上一步来从分区的第一步中检索数据:

Retrieve the data from the first step in your partitions, by looking up the previous step:

Long execId = jobCtx.getExecutionId();

List<StepExecution> stepExecs = jobOperator.getStepExecutions(execID);

MyPersistentUserData myData;

for (StepExecution step : stepExecs) {
    if (step.getStepName().equals("somePreviousStepX") {
        myData = (MyPersistentUserData)step.getPersistentUserData();
    }
}

//use myData

这篇关于从JSR 352中的分区步骤访问JobContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆