gpt4 book ai didi

java - 从 ExecutionContext 获取参数

转载 作者:行者123 更新时间:2023-12-02 01:54:56 24 4
gpt4 key购买 nike

我有一个 Spring 批处理作业,它使用如下 Web 参数执行:

https://localhost:8443/batch/async/orz003A?id=123&name=test

我已将这些参数 idtest 添加到我的 ExecutionContext

我在安装 Tasklet 中访问它们时遇到问题,如下所示。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.stereotype.Component;
import com.yrc.mcc.app.online.NTfp211;
import com.yrc.mcc.core.batch.tasklet.AbstractSetupTasklet;


@Component
public class Tfp211SetupTasklet extends AbstractSetupTasklet {

final static Logger LOGGER = LoggerFactory.getLogger(Tfp211SetupTasklet.class);

String gapId;

@Override
protected RepeatStatus performTask(ExecutionContext ec) {

//TODO create the map, check the params for the needed params
// throw an error if the param doesn't exist, because the param
// is necessary to run the job. If the param does exist, set the specific param

if (ec.isEmpty()) {
LOGGER.info("this shit is empty");
}
//setg on GAPID
gapId = ec.toString();
ec.get(BATCH_PROGRAM_PARAMS);
LOGGER.info(gapId);

ec.put(AbstractSetupTasklet.BATCH_PROGRAM_NAME, NTfp211.class.getSimpleName());
return RepeatStatus.FINISHED;
}

}

有什么建议吗?

编辑:

这是我的 AbstractSetupTaskler 的一段内容

Map<String, String> params = new HashMap<>();
if (!ec.containsKey(BATCH_PROGRAM_PARAMS)) {
ec.put(BATCH_PROGRAM_PARAMS, params);
}

在每个作业的SetupTasklet中,我想指定该作业所需的参数

编辑:我有这个小任务,我相信它启动了我的工作

@Component
public class CallM204ProgramTasklet implements Tasklet {

private static final Logger LOGGER = LoggerFactory.getLogger(CallM204ProgramTasklet.class);

@Autowired
private CommonConfig commonConfig;

@Autowired
private ProgramFactory programFactory;

@Autowired
private MidusService midusService;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
ExecutionContext ec = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
jobParameters.getParameters();
String progName = ec.getString(AbstractSetupTasklet.BATCH_PROGRAM_NAME);
Random randomSession = new Random();
String sessionId = "000000" + randomSession.nextInt(1000000);
sessionId = sessionId.substring(sessionId.length() - 6);
SessionData sessionData = new SessionDataImpl("Batch_" + sessionId, commonConfig);
IOHarness io = new BatchIOHarnessImpl(midusService, commonConfig.getMidus().getSendToMidus());
sessionData.setIOHarness(io);
sessionData.setUserId("mccBatch");
Program program = programFactory.createProgram(progName, sessionData);
String progResult = null;
// Create necessary globals for flat file handling.
@SuppressWarnings("unchecked")
Map<String, MccFtpFile> files = (Map<String, MccFtpFile>) ec.get(AbstractSetupTasklet.BATCH_FTP_FILES);
if (files != null) {
for (MccFtpFile mccFtpFile : files.values()) {
program.setg(mccFtpFile.getGlobalName(), mccFtpFile.getLocalFile());
}
}
@SuppressWarnings("unchecked")
Map<String, String> params = (Map<String, String>) ec.get(AbstractSetupTasklet.BATCH_PROGRAM_PARAMS);
//put params into globals
if (params != null) {
params.forEach((k, v) -> program.setg(k, v));
}
try {
program.processUnthreaded(sessionData);
progResult = io.close(sessionData);
} catch (Exception e) {
progResult = "Error running renovated program " + progName + ": " + e.getMessage();
LOGGER.error(progResult, e);
chunkContext.getStepContext().getStepExecution().setExitStatus(ExitStatus.FAILED);
} finally {
String currResult = ec.getString(AbstractSetupTasklet.BATCH_PROGRAM_RESULT).trim();
// Put the program result into the execution context.
ec.putString(AbstractSetupTasklet.BATCH_PROGRAM_RESULT, currResult + "\r" + progResult);
}
return RepeatStatus.FINISHED;
}
}

最佳答案

您需要设置作业启动器并按照此处文档中的描述传递参数:https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#runningJobsFromWebContainer .

之后,您可以从 block 上下文中访问tasklet中的作业参数。例如:

class MyTasklet implements Tasklet {

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
// get id and name from jobParameters
// use id and name to do the required work
return RepeatStatus.FINISHED;
}
}

关于java - 从 ExecutionContext 获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52444951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com