Spring Batch 의 Job Repository 생성시 Spring Batch 와 ResourcelessTransactionManager 를 함께 사용할 때 의도치 않게 문제가 된다.
따라서, Jdbc template을 바로 사용하는 다음의 방법으로 변경하였다.
bean 설정 파일
JobRepositoryService
public interface JobRepositoryService { public void execute(String replacePreFix); } public class JobRepositoryServiceImpl implements JobRepositoryService { private static final Log logger = LogFactory.getLog(JobRepositoryServiceImpl.class); @Autowired ScriptExecutionDAO scriptExecutionDAO; private Resource scriptResource; public void afterPropertiesSet() throws Exception { Assert.notNull(scriptResource); } private String stripComments(List<String> list) { StringBuffer buffer = new StringBuffer(); for (String line : list) { if (!line.startsWith("//") && !line.startsWith("--")) { buffer.append(line + "\n"); } } return buffer.toString(); } public void setInitScripts(Resource initScripts) { this.scriptResource = initScripts; } @Override public void execute(String replacePreFix) { String[] scripts; try { scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";"); } catch (IOException e) { throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); } for (int i = 0; i < scripts.length; i++) { String script = scripts[i].trim(); if (StringUtils.hasText(script)) { try { // replace script = script.replaceAll("BATCH_", replacePreFix); script = script.replaceAll("_FK", "_" + replacePreFix + "FK"); logger.info(script); scriptExecutionDAO.execute(script); } catch (DataAccessException e) { throw e; } } } } } |
ScriptExecutionDAO
public interface ScriptExecutionDAO { public void execute(String script); } public class ScriptExecutionDAOImpl implements ScriptExecutionDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @Override public void execute(String script) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute(script); } } |
실제 Job repository 호출하는 부분
@Autowired private JobRepositoryService JobRepositoryCreationService; @Autowired private JobRepositoryService JobRepositoryDropService; @RequestMapping(value = "/repository/{type}/{prefix}", method = {RequestMethod.POST, RequestMethod.GET}) @ResponseBody public String repository(@PathVariable String type, @PathVariable String prefix) throws Exception { if (type == null || prefix == null) { return "The type or prefix is not given"; } if (prefix.length() < 2) { return "Prefix length is too shrot."; } if (type.equalsIgnoreCase("create")) { JobRepositoryCreationService.execute(prefix); } else if (type.equalsIgnoreCase("drop")) { JobRepositoryDropService.execute(prefix); } else { return "unrecognized type : " + type; } return "success"; }
|