Spring Batch 의 Job Repository 생성시 Spring Batch 와 ResourcelessTransactionManager 를 함께 사용할 때 의도치 않게 문제가 된다.

 

따라서, Jdbc template을 바로 사용하는 다음의 방법으로 변경하였다.

 

bean 설정 파일

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
                          
       <bean id="JobRepositoryCreationService" class="jp.naver.talk.channel.noticenter.batch.repository.JobRepositoryServiceImpl">
        <property name="initScripts" value="classpath:/org/springframework/batch/core/schema-mysql.sql" />
    </bean>
   
    <bean id="JobRepositoryDropService" class="jp.naver.talk.channel.noticenter.batch.repository.JobRepositoryServiceImpl">
        <property name="initScripts" value="classpath:/org/springframework/batch/core/schema-drop-mysql.sql" />
    </bean>
   
    <bean id="scriptExecutionDAO" class="jp.naver.talk.channel.noticenter.batch.repository.ScriptExecutionDAOImpl">
        <property name="dataSource" ref="repositoryDataSource" />
    </bean>
   
</beans>

 

 

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";
}

Posted by '김용환'
,