Java Code Examples for org.springframework.batch.core.JobParametersBuilder#addString()
The following examples show how to use
org.springframework.batch.core.JobParametersBuilder#addString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SettleQuartzController.java From seed with Apache License 2.0 | 6 votes |
/** * SpringBatch的断点续跑 * ----------------------------------------------------------------------------------------------- * 执行Step过程中发生异常时,而该异常又没有被配置为skip,那么整个Job会中断 * 当人工修正完异常数据后,再次调用jobLauncher.run(),SpringBatch会从上次异常的地方开始跑 * 1、当Step为读处理写时,假设10条数据,Step配置chunk=3(表明每三条Write一次),若第5条出现异常 * 那么前三条可以成功Write,第4条即便处理成功也不会写入数据库,在修复后再跑的时,会从第4条开始读 * 2、当Step为Tasklet时,仅当Tasklet全部执行完,且未发生异常,才会真正的提交事务,写入数据到数据库 * 即只要其中某一条数据处理时发生异常,那么无论之前提交了多少次数据到数据库,都不会真正的写入数据库 * 3、当并行Step中的某个Step出现异常时,那么并行中的其它Step不受影响,会继续跑完,然后才会中断Job * 修复数据后再跑时,会直接从并行中发生异常的该Step开始跑,其它未发生异常的并行中的Step不会重复跑 * 注意:断点续跑时,传入的jobParameters必须相同,否则会认为是另一个任务,会从头跑,不会从断点的地方跑 * 也就是说,这一切都建立在jobParameters传值相同的条件下 * 另外:对于JobOperator.start()和restart()两个方法都试过,都没实现断点续跑的功能 * ----------------------------------------------------------------------------------------------- */ @RequestMapping("/batch") //@SeedQSSReg(qssHost="${qss.host}", appHost="${qss.appHost}", appname="${qss.appname}", name="${qss.name}", cron="${qss.cron}") CommResult<JobInstance> batch(String bizDate) throws Exception { //判断是否断点续跑 boolean isResume = false; if(StringUtils.isBlank(bizDate)){ bizDate = DateFormatUtils.format(new Date(), "yyyyMMdd"); }else{ isResume = true; } LogUtil.getLogger().info("结算跑批{}:Starting...bizDate={}", isResume?":断点续跑":"", bizDate); //构造JobParameters JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addString("bizDate", bizDate); //执行job JobExecution execution = jobLauncher.run(settleJob, jobParametersBuilder.toJobParameters()); LogUtil.getLogger().info("结算跑批{}:Ending......", isResume?":断点续跑":""); return CommResult.success(execution.getJobInstance()); }
Example 2
Source File: SettleQuartzController.java From seed with Apache License 2.0 | 6 votes |
/** * @param jobNameStr 任务名字符串 * @param jobNameDesc 任务名描述 * @param time 随机数:批量的唯一标志(非断点续跑可直接传null) * @param parameterMap 其它参数:不会作为批量的唯一标志(无参可传null) * Comment by 玄玉<https://jadyer.cn/> on 2019/8/12 18:25. */ private JobExecution runJob(String jobNameStr, String jobNameDesc, String time, Map<String, String> parameterMap) throws Exception { //判断是否断点续跑 boolean isResume = false; long timeLong; if(StringUtils.isBlank(time)){ timeLong = SystemClockUtil.INSTANCE.now(); }else{ isResume = true; timeLong = Long.parseLong(time); } LogUtil.getLogger().info("{}==>{}:Starting...time={}", jobNameDesc, isResume?":断点续跑":"", timeLong); //构造JobParameters JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addLong("time", timeLong); if(null!=parameterMap && !parameterMap.isEmpty()){ for(Map.Entry<String,String> entry : parameterMap.entrySet()){ jobParametersBuilder.addString(entry.getKey(), entry.getValue(), false); } } //执行job Job xmlSettleJob = (Job)SpringContextHolder.getBean(jobNameStr); JobExecution execution = jobLauncher.run(xmlSettleJob, jobParametersBuilder.toJobParameters()); LogUtil.getLogger().info("{}==>{}:Ending......jobInstance={}", jobNameDesc, isResume?":断点续跑":"", execution.getJobInstance()); return execution; }
Example 3
Source File: ServiceUtil.java From spring-batch-lightmin with Apache License 2.0 | 6 votes |
private static void attachJobParameter(final JobParametersBuilder jobParametersBuilder, final String parameterName, final Object parameterValue) { if (parameterValue instanceof Long) { jobParametersBuilder.addLong(parameterName, (Long) parameterValue); } else if (parameterValue instanceof Integer) { jobParametersBuilder.addLong(parameterName, Long.valueOf((Integer) parameterValue)); } else if (parameterValue instanceof Date) { jobParametersBuilder.addDate(parameterName, (Date) parameterValue); } else if (parameterValue instanceof String) { jobParametersBuilder.addString(parameterName, (String) parameterValue); } else if (parameterValue instanceof Double) { jobParametersBuilder.addDouble(parameterName, (Double) parameterValue); } else { log.error("Could not add Parameter. Cause: Unsupported Parameter Type:" + parameterValue.getClass() + " !"); } }
Example 4
Source File: ProductImportSeviceBatchImpl.java From website with GNU Affero General Public License v3.0 | 5 votes |
@Override public void importProductsFromFile(String filename) throws Exception { JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addString("input.file.name", filename); JobParameters jobParameters = jobParametersBuilder.toJobParameters(); jobLauncher.run(job, jobParameters); }
Example 5
Source File: JobLauncherDetails.java From spring-batch-quartz-admin with Apache License 2.0 | 5 votes |
private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) { JobParametersBuilder builder = new JobParametersBuilder(); for (Entry<String, Object> entry : jobDataMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (value instanceof String && !key.equals(Constants.JOB_NAME)) { builder.addString(key, (String) value); } else if (value instanceof Float || value instanceof Double) { builder.addDouble(key, ((Number) value).doubleValue()); } else if (value instanceof Integer || value instanceof Long) { builder.addLong(key, ((Number) value).longValue()); } else if (value instanceof Date) { builder.addDate(key, (Date) value); } else { // JobDataMap contains values which are not job parameters // (ignoring) } } // Needs a unique job parameter to rerun the completed job builder.addDate(Constants.JOB_RUN_DATE, new Date()); return builder.toJobParameters(); }
Example 6
Source File: SpringBatchApplication.java From tutorials with MIT License | 5 votes |
@Override public void run(String... args) throws Exception { JobParametersBuilder paramsBuilder = new JobParametersBuilder(); paramsBuilder.addString("file.input", input); paramsBuilder.addString("file.output", output); jobLauncher.run(transformBooksRecordsJob, paramsBuilder.toJobParameters()); }
Example 7
Source File: DomainParameterParser.java From spring-batch-lightmin with Apache License 2.0 | 4 votes |
/** * maps a String a parameters to Spring Batch {@link JobParameters}. * The String has to have the following format: * <ul> * <li> * name(type)=value, name(type2)=value2 * </li> * </ul> * The name is the job parameter name, the type, the Java type to the value. * Following Types are supported * <ul> * <li> * {@link String} * </li> * <li> * {@link Long} * </li> * <li> * {@link Double} * </li> * <li> * {@link Date} * </li> * </ul> * * @param parameters String of parameters * @return Spring Batch {@link JobParameters} */ public static JobParameters parseParametersToJobParameters(final String parameters) { final JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); final Map<String, Object> parametersMap = parseParameters(parameters); for (final Entry<String, Object> entry : parametersMap.entrySet()) { final Object value = entry.getValue(); if (value instanceof String) { jobParametersBuilder.addString(entry.getKey(), (String) value); } else if (value instanceof Long) { jobParametersBuilder.addLong(entry.getKey(), (Long) value); } else if (value instanceof Date) { jobParametersBuilder.addDate(entry.getKey(), (Date) value); } else if (value instanceof Double) { jobParametersBuilder.addDouble(entry.getKey(), (Double) value); } else { throw new SpringBatchLightminApplicationException("Unknown Parameter type:" + value.getClass().getName()); } } return jobParametersBuilder.toJobParameters(); }
Example 8
Source File: SpringBatchRetryIntegrationTest.java From tutorials with MIT License | 4 votes |
private JobParameters defaultJobParameters() { JobParametersBuilder paramsBuilder = new JobParametersBuilder(); paramsBuilder.addString("jobID", String.valueOf(System.currentTimeMillis())); return paramsBuilder.toJobParameters(); }
Example 9
Source File: SpringBatchStepScopeIntegrationTest.java From tutorials with MIT License | 4 votes |
private JobParameters defaultJobParameters() { JobParametersBuilder paramsBuilder = new JobParametersBuilder(); paramsBuilder.addString("file.input", TEST_INPUT_ONE); paramsBuilder.addString("file.output", TEST_OUTPUT); return paramsBuilder.toJobParameters(); }
Example 10
Source File: SpringBatchIntegrationTest.java From tutorials with MIT License | 4 votes |
private JobParameters defaultJobParameters() { JobParametersBuilder paramsBuilder = new JobParametersBuilder(); paramsBuilder.addString("file.input", TEST_INPUT); paramsBuilder.addString("file.output", TEST_OUTPUT); return paramsBuilder.toJobParameters(); }