如何在单独的线程Grails / Groovy中运行数据导入脚本?

时间:2013-09-19 06:19:58

标签: multithreading grails groovy

我用来从excel导入数据,但我使用bootstrap.groovy编写代码,并在应用程序启动时调用我的import脚本方法。

这里的场景是我有8000个相关数据一旦导入,如果它们不在我的数据库上。而且,当我将它部署到tomcat6时,它阻止其他应用程序部署,直到它完成导入。所以,我想要使用单独的线程来运行脚本,而不影响性能和阻止其他部署。

代码摘录......

class BootStrap {

    def grailsApplication
    def sessionFactory

    def excelService

def importStateLgaArea(){


        String fileName = grailsApplication.mainContext.servletContext.getRealPath('filename.xlsx')
        ExcelImporter importer = new ExcelImporter(fileName)
        def listState = importer.getStateLgaTerritoryList() //get the map,form excel
        log.info "List form excel:${listState}"

        def checkPreviousImport = Area.findByName('Osusu')
        if(!checkPreviousImport) {
        int  i = 0
        int j = 0 // up

    date cases
            def beforeTime = System.currentTimeMillis()
            for(row in listState){

                def state = State.findByName(row['state']) 
                if(!state) {
                //  log.info "Saving State:${row['state']}"
                    row['state'] = row['state'].toString().toLowerCase().capitalize()
                //  log.info "after capitalized" + row['state']
                    state = new State(name:row['state'])
                    if(!state.save(flash:true)){
                        log.info "${state.errors}"  
                        break;
                    }           
                }

}
}

1 个答案:

答案 0 :(得分:0)

对于导入大数据,我建议考虑使用Spring Batch。很容易将它整合到grails中。您可以尝试this plugin或手动集成。

相关问题