2017-05-01 22 views
0

我的其餘控制器接收到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 
} 

是否有更好的方法來設計呢?感謝您的任何建議。

+1

將JSON寫入文件並使用物品閱讀器檢索ID? –

+1

爲什麼不加大限度?如果是Spring Batch表格,我敢肯定在代碼中沒有任何限制。這只是一個明智的默認。 –

+0

將作業參數保存在臨時數據庫表中是明智的嗎?因爲之後你會失去實際的參數,這對維護和一般故障排除來說可能是一種痛苦。 –

回答

0

我已採取了@MichaelMinella的建議 - 我已經提高了批處理作業執行參數的250個varchar限制。我的方式做這是

  • 我編輯,我是初始化spring.batch.schema與像

    CREATE TABLE BATCH_JOB_EXECUTION_PARAMS( JOB_EXECUTION_ID BIGINT NOT NULL, TYPE_CD VARCHAR的架構sqlserver.sql腳本(6)NOT NULL, KEY_NAME VARCHAR(100)NOT NULL, STRING_VAL VARCHAR(MAX)NULL, DATE_VAL DATETIME DEFAULT NULL, LONG_VAL BIGINT NULL, DOUBLE_VAL DOUBLE PRECISION NULL, IDENTIFYING CHAR(1)NOT NU LL, 約束JOB_EXEC_PARAMS_FK外鍵(JOB_EXECUTION_ID) 引用BATCH_JOB_EXECUTION(JOB_EXECUTION_ID) );

然後我指向手動編輯架構sqlserver.sql文件application.properties

spring.batch.schema=classpath:BOOT-INF/classes/sql/schema-sqlserver.sql 

我現在能夠通過250多個字符串作爲jobParameters我的春天批處理作業。

相關問題