我的其餘控制器接收到List的id並將List的json字符串作爲jobParameter發送到Spring Batch Job。Spring Batch 3.0傳遞250+字符串作爲工作的最佳方式參數
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
RequestMapping(value="/startjob", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<EventReports> addReportIds(@RequestBody List<Integer> reportIds) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
Logger logger = LoggerFactory.getLogger(this.getClass());
try {
JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
.addString("eventType", "Event Reports")
.addString("reportIdsJson", reportIds.toString())
.toJobParameters();
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
logger.info(e.getMessage());
}
System.out.println("Completed event reports batch job");
return null;
}
我的Spring Batch的讀者看起來像
@Component
@StepScope
public class Reader implements ItemReader<String> {
private String[] messages = {"Hello World!", "Welcome to Spring Batch!"};
@Value("#{jobParameters['time']}")
private Long time;
@Value("#{jobParameters['eventType']}")
private String eventType;
@Value("#{jobParameters['reportIdsJson']}")
private String reportIdsJson;
private int count=0;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
System.out.println("Time: " + time);
System.out.println("Type: " + eventType);
System.out.println("JSON: " + reportIdsJson);
if(count < messages.length){
return messages[count++];
}else{
count=0;
}
我遇到的問題是 - JSON字符串我傳遞一個jobParameter可以說是相當大的,而且我絕對比250個字符限制。當JSON字符串,我傳遞的jobParameter我的閱讀器是超過250個字符時,我得到的是這樣的
Started event reports batch job
2017-05-01 10:50:03.662 INFO 8724 --- [nio-8081-exec-3] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean def
initions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2017-05-01 10:50:03.788 INFO 8724 --- [nio-8081-exec-3] o.s.jdbc.support.SQLErrorCodesFactory : SQLErrorCodes loaded
: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2017-05-01 10:50:04.296 INFO 8724 --- [nio-8081-exec-3] c.u.r.s.RailAgentCollectorServiceImpl : PreparedStatementCal
lback; SQL [INSERT into BATCH_JOB_EXECUTION_PARAMS(JOB_EXECUTION_ID, KEY_NAME, TYPE_CD, STRING_VAL, DATE_VAL, LONG_VAL,
DOUBLE_VAL, IDENTIFYING) values (?, ?, ?, ?, ?, ?, ?, ?)]; String or binary data would be truncated.; nested exception i
s com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.
Completed event reports batch job
我已經看過了各種方法來這個問題的錯誤。我現在正在嘗試的是 - 不是將非常大的JSON字符串作爲jobParameter傳遞給Spring Batch,而是將其作爲String保存到臨時數據庫表中 - 然後使用傳遞給Spring Batch的其他jobParameters來查詢我的temp非常大的JSON字符串的數據庫表。所以,在開始我的Spring Batch作業之前,我必須將JSON字符串保存到我的臨時數據庫表中。這個解決方案對我來說似乎並不「乾淨」 - 理想情況下,我只想將我的大型JSON字符串傳遞給我的批處理作業,並立即開始處理。相反,我必須首先將非常大的JSON字符串保存到臨時數據庫,然後才能開始我的Spring Batch作業 - 因此這部分處理在Spring Batch之外存在。所以,我在這種情況下,代碼看起來像
//save list of Integers JSON to temp db table here
try {
JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
.addString("eventType", "Event Reports")
.toJobParameters();
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
logger.info(e.getMessage());
}
System.out.println("Completed event reports batch job");
return null;
}
我休息控制器,併爲我的讀者
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
System.out.println("Time: " + time);
System.out.println("Type: " + eventType);
//use above 2 job parameters to query temp db table for large JSON string
//pass large JSON string to my Spring Batch Processor
}
是否有更好的方法來設計呢?感謝您的任何建議。
將JSON寫入文件並使用物品閱讀器檢索ID? –
爲什麼不加大限度?如果是Spring Batch表格,我敢肯定在代碼中沒有任何限制。這只是一個明智的默認。 –
將作業參數保存在臨時數據庫表中是明智的嗎?因爲之後你會失去實際的參數,這對維護和一般故障排除來說可能是一種痛苦。 –